1 20 package org.enhydra.barracuda.core.forms; 21 22 import java.math.BigDecimal ; 23 import java.text.*; 24 import java.sql.Time ; 25 import java.sql.Timestamp ; 26 import java.util.*; 27 import java.util.Date ; 28 29 import org.enhydra.barracuda.plankton.*; 30 31 55 public abstract class FormType { 56 57 public static FormType STRING = new FormType() { 59 public Class getFormClass() { 60 return String .class; 61 } 62 63 public Object parse(String origVal, Locale locale) throws ParseException { 64 return origVal; 65 } 66 67 public Object [] getTypeArray(int size) { 68 return new String [size]; 69 } 70 }; 71 72 public static FormType BOOLEAN = new FormType() { 74 public Class getFormClass() { 75 return Boolean .class; 76 } 77 78 public Object parse(String origVal, Locale locale) throws ParseException { 79 if (origVal==null) 81 return null; 82 83 String tval = origVal.trim().toLowerCase(); 84 if (tval.equals("on") || tval.equals("yes") || tval.equals("true") || tval.equals("y")) { origVal = "true"; 87 } else if (tval.equals("off") || tval.equals("no") || tval.equals("false") || tval.equals("n")) { origVal = "false"; 90 } else { 91 throw new ParseException(origVal); 92 } 93 return new Boolean (origVal); 94 } 95 96 public Object [] getTypeArray(int size) { 97 return new Boolean [size]; 98 } 99 }; 100 101 public static FormType INTEGER = new FormType() { 103 public Class getFormClass() { 104 return Integer .class; 105 } 106 107 public Object parse(String origVal, Locale locale) throws ParseException { 108 if (origVal==null) 110 return null; 111 112 try { 113 return new Integer (origVal); 114 } catch (NumberFormatException e) { 115 try { 116 Double d1 = new Double (origVal); 124 int d1val = d1.intValue(); 125 Double d2 = new Double (d1val); 126 if (d1.equals(d2)) { 127 return new Integer (d1val); 128 } else { 129 throw e; 130 } 131 } catch (NumberFormatException e2) { 132 throw new ParseException(e2); 133 } 134 } 135 } 136 public Object [] getTypeArray(int size) { 137 return new Integer [size]; 138 } 139 }; 140 141 public static FormType LONG = new FormType() { 143 public Class getFormClass() { 144 return Long .class; 145 } 146 147 public Object parse(String origVal, Locale locale) throws ParseException { 148 if (origVal==null) 150 return null; 151 152 try { 156 return new Long (origVal); 157 } catch (NumberFormatException e) { 158 try { 159 Double d1 = new Double (origVal); 167 long d1val = d1.longValue(); 168 Double d2 = new Double (d1val); 169 if (d1.equals(d2)) { 170 return new Long (d1val); 171 } else { 172 throw e; 173 } 174 } catch (NumberFormatException e2) { 175 throw new ParseException(e2); 176 } 177 } 178 } 180 181 public Object [] getTypeArray(int size) { 182 return new Long [size]; 183 } 184 }; 185 186 public static FormType SHORT = new FormType() { 188 public Class getFormClass() { 189 return Short .class; 190 } 191 192 public Object parse(String origVal, Locale locale) throws ParseException { 193 if (origVal==null) 195 return null; 196 197 try { 201 return new Short (origVal); 202 } catch (NumberFormatException e) { 203 try { 204 Double d1 = new Double (origVal); 212 short d1val = d1.shortValue(); 213 Double d2 = new Double (d1val); 214 if (d1.equals(d2)) { 215 return new Short (d1val); 216 } else { 217 throw e; 218 } 219 } catch (NumberFormatException e2) { 220 throw new ParseException(e2); 221 } 222 } 223 } 225 public Object [] getTypeArray(int size) { 226 return new Short [size]; 227 } 228 }; 229 230 public static FormType DOUBLE = new FormType() { 232 public Class getFormClass() { 233 return Double .class; 234 } 235 236 public Object parse(String origVal, Locale locale) throws ParseException { 237 if (origVal==null) 239 return null; 240 241 try { 242 return new Double (origVal); 243 } catch (NumberFormatException e) { 244 throw new ParseException(e); 245 } 246 } 247 248 public Object [] getTypeArray(int size) { 249 return new Double [size]; 250 } 251 }; 252 253 public static FormType FLOAT = new FormType() { 255 public Class getFormClass() { 256 return Float .class; 257 } 258 259 public Object parse(String origVal, Locale locale) throws ParseException { 260 if (origVal==null) 262 return null; 263 264 try { 265 return new Float (origVal); 266 } catch (NumberFormatException e) { 267 throw new ParseException(e); 268 } 269 } 270 271 public Object [] getTypeArray(int size) { 272 return new Float [size]; 273 } 274 }; 275 276 public static FormType BIG_DECIMAL = new FormType() { 278 public Class getFormClass() { 279 return BigDecimal .class; 280 } 281 282 public Object parse(String origVal, Locale locale) throws ParseException { 283 294 try { 296 String s = origVal.trim(); 297 s = StringUtil.replace(s, "$",""); s = StringUtil.replace(s, "£",""); s = StringUtil.replace(s, ",",""); if (s.startsWith("(") && s.endsWith(")")) { s = "-"+s.substring(1,s.length()-1); 302 } 303 return new BigDecimal (s); 304 } catch (NumberFormatException e) { 305 throw new ParseException(e); 306 } 307 308 309 } 311 312 public Object [] getTypeArray(int size) { 313 return new BigDecimal [size]; 314 } 315 }; 316 317 public static FormType DATE = new FormType() { 319 public Class getFormClass() { 320 return java.util.Date .class; 321 } 322 323 public Object parse(String origVal, Locale locale) throws ParseException { 324 if (origVal==null) return null; 326 327 if (locale==null) locale = Locale.getDefault(); 328 329 try { 331 return new Date (Long.parseLong(origVal)); 332 } catch (NumberFormatException e0) { 333 try { 336 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); df.setLenient(false); 339 return df.parse(origVal); 340 } catch (java.text.ParseException e1) { 341 try { 342 DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); df.setLenient(false); 345 return df.parse(origVal); 346 } catch (java.text.ParseException e2) { 347 try { 348 DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, locale); df.setLenient(false); 351 return df.parse(origVal); 352 } catch (java.text.ParseException e3) { 353 try { 354 DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale); df.setLenient(false); 357 return df.parse(origVal); 358 } catch (java.text.ParseException e4) { 359 throw new ParseException(e4, "Locale is " + locale.getCountry()); 360 368 } 369 } 370 } 371 } 373 } } 375 376 public Object [] getTypeArray(int size) { 377 return new Date [size]; 378 } 379 }; 380 381 public static FormType TIMESTAMP = new FormType() { 384 public Class getFormClass() { 385 return Timestamp .class; 386 } 387 388 public Object parse(String origVal, Locale locale) throws ParseException { 389 409 if (origVal==null) return null; 410 if (locale==null) locale = Locale.getDefault(); 411 412 try { 414 return new Timestamp (Long.parseLong(origVal)); 415 } catch (NumberFormatException e0) { 416 try { 418 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale); 419 df.setLenient(false); 420 return new Timestamp (df.parse(origVal).getTime()); 421 } catch (java.text.ParseException e1) { 422 try { 423 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale); 424 df.setLenient(false); 425 return new Timestamp (df.parse(origVal).getTime()); 426 } catch (java.text.ParseException e2) { 427 try { 428 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 429 df.setLenient(false); 430 return new Timestamp (df.parse(origVal).getTime()); 431 } catch (java.text.ParseException e3) { 432 try { 433 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale); 434 df.setLenient(false); 435 return new Timestamp (df.parse(origVal).getTime()); 436 } catch (java.text.ParseException e4) { 437 try { 439 return Timestamp.valueOf(origVal); 440 } catch (IllegalArgumentException e5) { 441 throw new ParseException(e5, "Locale is "+locale.getCountry()); 442 } 443 } 444 } 445 } 446 } 447 } 448 } 450 451 public Object [] getTypeArray(int size) { 452 return new Timestamp [size]; 453 } 454 }; 455 456 460 public static FormType TIME = new FormType() { 461 public Class getFormClass() { 462 return Time .class; 463 } 464 465 public Object parse(String origVal, Locale locale) throws ParseException { 466 if (origVal==null) return null; 467 if (locale==null) locale = Locale.getDefault(); 468 469 try { 471 return new Time (Long.parseLong(origVal)); 472 } catch (NumberFormatException e0) { 473 try { 475 DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, locale); 476 df.setLenient(false); 477 return new Time (df.parse(origVal).getTime()); 478 } catch (java.text.ParseException e1) { 479 try { 480 DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale); 481 df.setLenient(false); 482 return new Time (df.parse(origVal).getTime()); 483 } catch (java.text.ParseException e2) { 484 try { 485 DateFormat df = DateFormat.getTimeInstance(DateFormat.LONG, locale); 486 df.setLenient(false); 487 return new Time (df.parse(origVal).getTime()); 488 } catch (java.text.ParseException e3) { 489 try { 490 DateFormat df = DateFormat.getTimeInstance(DateFormat.FULL, locale); 491 df.setLenient(false); 492 return new Time (df.parse(origVal).getTime()); 493 } catch (java.text.ParseException e4) { 494 try { 496 return Time.valueOf(origVal); 497 } catch (IllegalArgumentException e5) { 498 throw new ParseException(e5, "Locale is "+locale.getCountry()); 499 } 500 } 501 } 502 } 503 } 504 } 505 } 507 508 public Object [] getTypeArray(int size) { 509 return new Time [size]; 510 } 511 }; 512 513 518 protected FormType() { } 519 520 523 public abstract Class getFormClass(); 524 525 528 public Object parse(String origVal) throws ParseException { 529 return parse(origVal, null); 530 } 531 532 535 public abstract Object parse(String origVal, Locale loc) throws ParseException; 536 537 538 541 public abstract Object [] getTypeArray(int size); 542 543 546 public String toString() { 547 return this.getFormClass().getName(); 548 } 549 550 } 551 | Popular Tags |