View Javadoc

1   /*
2    * $Id: GenericValidator.java 280974 2005-09-15 00:06:59Z niallp $
3    * $Rev: 280974 $
4    * $Date: 2005-09-15 01:06:59 +0100 (Thu, 15 Sep 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  
22  package org.apache.commons.validator;
23  
24  import java.io.Serializable;
25  import java.util.Locale;
26  
27  import org.apache.oro.text.perl.Perl5Util;
28  
29  /***
30   * This class contains basic methods for performing validations.
31   */
32  public class GenericValidator implements Serializable {
33  
34      /***
35       * UrlValidator used in wrapper method.
36       */
37      private static final UrlValidator urlValidator = new UrlValidator();
38  
39      /***
40       * CreditCardValidator used in wrapper method.
41       */
42      private static final CreditCardValidator creditCardValidator =
43          new CreditCardValidator();
44  
45      /***
46       * <p>Checks if the field isn't null and length of the field is greater 
47       * than zero not including whitespace.</p>
48       *
49       * @param value The value validation is being performed on.
50       * @return true if blank or null.
51       */
52      public static boolean isBlankOrNull(String value) {
53          return ((value == null) || (value.trim().length() == 0));
54      }
55  
56      /***
57       * <p>Checks if the value matches the regular expression.</p>
58       *
59       * @param value The value validation is being performed on.
60       * @param regexp The regular expression.
61       * @return true if matches the regular expression.
62       */
63      public static boolean matchRegexp(String value, String regexp) {
64          if (regexp == null || regexp.length() <= 0) {
65              return false;
66          }
67  
68          Perl5Util matcher = new Perl5Util();
69          return matcher.match("/" + regexp + "/", value);
70      }
71  
72      /***
73       * <p>Checks if the value can safely be converted to a byte primitive.</p>
74       *
75       * @param value The value validation is being performed on.
76       * @return true if the value can be converted to a Byte.
77       */
78      public static boolean isByte(String value) {
79          return (GenericTypeValidator.formatByte(value) != null);
80      }
81  
82      /***
83       * <p>Checks if the value can safely be converted to a short primitive.</p>
84       *
85       * @param value The value validation is being performed on.
86       * @return true if the value can be converted to a Short.
87       */
88      public static boolean isShort(String value) {
89          return (GenericTypeValidator.formatShort(value) != null);
90      }
91  
92      /***
93       * <p>Checks if the value can safely be converted to a int primitive.</p>
94       *
95       * @param value The value validation is being performed on.
96       * @return true if the value can be converted to an Integer.
97       */
98      public static boolean isInt(String value) {
99          return (GenericTypeValidator.formatInt(value) != null);
100     }
101 
102     /***
103      * <p>Checks if the value can safely be converted to a long primitive.</p>
104      *
105      * @param value The value validation is being performed on.
106      * @return true if the value can be converted to a Long.
107      */
108     public static boolean isLong(String value) {
109         return (GenericTypeValidator.formatLong(value) != null);
110     }
111 
112     /***
113      * <p>Checks if the value can safely be converted to a float primitive.</p>
114      *
115      * @param value The value validation is being performed on.
116      * @return true if the value can be converted to a Float.
117      */
118     public static boolean isFloat(String value) {
119         return (GenericTypeValidator.formatFloat(value) != null);
120     }
121 
122     /***
123      * <p>Checks if the value can safely be converted to a double primitive.</p>
124      *
125      * @param value The value validation is being performed on.
126      * @return true if the value can be converted to a Double.
127      */
128     public static boolean isDouble(String value) {
129         return (GenericTypeValidator.formatDouble(value) != null);
130     }
131 
132     /***
133      * <p>Checks if the field is a valid date.  The <code>Locale</code> is
134      * used with <code>java.text.DateFormat</code>.  The setLenient method
135      * is set to <code>false</code> for all.</p>
136      *
137      * @param value The value validation is being performed on.
138      * @param locale The locale to use for the date format, defaults to the 
139      * system default if null.
140      * @return true if the value can be converted to a Date.
141      */
142     public static boolean isDate(String value, Locale locale) {
143         return DateValidator.getInstance().isValid(value, locale);
144     }
145 
146     /***
147      * <p>Checks if the field is a valid date.  The pattern is used with
148      * <code>java.text.SimpleDateFormat</code>.  If strict is true, then the
149      * length will be checked so '2/12/1999' will not pass validation with
150      * the format 'MM/dd/yyyy' because the month isn't two digits.
151      * The setLenient method is set to <code>false</code> for all.</p>
152      *
153      * @param value The value validation is being performed on.
154      * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
155      * @param strict Whether or not to have an exact match of the datePattern.
156      * @return true if the value can be converted to a Date.
157      */
158     public static boolean isDate(String value, String datePattern, boolean strict) {
159         return DateValidator.getInstance().isValid(value, datePattern, strict);
160     }
161 
162     /***
163     * <p>Checks if a value is within a range (min &amp; max specified
164     * in the vars attribute).</p>
165     *
166     * @param value The value validation is being performed on.
167     * @param min The minimum value of the range.
168     * @param max The maximum value of the range.
169      * @return true if the value is in the specified range.
170     */
171     public static boolean isInRange(byte value, byte min, byte max) {
172         return ((value >= min) && (value <= max));
173     }
174 
175     /***
176      * <p>Checks if a value is within a range (min &amp; max specified
177      * in the vars attribute).</p>
178      *
179      * @param value The value validation is being performed on.
180      * @param min The minimum value of the range.
181      * @param max The maximum value of the range.
182      * @return true if the value is in the specified range.
183      */
184     public static boolean isInRange(int value, int min, int max) {
185         return ((value >= min) && (value <= max));
186     }
187 
188     /***
189      * <p>Checks if a value is within a range (min &amp; max specified
190      * in the vars attribute).</p>
191      *
192      * @param value The value validation is being performed on.
193      * @param min The minimum value of the range.
194      * @param max The maximum value of the range.
195      * @return true if the value is in the specified range.
196      */
197     public static boolean isInRange(float value, float min, float max) {
198         return ((value >= min) && (value <= max));
199     }
200 
201     /***
202      * <p>Checks if a value is within a range (min &amp; max specified
203      * in the vars attribute).</p>
204      *
205      * @param value The value validation is being performed on.
206      * @param min The minimum value of the range.
207      * @param max The maximum value of the range.
208      * @return true if the value is in the specified range.
209      */
210     public static boolean isInRange(short value, short min, short max) {
211         return ((value >= min) && (value <= max));
212     }
213 
214     /***
215      * <p>Checks if a value is within a range (min &amp; max specified
216      * in the vars attribute).</p>
217      *
218      * @param value The value validation is being performed on.
219      * @param min The minimum value of the range.
220      * @param max The maximum value of the range.
221      * @return true if the value is in the specified range.
222      */
223     public static boolean isInRange(long value, long min, long max) {
224         return ((value >= min) && (value <= max));
225     }
226 
227     /***
228      * <p>Checks if a value is within a range (min &amp; max specified
229      * in the vars attribute).</p>
230      *
231      * @param value The value validation is being performed on.
232      * @param min The minimum value of the range.
233      * @param max The maximum value of the range.
234      * @return true if the value is in the specified range.
235      */
236     public static boolean isInRange(double value, double min, double max) {
237         return ((value >= min) && (value <= max));
238     }
239 
240     /***
241      * Checks if the field is a valid credit card number.
242      * @param value The value validation is being performed on.
243      * @return true if the value is valid Credit Card Number.
244      */
245     public static boolean isCreditCard(String value) {
246         return creditCardValidator.isValid(value);
247     }
248 
249     /***
250      * <p>Checks if a field has a valid e-mail address.</p>
251      *
252      * @param value The value validation is being performed on.
253      * @return true if the value is valid Email Address.
254      */
255     public static boolean isEmail(String value) {
256         return EmailValidator.getInstance().isValid(value);
257     }
258 
259     /***
260      * <p>Checks if a field is a valid url address.</p>
261      * If you need to modify what is considered valid then
262      * consider using the UrlValidator directly.
263      *
264      * @param value The value validation is being performed on.
265      * @return true if the value is valid Url.
266      */
267     public static boolean isUrl(String value) {
268         return urlValidator.isValid(value);
269     }
270 
271     /***
272      * <p>Checks if the value's length is less than or equal to the max.</p>
273      *
274      * @param value The value validation is being performed on.
275      * @param max The maximum length.
276      * @return true if the value's length is less than the specified maximum.
277      */
278     public static boolean maxLength(String value, int max) {
279         return (value.length() <= max);
280     }
281 
282     /***
283      * <p>Checks if the value's length is greater than or equal to the min.</p>
284      *
285      * @param value The value validation is being performed on.
286      * @param min The minimum length.
287      * @return true if the value's length is more than the specified minimum.
288      */
289     public static boolean minLength(String value, int min) {
290         return (value.length() >= min);
291     }
292     
293     // See http://issues.apache.org/bugzilla/show_bug.cgi?id=29015 WRT the "value" methods
294 
295     /***
296      * <p>Checks if the value is greater than or equal to the min.</p>
297      *
298      * @param value The value validation is being performed on.
299      * @param min The minimum numeric value.
300      * @return true if the value is &gt;= the specified minimum.
301      */
302     public static boolean minValue(int value, int min) {
303         return (value >= min);
304     }
305 
306     /***
307      * <p>Checks if the value is greater than or equal to the min.</p>
308      *
309      * @param value The value validation is being performed on.
310      * @param min The minimum numeric value.
311      * @return true if the value is &gt;= the specified minimum.
312      */
313     public static boolean minValue(long value, long min) {
314         return (value >= min);
315     }
316 
317     /***
318      * <p>Checks if the value is greater than or equal to the min.</p>
319      *
320      * @param value The value validation is being performed on.
321      * @param min The minimum numeric value.
322      * @return true if the value is &gt;= the specified minimum.
323      */
324     public static boolean minValue(double value, double min) {
325         return (value >= min);
326     }
327 
328     /***
329      * <p>Checks if the value is greater than or equal to the min.</p>
330      *
331      * @param value The value validation is being performed on.
332      * @param min The minimum numeric value.
333      * @return true if the value is &gt;= the specified minimum.
334      */
335     public static boolean minValue(float value, float min) {
336         return (value >= min);
337     }
338 
339     /***
340      * <p>Checks if the value is less than or equal to the max.</p>
341      *
342      * @param value The value validation is being performed on.
343      * @param max The maximum numeric value.
344      * @return true if the value is &lt;= the specified maximum.
345      */
346     public static boolean maxValue(int value, int max) {
347         return (value <= max);
348     }
349 
350     /***
351      * <p>Checks if the value is less than or equal to the max.</p>
352      *
353      * @param value The value validation is being performed on.
354      * @param max The maximum numeric value.
355      * @return true if the value is &lt;= the specified maximum.
356      */
357     public static boolean maxValue(long value, long max) {
358         return (value <= max);
359     }
360 
361     /***
362      * <p>Checks if the value is less than or equal to the max.</p>
363      *
364      * @param value The value validation is being performed on.
365      * @param max The maximum numeric value.
366      * @return true if the value is &lt;= the specified maximum.
367      */
368     public static boolean maxValue(double value, double max) {
369         return (value <= max);
370     }
371 
372     /***
373      * <p>Checks if the value is less than or equal to the max.</p>
374      *
375      * @param value The value validation is being performed on.
376      * @param max The maximum numeric value.
377      * @return true if the value is &lt;= the specified maximum.
378      */
379     public static boolean maxValue(float value, float max) {
380         return (value <= max);
381     }
382 
383 }