View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericTypeValidator.java,v 1.13 2004/02/21 17:10:29 rleland Exp $
3    * $Revision: 1.13 $
4    * $Date: 2004/02/21 17:10:29 $
5    *
6    * ====================================================================
7    * Copyright 2001-2004 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  
22  package org.apache.commons.validator;
23  
24  import java.io.Serializable;
25  import java.util.Date;
26  import java.util.Locale;
27  import java.text.DateFormat;
28  import java.text.SimpleDateFormat;
29  import java.text.ParseException;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /***
34   * This class contains basic methods for performing validations that return the
35   * correctly typed class based on the validation performed.
36   */
37  public class GenericTypeValidator implements Serializable {
38      /*
39      * Logger.
40      */
41     private static Log log = LogFactory.getLog(GenericTypeValidator.class);
42  
43      /***
44       * Checks if the value can safely be converted to a byte primitive.
45       *
46       * @param value The value validation is being performed on.
47       */
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      /***
62       * Checks if the value can safely be converted to a short primitive.
63       *
64       * @param value The value validation is being performed on.
65       */
66      public static Short formatShort(String value) {
67          if (value == null) {
68              return null;
69          }
70  
71          try {
72              return new Short(value);
73          } catch(NumberFormatException e) {
74              return null;
75          }
76  
77      }
78  
79      /***
80       * Checks if the value can safely be converted to a int primitive.
81       *
82       * @param value The value validation is being performed on.
83       */
84      public static Integer formatInt(String value) {
85          if (value == null) {
86              return null;
87          }
88  
89          try {
90              return new Integer(value);
91          } catch(NumberFormatException e) {
92              return null;
93          }
94  
95      }
96  
97      /***
98       * Checks if the value can safely be converted to a long primitive.
99       *
100      * @param value The value validation is being performed on.
101      */
102     public static Long formatLong(String value) {
103         if (value == null) {
104             return null;
105         }
106 
107         try {
108             return new Long(value);
109         } catch(NumberFormatException e) {
110             return null;
111         }
112 
113     }
114 
115     /***
116      * Checks if the value can safely be converted to a float primitive.
117      *
118      * @param value The value validation is being performed on.
119      */
120     public static Float formatFloat(String value) {
121         if (value == null) {
122             return null;
123         }
124 
125         try {
126             return new Float(value);
127         } catch(NumberFormatException e) {
128             return null;
129         }
130 
131     }
132 
133     /***
134      * Checks if the value can safely be converted to a double primitive.
135      *
136      * @param value The value validation is being performed on.
137      */
138     public static Double formatDouble(String value) {
139         if (value == null) {
140             return null;
141         }
142 
143         try {
144             return new Double(value);
145         } catch(NumberFormatException e) {
146             return null;
147         }
148 
149     }
150 
151     /***
152      * <p>Checks if the field is a valid date.  The <code>Locale</code> is
153      * used with <code>java.text.DateFormat</code>.  The setLenient method
154      * is set to <code>false</code> for all.</p>
155      *
156      * @param value The value validation is being performed on.
157      * @param locale The Locale to use to parse the date (system default if null)
158      */
159     public static Date formatDate(String value, Locale locale) {
160         Date date = null;
161 
162         if (value == null) {
163             return null;
164         }
165 
166         try {
167             DateFormat formatter = null;
168             if (locale != null) {
169                 formatter =
170                         DateFormat.getDateInstance(DateFormat.SHORT, locale);
171             } else {
172                 formatter =
173                         DateFormat.getDateInstance(
174                                 DateFormat.SHORT,
175                                 Locale.getDefault());
176             }
177 
178             formatter.setLenient(false);
179 
180             date = formatter.parse(value);
181         } catch(ParseException e) {
182             // Bad date so return null
183             log.warn(value, e);
184         }
185 
186         return date;
187     }
188 
189     /***
190      * <p>Checks if the field is a valid date.  The pattern is used with
191      * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
192      * length will be checked so '2/12/1999' will not pass validation with
193      * the format 'MM/dd/yyyy' because the month isn't two digits.
194      * The setLenient method is set to <code>false</code> for all.</p>
195      *
196      * @param value The value validation is being performed on.
197      * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
198      * @param strict Whether or not to have an exact match of the datePattern.
199      */
200     public static Date formatDate(String value, String datePattern, boolean strict) {
201         Date date = null;
202 
203         if (value == null
204                 || datePattern == null
205                 || datePattern.length() == 0) {
206             return null;
207         }
208 
209         try {
210             SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
211             formatter.setLenient(false);
212 
213             date = formatter.parse(value);
214 
215             if (strict) {
216                 if (datePattern.length() != value.length()) {
217                     date = null;
218                 }
219             }
220         } catch(ParseException e) {
221             // Bad date so return null
222             log.warn(value, e);
223         }
224 
225         return date;
226     }
227 
228     /***
229      * <p>Checks if the field is a valid credit card number.</p>
230      * <p>Reference Sean M. Burke's
231      * <a href="http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl">script</a>.</p>
232      *
233      * @param value The value validation is being performed on.
234      */
235     public static Long formatCreditCard(String value) {
236         return GenericValidator.isCreditCard(value) ? new Long(value) : null;
237     }
238 
239 }