1 21 package org.apache.commons.validator; 22 23 import java.io.Serializable ; 24 import java.util.Date ; 25 import java.util.Locale ; 26 import java.text.DateFormat ; 27 import java.text.SimpleDateFormat ; 28 import java.text.NumberFormat ; 29 import java.text.ParseException ; 30 import java.text.ParsePosition ; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 38 public class GenericTypeValidator implements Serializable { 39 40 private final static Log log = LogFactory.getLog(GenericTypeValidator.class); 41 42 48 public static Byte formatByte(String value) { 49 if (value == null) { 50 return null; 51 } 52 53 try { 54 return new Byte (value); 55 } catch (NumberFormatException e) { 56 return null; 57 } 58 59 } 60 61 69 public static Byte formatByte(String value, Locale locale) { 70 Byte result = null; 71 72 if (value != null) { 73 NumberFormat formatter = null; 74 if (locale != null) { 75 formatter = NumberFormat.getIntegerInstance(locale); 76 } else { 77 formatter = NumberFormat.getIntegerInstance(Locale.getDefault()); 78 } 79 ParsePosition pos = new ParsePosition (0); 80 Number num = formatter.parse(value, pos); 81 82 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { 84 if (num.doubleValue() >= Byte.MIN_VALUE && 85 num.doubleValue() <= Byte.MAX_VALUE) { 86 result = new Byte (num.byteValue()); 87 } 88 } 89 } 90 91 return result; 92 } 93 94 100 public static Short formatShort(String value) { 101 if (value == null) { 102 return null; 103 } 104 105 try { 106 return new Short (value); 107 } catch (NumberFormatException e) { 108 return null; 109 } 110 111 } 112 113 121 public static Short formatShort(String value, Locale locale) { 122 Short result = null; 123 124 if (value != null) { 125 NumberFormat formatter = null; 126 if (locale != null) { 127 formatter = NumberFormat.getIntegerInstance(locale); 128 } else { 129 formatter = NumberFormat.getIntegerInstance(Locale.getDefault()); 130 } 131 ParsePosition pos = new ParsePosition (0); 132 Number num = formatter.parse(value, pos); 133 134 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { 136 if (num.doubleValue() >= Short.MIN_VALUE && 137 num.doubleValue() <= Short.MAX_VALUE) { 138 result = new Short (num.shortValue()); 139 } 140 } 141 } 142 143 return result; 144 } 145 146 152 public static Integer formatInt(String value) { 153 if (value == null) { 154 return null; 155 } 156 157 try { 158 return new Integer (value); 159 } catch (NumberFormatException e) { 160 return null; 161 } 162 163 } 164 165 173 public static Integer formatInt(String value, Locale locale) { 174 Integer result = null; 175 176 if (value != null) { 177 NumberFormat formatter = null; 178 if (locale != null) { 179 formatter = NumberFormat.getIntegerInstance(locale); 180 } else { 181 formatter = NumberFormat.getIntegerInstance(Locale.getDefault()); 182 } 183 ParsePosition pos = new ParsePosition (0); 184 Number num = formatter.parse(value, pos); 185 186 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { 188 if (num.doubleValue() >= Integer.MIN_VALUE && 189 num.doubleValue() <= Integer.MAX_VALUE) { 190 result = new Integer (num.intValue()); 191 } 192 } 193 } 194 195 return result; 196 } 197 198 204 public static Long formatLong(String value) { 205 if (value == null) { 206 return null; 207 } 208 209 try { 210 return new Long (value); 211 } catch (NumberFormatException e) { 212 return null; 213 } 214 215 } 216 217 225 public static Long formatLong(String value, Locale locale) { 226 Long result = null; 227 228 if (value != null) { 229 NumberFormat formatter = null; 230 if (locale != null) { 231 formatter = NumberFormat.getIntegerInstance(locale); 232 } else { 233 formatter = NumberFormat.getIntegerInstance(Locale.getDefault()); 234 } 235 ParsePosition pos = new ParsePosition (0); 236 Number num = formatter.parse(value, pos); 237 238 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { 240 if (num.doubleValue() >= Long.MIN_VALUE && 241 num.doubleValue() <= Long.MAX_VALUE) { 242 result = new Long (num.longValue()); 243 } 244 } 245 } 246 247 return result; 248 } 249 250 256 public static Float formatFloat(String value) { 257 if (value == null) { 258 return null; 259 } 260 261 try { 262 return new Float (value); 263 } catch (NumberFormatException e) { 264 return null; 265 } 266 267 } 268 269 277 public static Float formatFloat(String value, Locale locale) { 278 Float result = null; 279 280 if (value != null) { 281 NumberFormat formatter = null; 282 if (locale != null) { 283 formatter = NumberFormat.getInstance(locale); 284 } else { 285 formatter = NumberFormat.getInstance(Locale.getDefault()); 286 } 287 ParsePosition pos = new ParsePosition (0); 288 Number num = formatter.parse(value, pos); 289 290 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { 292 if (num.doubleValue() >= (Float.MIN_VALUE * -1) && 293 num.doubleValue() <= Float.MAX_VALUE) { 294 result = new Float (num.floatValue()); 295 } 296 } 297 } 298 299 return result; 300 } 301 302 308 public static Double formatDouble(String value) { 309 if (value == null) { 310 return null; 311 } 312 313 try { 314 return new Double (value); 315 } catch (NumberFormatException e) { 316 return null; 317 } 318 319 } 320 321 329 public static Double formatDouble(String value, Locale locale) { 330 Double result = null; 331 332 if (value != null) { 333 NumberFormat formatter = null; 334 if (locale != null) { 335 formatter = NumberFormat.getInstance(locale); 336 } else { 337 formatter = NumberFormat.getInstance(Locale.getDefault()); 338 } 339 ParsePosition pos = new ParsePosition (0); 340 Number num = formatter.parse(value, pos); 341 342 if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { 344 if (num.doubleValue() >= (Double.MIN_VALUE * -1) && 345 num.doubleValue() <= Double.MAX_VALUE) { 346 result = new Double (num.doubleValue()); 347 } 348 } 349 } 350 351 return result; 352 } 353 354 366 public static Date formatDate(String value, Locale locale) { 367 Date date = null; 368 369 if (value == null) { 370 return null; 371 } 372 373 try { 374 DateFormat formatter = null; 375 if (locale != null) { 376 formatter = 377 DateFormat.getDateInstance(DateFormat.SHORT, locale); 378 } else { 379 formatter = 380 DateFormat.getDateInstance( 381 DateFormat.SHORT, 382 Locale.getDefault()); 383 } 384 385 formatter.setLenient(false); 386 387 date = formatter.parse(value); 388 } catch (ParseException e) { 389 log.warn(value, e); 391 } 392 393 return date; 394 } 395 396 410 public static Date formatDate(String value, String datePattern, boolean strict) { 411 Date date = null; 412 413 if (value == null 414 || datePattern == null 415 || datePattern.length() == 0) { 416 return null; 417 } 418 419 try { 420 SimpleDateFormat formatter = new SimpleDateFormat (datePattern); 421 formatter.setLenient(false); 422 423 date = formatter.parse(value); 424 425 if (strict) { 426 if (datePattern.length() != value.length()) { 427 date = null; 428 } 429 } 430 } catch (ParseException e) { 431 log.warn(value, e); 433 } 434 435 return date; 436 } 437 438 448 public static Long formatCreditCard(String value) { 449 return GenericValidator.isCreditCard(value) ? new Long (value) : null; 450 } 451 452 } 453 454 | Popular Tags |