| Home >> All >> com >> aendvari >> common >> [ util Javadoc ] |
Source code: com/aendvari/common/util/ConversionUtil.java
1 /* 2 * ConversionUtil.java 3 * 4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved. 5 * 6 * See the file LICENSE for terms of use. 7 * 8 */ 9 10 package com.aendvari.common.util; 11 12 import java.util.*; 13 import java.lang.*; 14 15 import java.text.SimpleDateFormat; 16 import java.text.FieldPosition; 17 18 import java.net.URLDecoder; 19 20 import java.lang.reflect.*; 21 import java.lang.NoSuchMethodException; 22 import java.lang.IllegalAccessException; 23 24 import java.io.IOException; 25 26 import javax.servlet.http.*; 27 import javax.servlet.jsp.*; 28 import javax.servlet.jsp.tagext.*; 29 30 import com.aendvari.tethys.*; 31 import com.aendvari.tethys.context.*; 32 33 34 /** 35 * <p>Utility class for conversion functions.</p> 36 * 37 * @author Scott Milne 38 * 39 */ 40 41 public class ConversionUtil 42 { 43 /** 44 * Converts a string into a integer. 45 * 46 * @param sIntegerString The string to convert. 47 * 48 * @return The integer value or 0 if invalid. 49 * 50 */ 51 52 public static int stringToInt( String sIntegerString ) 53 { 54 if( !isValidIntegerValue(sIntegerString) ) 55 return new Integer(0).intValue(); 56 57 return new Integer(sIntegerString).intValue(); 58 } 59 60 /** 61 * Determines if the string value can be converted safely into a integer. 62 * 63 * @param sIntegerString The string to convert. 64 * 65 * @return True if the value can be converted. 66 * 67 */ 68 69 public static boolean isValidIntegerValue( String sIntegerString ) 70 { 71 if (!sIntegerString.equals("")) 72 { 73 try 74 { 75 Integer.parseInt(sIntegerString); 76 } 77 catch (NumberFormatException exception) 78 { 79 return false; 80 } 81 82 return true; 83 } 84 85 // empty string is NOT OK 86 return false; 87 } 88 89 /** 90 * Converts a string into a <code>double</code>. 91 * 92 * @param sDoubleString The string to convert. 93 * 94 * @return The <code>double</code> value or 0 if invalid. 95 * 96 */ 97 98 public static double stringToDouble( String sDoubleString ) 99 { 100 if( !isValidDoubleValue(sDoubleString) ) 101 return new Double(0).doubleValue(); 102 103 return new Double(sDoubleString).doubleValue(); 104 } 105 106 /** 107 * Determines if the string value can be converted safely into a <code>double</code>. 108 * 109 * @param sDoubleString The string to convert. 110 * 111 * @return True if the value can be converted. 112 * 113 */ 114 115 public static boolean isValidDoubleValue( String sDoubleString ) 116 { 117 if (!sDoubleString.equals("")) 118 { 119 try 120 { 121 Double.parseDouble(sDoubleString); 122 } 123 catch (NumberFormatException exception) 124 { 125 return false; 126 } 127 128 return true; 129 } 130 131 // empty string is NOT OK 132 return false; 133 } 134 135 136 /** 137 * Converts all \n to <BR>. This method leaves the original string alone 138 * and creates a new string to return instead. 139 * 140 * @param sString The string to convert. 141 * 142 * @return The converted string. 143 * 144 */ 145 146 public static String nl2br( String sString ) 147 { 148 StringBuffer buffer = new StringBuffer(); 149 150 StringBuffer stringBuffer = new StringBuffer(sString); 151 152 for (int i=0; i<sString.length(); i++) 153 { 154 char ch = stringBuffer.charAt(i); 155 156 if (ch == '\r' || ch == '\n') 157 { 158 if (ch == '\n') 159 { 160 buffer.append("<BR>"); 161 } 162 } 163 else 164 { 165 buffer.append(ch); 166 } 167 } 168 169 return buffer.toString(); 170 } 171 172 /** 173 * Converts all < or > to their HTML equivalent. 174 * 175 * This method leaves the original string alone 176 * and creates a new string to return instead. 177 * 178 * @param sString The string to convert. 179 * 180 * @return The converted string. 181 * 182 */ 183 184 public static String ltgt2HTML( String sString ) 185 { 186 StringBuffer buffer = new StringBuffer(); 187 188 int length = sString.length(); 189 190 for (int index=0; index<length; index++) 191 { 192 char ch = sString.charAt(index); 193 194 if (ch == '<') 195 { 196 buffer.append("<"); 197 } 198 else if (ch == '>') 199 { 200 buffer.append(">"); 201 } 202 else 203 { 204 buffer.append(ch); 205 } 206 } 207 208 return buffer.toString(); 209 } 210 211 /** 212 * Converts all " into " 213 * 214 * @param value The string to convert. 215 * 216 * @return The converted string. 217 * 218 */ 219 220 public static String doubleQuotesToHtml(String value) 221 { 222 StringBuffer newValue = new StringBuffer(); 223 224 int index; 225 226 for (index = 0; index < value.length(); index++) 227 { 228 char c = value.charAt(index); 229 230 // replace ' with '' 231 if (c == '"') 232 newValue.append("""); 233 else 234 newValue.append(c); 235 } 236 237 return newValue.toString(); 238 } 239 240 /** 241 * Converts the <code>Date</code> into a string form using the format provided. 242 * See <code>SimpleDateFormat</code> for format options. 243 * 244 * @param date The date to convert. 245 * @param format The format the date is to be converted into. 246 * 247 * @return The converted string 248 * 249 */ 250 251 public static String dateToString( Date date, String format ) 252 { 253 SimpleDateFormat formatter = new SimpleDateFormat(format); 254 255 StringBuffer result = new StringBuffer(); 256 StringBuffer buffer = formatter.format( 257 date, 258 result, 259 new FieldPosition(SimpleDateFormat.DATE_FIELD) 260 ); 261 262 return buffer.toString(); 263 } 264 265 /** 266 * Converts the <code>Date</code> into a string form using the format provided. 267 * See <code>SimpleDateFormat</code> for format options. 268 * 269 * @param dateString The string to convert. 270 * 271 * @return The converted string. 272 * 273 */ 274 275 public static Date stringToDateMMDDYYYY( String dateString ) 276 throws java.text.ParseException 277 { 278 SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); 279 280 Date nativeDate = formatter.parse(dateString); 281 282 return nativeDate; 283 } 284 285 /** 286 * Converts the <code>Date</code> into a string form using the format provided. 287 * See <code>SimpleDateFormat</code> for format options. 288 * 289 * @param dateString The string to convert. 290 * 291 * @return The converted string 292 * 293 */ 294 295 public static Date stringToDateYYYYMMDD( String dateString ) 296 throws java.text.ParseException 297 { 298 SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); 299 300 Date nativeDate = formatter.parse(dateString); 301 302 return nativeDate; 303 } 304 } 305