KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > GenericTypeValidator


1 /*
2  * $Id: GenericTypeValidator.java 164749 2005-04-26 06:15:17Z mrdon $
3  * $Rev$
4  * $Date: 2005-04-25 23:15:17 -0700 (Mon, 25 Apr 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21 package org.apache.commons.validator;
22
23 import java.io.Serializable JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.text.DateFormat JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.text.NumberFormat JavaDoc;
29 import java.text.ParseException JavaDoc;
30 import java.text.ParsePosition JavaDoc;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /**
35  * This class contains basic methods for performing validations that return the
36  * correctly typed class based on the validation performed.
37  */

38 public class GenericTypeValidator implements Serializable JavaDoc {
39
40     private final static Log log = LogFactory.getLog(GenericTypeValidator.class);
41
42     /**
43      * Checks if the value can safely be converted to a byte primitive.
44      *
45      *@param value The value validation is being performed on.
46      *@return
47      */

48     public static Byte JavaDoc formatByte(String JavaDoc value) {
49         if (value == null) {
50             return null;
51         }
52
53         try {
54             return new Byte JavaDoc(value);
55         } catch (NumberFormatException JavaDoc e) {
56             return null;
57         }
58
59     }
60
61     /**
62      * Checks if the value can safely be converted to a byte primitive.
63      *
64      *@param value The value validation is being performed on.
65      *@param locale The locale to use to parse the number (system default if
66      * null)
67      *@return
68      */

69     public static Byte JavaDoc formatByte(String JavaDoc value, Locale JavaDoc locale) {
70         Byte JavaDoc result = null;
71
72         if (value != null) {
73             NumberFormat JavaDoc formatter = null;
74             if (locale != null) {
75                 formatter = NumberFormat.getIntegerInstance(locale);
76             } else {
77                 formatter = NumberFormat.getIntegerInstance(Locale.getDefault());
78             }
79             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
80             Number JavaDoc num = formatter.parse(value, pos);
81
82             // If there was no error and we used the whole string
83
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 JavaDoc(num.byteValue());
87                 }
88             }
89         }
90
91         return result;
92     }
93
94     /**
95      * Checks if the value can safely be converted to a short primitive.
96      *
97      *@param value The value validation is being performed on.
98      *@return
99      */

100     public static Short JavaDoc formatShort(String JavaDoc value) {
101         if (value == null) {
102             return null;
103         }
104
105         try {
106             return new Short JavaDoc(value);
107         } catch (NumberFormatException JavaDoc e) {
108             return null;
109         }
110
111     }
112
113     /**
114      * Checks if the value can safely be converted to a short primitive.
115      *
116      *@param value The value validation is being performed on.
117      *@param locale The locale to use to parse the number (system default if
118      * null)vv
119      *@return
120      */

121     public static Short JavaDoc formatShort(String JavaDoc value, Locale JavaDoc locale) {
122         Short JavaDoc result = null;
123
124         if (value != null) {
125             NumberFormat JavaDoc formatter = null;
126             if (locale != null) {
127                 formatter = NumberFormat.getIntegerInstance(locale);
128             } else {
129                 formatter = NumberFormat.getIntegerInstance(Locale.getDefault());
130             }
131             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
132             Number JavaDoc num = formatter.parse(value, pos);
133
134             // If there was no error and we used the whole string
135
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 JavaDoc(num.shortValue());
139                 }
140             }
141         }
142
143         return result;
144     }
145
146     /**
147      * Checks if the value can safely be converted to a int primitive.
148      *
149      *@param value The value validation is being performed on.
150      *@return
151      */

152     public static Integer JavaDoc formatInt(String JavaDoc value) {
153         if (value == null) {
154             return null;
155         }
156
157         try {
158             return new Integer JavaDoc(value);
159         } catch (NumberFormatException JavaDoc e) {
160             return null;
161         }
162
163     }
164
165     /**
166      * Checks if the value can safely be converted to an int primitive.
167      *
168      *@param value The value validation is being performed on.
169      *@param locale The locale to use to parse the number (system default if
170      * null)
171      *@return
172      */

173     public static Integer JavaDoc formatInt(String JavaDoc value, Locale JavaDoc locale) {
174         Integer JavaDoc result = null;
175
176         if (value != null) {
177             NumberFormat JavaDoc formatter = null;
178             if (locale != null) {
179                 formatter = NumberFormat.getIntegerInstance(locale);
180             } else {
181                 formatter = NumberFormat.getIntegerInstance(Locale.getDefault());
182             }
183             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
184             Number JavaDoc num = formatter.parse(value, pos);
185
186             // If there was no error and we used the whole string
187
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 JavaDoc(num.intValue());
191                 }
192             }
193         }
194
195         return result;
196     }
197
198     /**
199      * Checks if the value can safely be converted to a long primitive.
200      *
201      *@param value The value validation is being performed on.
202      *@return
203      */

204     public static Long JavaDoc formatLong(String JavaDoc value) {
205         if (value == null) {
206             return null;
207         }
208
209         try {
210             return new Long JavaDoc(value);
211         } catch (NumberFormatException JavaDoc e) {
212             return null;
213         }
214
215     }
216
217     /**
218      * Checks if the value can safely be converted to a long primitive.
219      *
220      *@param value The value validation is being performed on.
221      *@param locale The locale to use to parse the number (system default if
222      * null)
223      *@return
224      */

225     public static Long JavaDoc formatLong(String JavaDoc value, Locale JavaDoc locale) {
226         Long JavaDoc result = null;
227
228         if (value != null) {
229             NumberFormat JavaDoc formatter = null;
230             if (locale != null) {
231                 formatter = NumberFormat.getIntegerInstance(locale);
232             } else {
233                 formatter = NumberFormat.getIntegerInstance(Locale.getDefault());
234             }
235             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
236             Number JavaDoc num = formatter.parse(value, pos);
237
238             // If there was no error and we used the whole string
239
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 JavaDoc(num.longValue());
243                 }
244             }
245         }
246
247         return result;
248     }
249
250     /**
251      * Checks if the value can safely be converted to a float primitive.
252      *
253      *@param value The value validation is being performed on.
254      *@return
255      */

256     public static Float JavaDoc formatFloat(String JavaDoc value) {
257         if (value == null) {
258             return null;
259         }
260
261         try {
262             return new Float JavaDoc(value);
263         } catch (NumberFormatException JavaDoc e) {
264             return null;
265         }
266
267     }
268
269     /**
270      * Checks if the value can safely be converted to a float primitive.
271      *
272      *@param value The value validation is being performed on.
273      *@param locale The locale to use to parse the number (system default if
274      * null)
275      *@return
276      */

277     public static Float JavaDoc formatFloat(String JavaDoc value, Locale JavaDoc locale) {
278         Float JavaDoc result = null;
279
280         if (value != null) {
281             NumberFormat JavaDoc formatter = null;
282             if (locale != null) {
283                 formatter = NumberFormat.getInstance(locale);
284             } else {
285                 formatter = NumberFormat.getInstance(Locale.getDefault());
286             }
287             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
288             Number JavaDoc num = formatter.parse(value, pos);
289
290             // If there was no error and we used the whole string
291
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 JavaDoc(num.floatValue());
295                 }
296             }
297         }
298
299         return result;
300     }
301
302     /**
303      * Checks if the value can safely be converted to a double primitive.
304      *
305      *@param value The value validation is being performed on.
306      *@return
307      */

308     public static Double JavaDoc formatDouble(String JavaDoc value) {
309         if (value == null) {
310             return null;
311         }
312
313         try {
314             return new Double JavaDoc(value);
315         } catch (NumberFormatException JavaDoc e) {
316             return null;
317         }
318
319     }
320
321     /**
322      * Checks if the value can safely be converted to a double primitive.
323      *
324      *@param value The value validation is being performed on.
325      *@param locale The locale to use to parse the number (system default if
326      * null)vvv
327      *@return
328      */

329     public static Double JavaDoc formatDouble(String JavaDoc value, Locale JavaDoc locale) {
330         Double JavaDoc result = null;
331
332         if (value != null) {
333             NumberFormat JavaDoc formatter = null;
334             if (locale != null) {
335                 formatter = NumberFormat.getInstance(locale);
336             } else {
337                 formatter = NumberFormat.getInstance(Locale.getDefault());
338             }
339             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
340             Number JavaDoc num = formatter.parse(value, pos);
341
342             // If there was no error and we used the whole string
343
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 JavaDoc(num.doubleValue());
347                 }
348             }
349         }
350
351         return result;
352     }
353
354     /**
355      * <p>
356      *
357      * Checks if the field is a valid date. The <code>Locale</code> is used
358      * with <code>java.text.DateFormat</code>. The setLenient method is set to
359      * <code>false</code> for all.</p>
360      *
361      *@param value The value validation is being performed on.
362      *@param locale The Locale to use to parse the date (system default if
363      * null)
364      *@return
365      */

366     public static Date JavaDoc formatDate(String JavaDoc value, Locale JavaDoc locale) {
367         Date JavaDoc date = null;
368
369         if (value == null) {
370             return null;
371         }
372
373         try {
374             DateFormat JavaDoc 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 JavaDoc e) {
389             // Bad date so return null
390
log.warn(value, e);
391         }
392
393         return date;
394     }
395
396     /**
397      * <p>
398      * Checks if the field is a valid date. The pattern is used with <code>java.text.SimpleDateFormat</code>
399      * . If strict is true, then the length will be checked so '2/12/1999' will
400      * not pass validation with the format 'MM/dd/yyyy' because the month isn't
401      * two digits. The setLenient method is set to <code>false</code> for all.
402      * </p>
403      *
404      *@param value The value validation is being performed on.
405      *@param datePattern The pattern passed to <code>SimpleDateFormat</code>.
406      *@param strict Whether or not to have an exact match of the
407      * datePattern.
408      *@return
409      */

410     public static Date JavaDoc formatDate(String JavaDoc value, String JavaDoc datePattern, boolean strict) {
411         Date JavaDoc date = null;
412
413         if (value == null
414              || datePattern == null
415              || datePattern.length() == 0) {
416             return null;
417         }
418
419         try {
420             SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(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 JavaDoc e) {
431             // Bad date so return null
432
log.warn(value, e);
433         }
434
435         return date;
436     }
437
438     /**
439      * <p>
440      * Checks if the field is a valid credit card number.</p> <p>
441      *
442      * Reference Sean M. Burke's <a HREF="http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl">
443      * script</a> .</p>
444      *
445      *@param value The value validation is being performed on.
446      *@return
447      */

448     public static Long JavaDoc formatCreditCard(String JavaDoc value) {
449         return GenericValidator.isCreditCard(value) ? new Long JavaDoc(value) : null;
450     }
451
452 }
453
454
Popular Tags