KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > FormType


1 /*
2  * Copyright (C) 2003 Chris Webb <chris.webb@voxsurf.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: FormType.java,v 1.28 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms;
21
22 import java.math.BigDecimal JavaDoc;
23 import java.text.*;
24 import java.sql.Time JavaDoc;
25 import java.sql.Timestamp JavaDoc;
26 import java.util.*;
27 import java.util.Date JavaDoc;
28
29 import org.enhydra.barracuda.plankton.*;
30
31 /**
32  * This class defines all valid FormTypes. Currently we support:
33  *
34  * <ul>
35  * <li>String</li>
36  * <li>Boolean</li>
37  * <li>Integer</li>
38  * <li>Long</li>
39  * <li>Short</li>
40  * <li>Double</li>
41  * <li>Float</li>
42  * <li>BigDecimal</li>
43  * <li>Date</li>
44  * <li>Timestamp</li>
45  * <li>Time</li>
46  * </ul>
47  * @author Chris Webb <chris.webb@voxsurf.com>
48  * @author Diez Roggisch <diez.roggisch@artnology.com>
49  * @author Iman L. Crawford <icrawford@greatnation.com>
50  * @author Christian Cryder <christianc@granitepeaks.com>
51  * @author Jacob Kjome <hoju@visi.com>
52  * @version %I%, %G%
53  * @since 1.0
54  */

55 public abstract class FormType {
56
57     // String type definition.
58
public static FormType STRING = new FormType() {
59         public Class JavaDoc getFormClass() {
60             return String JavaDoc.class;
61         }
62
63         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
64             return origVal;
65         }
66
67         public Object JavaDoc [] getTypeArray(int size) {
68             return new String JavaDoc [size];
69         }
70     };
71
72     // Boolean type definition.
73
public static FormType BOOLEAN = new FormType() {
74         public Class JavaDoc getFormClass() {
75             return Boolean JavaDoc.class;
76         }
77
78         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
79             // Eliminate the obvious.
80
if (origVal==null)
81                 return null;
82
83             String JavaDoc tval = origVal.trim().toLowerCase();
84             //csc_061902.1 if (tval.equals("on") || tval.equals("yes") || tval.equals("true")) {
85
if (tval.equals("on") || tval.equals("yes") || tval.equals("true") || tval.equals("y")) { //csc_061902.1
86
origVal = "true";
87                 //csc_061902.1 } else if (tval.equals("off") || tval.equals("no") || tval.equals("false")) {
88
} else if (tval.equals("off") || tval.equals("no") || tval.equals("false") || tval.equals("n")) { //csc_061902.1
89
origVal = "false";
90             } else {
91                 throw new ParseException(origVal);
92             }
93             return new Boolean JavaDoc(origVal);
94         }
95
96         public Object JavaDoc [] getTypeArray(int size) {
97             return new Boolean JavaDoc [size];
98         }
99     };
100
101     // Integer type definition.
102
public static FormType INTEGER = new FormType() {
103         public Class JavaDoc getFormClass() {
104             return Integer JavaDoc.class;
105         }
106
107         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
108             // Eliminate the obvious.
109
if (origVal==null)
110                 return null;
111
112             try {
113                 return new Integer JavaDoc(origVal);
114             } catch (NumberFormatException JavaDoc e) {
115                 try {
116                     //this basically handles the case where the user
117
//typed in an integer value like 123.00...we convert
118
//that value to a double, then to an int, and then form
119
//a new double from that. If the values are equal, we
120
//know no roundoff occurred, meaning the decimal places
121
//were all zeros, and we're in business. Otherwise, throw
122
//the exception
123
Double JavaDoc d1 = new Double JavaDoc(origVal);
124                     int d1val = d1.intValue();
125                     Double JavaDoc d2 = new Double JavaDoc(d1val);
126                     if (d1.equals(d2)) {
127                         return new Integer JavaDoc(d1val);
128                     } else {
129                         throw e;
130                     }
131                 } catch (NumberFormatException JavaDoc e2) {
132                     throw new ParseException(e2);
133                 }
134             }
135         }
136         public Object JavaDoc [] getTypeArray(int size) {
137             return new Integer JavaDoc [size];
138         }
139     };
140
141     // Long type definition.
142
public static FormType LONG = new FormType() {
143         public Class JavaDoc getFormClass() {
144             return Long JavaDoc.class;
145         }
146
147         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
148             // Eliminate the obvious.
149
if (origVal==null)
150                 return null;
151
152             // ilc_022702.1_start
153
// check for 0's after decimal place.
154
// val = new Long(origVal);
155
try {
156                 return new Long JavaDoc(origVal);
157             } catch (NumberFormatException JavaDoc e) {
158                 try {
159                     //this basically handles the case where the user
160
//typed in an integer value like 123.00...we convert
161
//that value to a double, then to an int, and then form
162
//a new double from that. If the values are equal, we
163
//know no roundoff occurred, meaning the decimal places
164
//were all zeros, and we're in business. Otherwise, throw
165
//the exception
166
Double JavaDoc d1 = new Double JavaDoc(origVal);
167                     long d1val = d1.longValue();
168                     Double JavaDoc d2 = new Double JavaDoc(d1val);
169                     if (d1.equals(d2)) {
170                         return new Long JavaDoc(d1val);
171                     } else {
172                         throw e;
173                     }
174                 } catch (NumberFormatException JavaDoc e2) {
175                     throw new ParseException(e2);
176                 }
177             }
178             // ilc_022702.1_end
179
}
180
181         public Object JavaDoc [] getTypeArray(int size) {
182             return new Long JavaDoc [size];
183         }
184     };
185
186     // Short type definition.
187
public static FormType SHORT = new FormType() {
188         public Class JavaDoc getFormClass() {
189             return Short JavaDoc.class;
190         }
191
192         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
193             // Eliminate the obvious.
194
if (origVal==null)
195                 return null;
196
197             // ilc_022702.2_start
198
// check for 0's after decimal place.
199
// val = new Short(origVal);
200
try {
201                 return new Short JavaDoc(origVal);
202             } catch (NumberFormatException JavaDoc e) {
203                 try {
204                     //this basically handles the case where the user
205
//typed in an integer value like 123.00...we convert
206
//that value to a double, then to an int, and then form
207
//a new double from that. If the values are equal, we
208
//know no roundoff occurred, meaning the decimal places
209
//were all zeros, and we're in business. Otherwise, throw
210
//the exception
211
Double JavaDoc d1 = new Double JavaDoc(origVal);
212                     short d1val = d1.shortValue();
213                     Double JavaDoc d2 = new Double JavaDoc(d1val);
214                     if (d1.equals(d2)) {
215                         return new Short JavaDoc(d1val);
216                     } else {
217                         throw e;
218                     }
219                 } catch (NumberFormatException JavaDoc e2) {
220                     throw new ParseException(e2);
221                 }
222             }
223             // ilc_022702.2_end
224
}
225         public Object JavaDoc [] getTypeArray(int size) {
226             return new Short JavaDoc [size];
227         }
228     };
229
230     // Double type definition.
231
public static FormType DOUBLE = new FormType() {
232         public Class JavaDoc getFormClass() {
233             return Double JavaDoc.class;
234         }
235
236         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
237             // Eliminate the obvious.
238
if (origVal==null)
239                 return null;
240
241             try {
242                 return new Double JavaDoc(origVal);
243             } catch (NumberFormatException JavaDoc e) {
244                 throw new ParseException(e);
245             }
246         }
247
248         public Object JavaDoc [] getTypeArray(int size) {
249             return new Double JavaDoc [size];
250         }
251     };
252
253     // Float type definition.
254
public static FormType FLOAT = new FormType() {
255         public Class JavaDoc getFormClass() {
256             return Float JavaDoc.class;
257         }
258
259         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
260             // Eliminate the obvious.
261
if (origVal==null)
262                 return null;
263
264             try {
265                 return new Float JavaDoc(origVal);
266             } catch (NumberFormatException JavaDoc e) {
267                 throw new ParseException(e);
268             }
269         }
270
271         public Object JavaDoc [] getTypeArray(int size) {
272             return new Float JavaDoc [size];
273         }
274     };
275
276     // BigDecimal type definition.
277
public static FormType BIG_DECIMAL = new FormType() {
278         public Class JavaDoc getFormClass() {
279             return BigDecimal JavaDoc.class;
280         }
281
282         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
283             //csc_060702.1_start - I changed this to use the String constructor since
284
//the javadocs say that using the Double constructor is unpredicatable. Note
285
//that I also am stripping out the dollar sign if its there (probably still
286
//need to handle other currency symbols, based on locales)
287
/*
288               Object d = DOUBLE.parse(origVal, locale);
289               if (d!=null) {
290               return new BigDecimal(((Double)d).doubleValue());
291               }
292               return null;
293             */

294             //csc_040203.2 - wrapped the logic in a block to catch NumberFormatExceptions...not sure why this hadn't been done originally!
295
try {
296                 String JavaDoc s = origVal.trim();
297                 s = StringUtil.replace(s, "$",""); //strip off $ sign
298
s = StringUtil.replace(s, "£",""); //strip off £ sign
299
s = StringUtil.replace(s, ",",""); //strip off commas
300
if (s.startsWith("(") && s.endsWith(")")) { //if its a debit (ie. in parenthesis), strip off parenthesis and add a - sign
301
s = "-"+s.substring(1,s.length()-1);
302                 }
303                 return new BigDecimal JavaDoc(s);
304             } catch (NumberFormatException JavaDoc e) {
305                 throw new ParseException(e);
306             }
307
308
309             //csc_060702.1_end
310
}
311
312         public Object JavaDoc [] getTypeArray(int size) {
313             return new BigDecimal JavaDoc [size];
314         }
315     };
316
317     // Date type definition.
318
public static FormType DATE = new FormType() {
319         public Class JavaDoc getFormClass() {
320             return java.util.Date JavaDoc.class;
321         }
322
323         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
324             // Eliminate the obvious.
325
if (origVal==null) return null;
326
327             if (locale==null) locale = Locale.getDefault();
328
329             //saw_061203_2 begin - first try to convert from a long value
330
try {
331                 return new Date JavaDoc(Long.parseLong(origVal));
332             } catch (NumberFormatException JavaDoc e0) {
333             //saw_061203_2 end
334
// try all the various forms of date format to cover more bases
335
try {
336                     //dbr_011602.1 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
337
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); //dbr_011602.1
338
df.setLenient(false);
339                     return df.parse(origVal);
340                 } catch (java.text.ParseException JavaDoc e1) {
341                     try {
342                         //dbr_011602.1 DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
343
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); //dbr_011602.1
344
df.setLenient(false);
345                         return df.parse(origVal);
346                     } catch (java.text.ParseException JavaDoc e2) {
347                         try {
348                             //dbr_011602.1 DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
349
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, locale); //dbr_011602.1
350
df.setLenient(false);
351                             return df.parse(origVal);
352                         } catch (java.text.ParseException JavaDoc e3) {
353                             try {
354                                 //dbr_011602.1 DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
355
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale); //dbr_011602.1
356
df.setLenient(false);
357                                 return df.parse(origVal);
358                             } catch (java.text.ParseException JavaDoc e4) {
359                                 throw new ParseException(e4, "Locale is " + locale.getCountry());
360 /*
361 csc - we are NOT going to parse as a sql Date, because this does NOT correctly take locales into account
362                                 try {
363                                     return new Date(java.sql.Date.valueOf(origVal).getTime());
364                                 } catch (IllegalArgumentException e5) {
365                                     throw new ParseException(e5, "Locale is "+locale.getCountry());
366                                 }
367 */

368                             }
369                         }
370                     }
371                     //rtl20010822 - end new
372
}
373             }//saw_061203_2
374
}
375
376         public Object JavaDoc [] getTypeArray(int size) {
377             return new Date JavaDoc[size];
378         }
379     };
380
381     //csc_051503_1 - added
382
// Timestamp type definition.
383
public static FormType TIMESTAMP = new FormType() {
384         public Class JavaDoc getFormClass() {
385             return Timestamp JavaDoc.class;
386         }
387
388         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
389             //saw_061203_2 begin - TIMESTAMP should work much the same way as DATE, only it should
390
//use DateFormat.getDateTimeInstance() instead of getDateInstance().
391
/*
392             Timestamp ts = null;
393             Long l = null;
394
395             //first of all, try and see if we can treat it as a Long value
396             try {
397                 l = (Long) LONG.parse(origVal, locale);
398             } catch (ParseException e) {}
399
400             //if that didn't work, try to convert it as a date
401             if (l==null) {
402                 Date d = (Date) DATE.parse(origVal, locale);
403                 l = new Long(d.getTime());
404             }
405
406             //finally, try creating the timestamp from the long value
407             return new Timestamp(l.longValue());
408 */

409             if (origVal==null) return null;
410             if (locale==null) locale = Locale.getDefault();
411
412             //first try to parse from a long value
413
try {
414                 return new Timestamp JavaDoc(Long.parseLong(origVal));
415             } catch (NumberFormatException JavaDoc e0) {
416                 // try all the various forms of datetime format to cover more bases
417
try {
418                     DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
419                     df.setLenient(false);
420                     return new Timestamp JavaDoc(df.parse(origVal).getTime());
421                 } catch (java.text.ParseException JavaDoc e1) {
422                     try {
423                         DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
424                         df.setLenient(false);
425                         return new Timestamp JavaDoc(df.parse(origVal).getTime());
426                     } catch (java.text.ParseException JavaDoc e2) {
427                         try {
428                             DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
429                             df.setLenient(false);
430                             return new Timestamp JavaDoc(df.parse(origVal).getTime());
431                         } catch (java.text.ParseException JavaDoc e3) {
432                             try {
433                                 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
434                                 df.setLenient(false);
435                                 return new Timestamp JavaDoc(df.parse(origVal).getTime());
436                             } catch (java.text.ParseException JavaDoc e4) {
437                                 //finally try the SQL timestamp format
438
try {
439                                     return Timestamp.valueOf(origVal);
440                                 } catch (IllegalArgumentException JavaDoc e5) {
441                                     throw new ParseException(e5, "Locale is "+locale.getCountry());
442                                 }
443                             }
444                         }
445                     }
446                 }
447             }
448             //saw_061203_2 end
449
}
450
451         public Object JavaDoc [] getTypeArray(int size) {
452             return new Timestamp JavaDoc[size];
453         }
454     };
455
456     /**
457      * Time type definition
458      * @since saw_061203_2
459      */

460     public static FormType TIME = new FormType() {
461         public Class JavaDoc getFormClass() {
462             return Time JavaDoc.class;
463         }
464
465         public Object JavaDoc parse(String JavaDoc origVal, Locale locale) throws ParseException {
466             if (origVal==null) return null;
467             if (locale==null) locale = Locale.getDefault();
468
469             //first try to parse from a long value
470
try {
471                 return new Time JavaDoc(Long.parseLong(origVal));
472             } catch (NumberFormatException JavaDoc e0) {
473                 // try all the various forms of time format to cover more bases
474
try {
475                     DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
476                     df.setLenient(false);
477                     return new Time JavaDoc(df.parse(origVal).getTime());
478                 } catch (java.text.ParseException JavaDoc e1) {
479                     try {
480                         DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
481                         df.setLenient(false);
482                         return new Time JavaDoc(df.parse(origVal).getTime());
483                     } catch (java.text.ParseException JavaDoc e2) {
484                         try {
485                             DateFormat df = DateFormat.getTimeInstance(DateFormat.LONG, locale);
486                             df.setLenient(false);
487                             return new Time JavaDoc(df.parse(origVal).getTime());
488                         } catch (java.text.ParseException JavaDoc e3) {
489                             try {
490                                 DateFormat df = DateFormat.getTimeInstance(DateFormat.FULL, locale);
491                                 df.setLenient(false);
492                                 return new Time JavaDoc(df.parse(origVal).getTime());
493                             } catch (java.text.ParseException JavaDoc e4) {
494                                 //finally try the SQL time format
495
try {
496                                     return Time.valueOf(origVal);
497                                 } catch (IllegalArgumentException JavaDoc e5) {
498                                     throw new ParseException(e5, "Locale is "+locale.getCountry());
499                                 }
500                             }
501                         }
502                     }
503                 }
504             }
505             //saw_061203_2 end
506
}
507
508         public Object JavaDoc [] getTypeArray(int size) {
509             return new Time JavaDoc[size];
510         }
511     };
512
513     /**
514      * Protected constructor to prevent external instantiation. Cannot be
515      * private because we would be unable to call the constructor from a
516      * sub-class.
517      */

518     protected FormType() { }
519
520     /**
521      * Returns the class associated with this particular form type.
522      */

523     public abstract Class JavaDoc getFormClass();
524
525     /**
526      * Parses an object based on the specific form type.
527      */

528     public Object JavaDoc parse(String JavaDoc origVal) throws ParseException {
529         return parse(origVal, null);
530     }
531
532     /**
533      * Parses an object based on the specific form type.
534      */

535     public abstract Object JavaDoc parse(String JavaDoc origVal, Locale loc) throws ParseException;
536
537
538     /** create an array of the FormType's type - if heterogenous types
539      * are returned, an array of Object will be returned.
540      */

541     public abstract Object JavaDoc [] getTypeArray(int size);
542
543     /**
544      * Returns a string representation of this particular formt type.
545      */

546     public String JavaDoc toString() {
547         return this.getFormClass().getName();
548     }
549
550 }
551
Popular Tags