KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: GenericValidator.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 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 JavaDoc;
25 import java.util.Locale JavaDoc;
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 JavaDoc {
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      */

51     public static boolean isBlankOrNull(String JavaDoc value) {
52         return ((value == null) || (value.trim().length() == 0));
53     }
54
55     /**
56      * <p>Checks if the value matches the regular expression.</p>
57      *
58      * @param value The value validation is being performed on.
59      * @param regexp The regular expression.
60      */

61     public static boolean matchRegexp(String JavaDoc value, String JavaDoc regexp) {
62         if (regexp == null || regexp.length() <= 0) {
63             return false;
64         }
65
66         Perl5Util matcher = new Perl5Util();
67         return matcher.match("/" + regexp + "/", value);
68     }
69
70     /**
71      * <p>Checks if the value can safely be converted to a byte primitive.</p>
72      *
73      * @param value The value validation is being performed on.
74      */

75     public static boolean isByte(String JavaDoc value) {
76         return (GenericTypeValidator.formatByte(value) != null);
77     }
78
79     /**
80      * <p>Checks if the value can safely be converted to a short primitive.</p>
81      *
82      * @param value The value validation is being performed on.
83      */

84     public static boolean isShort(String JavaDoc value) {
85         return (GenericTypeValidator.formatShort(value) != null);
86     }
87
88     /**
89      * <p>Checks if the value can safely be converted to a int primitive.</p>
90      *
91      * @param value The value validation is being performed on.
92      */

93     public static boolean isInt(String JavaDoc value) {
94         return (GenericTypeValidator.formatInt(value) != null);
95     }
96
97     /**
98      * <p>Checks if the value can safely be converted to a long primitive.</p>
99      *
100      * @param value The value validation is being performed on.
101      */

102     public static boolean isLong(String JavaDoc value) {
103         return (GenericTypeValidator.formatLong(value) != null);
104     }
105
106     /**
107      * <p>Checks if the value can safely be converted to a float primitive.</p>
108      *
109      * @param value The value validation is being performed on.
110      */

111     public static boolean isFloat(String JavaDoc value) {
112         return (GenericTypeValidator.formatFloat(value) != null);
113     }
114
115     /**
116      * <p>Checks if the value can safely be converted to a double primitive.</p>
117      *
118      * @param value The value validation is being performed on.
119      */

120     public static boolean isDouble(String JavaDoc value) {
121         return (GenericTypeValidator.formatDouble(value) != null);
122     }
123
124     /**
125      * <p>Checks if the field is a valid date. The <code>Locale</code> is
126      * used with <code>java.text.DateFormat</code>. The setLenient method
127      * is set to <code>false</code> for all.</p>
128      *
129      * @param value The value validation is being performed on.
130      * @param locale The locale to use for the date format, defaults to the
131      * system default if null.
132      */

133     public static boolean isDate(String JavaDoc value, Locale JavaDoc locale) {
134         return DateValidator.getInstance().isValid(value, locale);
135     }
136
137     /**
138      * <p>Checks if the field is a valid date. The pattern is used with
139      * <code>java.text.SimpleDateFormat</code>. If strict is true, then the
140      * length will be checked so '2/12/1999' will not pass validation with
141      * the format 'MM/dd/yyyy' because the month isn't two digits.
142      * The setLenient method is set to <code>false</code> for all.</p>
143      *
144      * @param value The value validation is being performed on.
145      * @param datePattern The pattern passed to <code>SimpleDateFormat</code>.
146      * @param strict Whether or not to have an exact match of the datePattern.
147      */

148     public static boolean isDate(String JavaDoc value, String JavaDoc datePattern, boolean strict) {
149         return DateValidator.getInstance().isValid(value, datePattern, strict);
150     }
151
152     /**
153     * <p>Checks if a value is within a range (min &amp; max specified
154     * in the vars attribute).</p>
155     *
156     * @param value The value validation is being performed on.
157     * @param min The minimum value of the range.
158     * @param max The maximum value of the range.
159     */

160     public static boolean isInRange(byte value, byte min, byte max) {
161         return ((value >= min) && (value <= max));
162     }
163
164     /**
165      * <p>Checks if a value is within a range (min &amp; max specified
166      * in the vars attribute).</p>
167      *
168      * @param value The value validation is being performed on.
169      * @param min The minimum value of the range.
170      * @param max The maximum value of the range.
171      */

172     public static boolean isInRange(int value, int min, int max) {
173         return ((value >= min) && (value <= max));
174     }
175
176     /**
177      * <p>Checks if a value is within a range (min &amp; max specified
178      * in the vars attribute).</p>
179      *
180      * @param value The value validation is being performed on.
181      * @param min The minimum value of the range.
182      * @param max The maximum value of the range.
183      */

184     public static boolean isInRange(float value, float min, float 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      */

196     public static boolean isInRange(short value, short min, short max) {
197         return ((value >= min) && (value <= max));
198     }
199
200     /**
201      * <p>Checks if a value is within a range (min &amp; max specified
202      * in the vars attribute).</p>
203      *
204      * @param value The value validation is being performed on.
205      * @param min The minimum value of the range.
206      * @param max The maximum value of the range.
207      */

208     public static boolean isInRange(long value, long min, long max) {
209         return ((value >= min) && (value <= max));
210     }
211
212     /**
213      * <p>Checks if a value is within a range (min &amp; max specified
214      * in the vars attribute).</p>
215      *
216      * @param value The value validation is being performed on.
217      * @param min The minimum value of the range.
218      * @param max The maximum value of the range.
219      */

220     public static boolean isInRange(double value, double min, double max) {
221         return ((value >= min) && (value <= max));
222     }
223
224     /**
225      * Checks if the field is a valid credit card number.
226      * @param value The value validation is being performed on.
227      */

228     public static boolean isCreditCard(String JavaDoc value) {
229         return creditCardValidator.isValid(value);
230     }
231
232     /**
233      * <p>Checks if a field has a valid e-mail address.</p>
234      *
235      * @param value The value validation is being performed on.
236      */

237     public static boolean isEmail(String JavaDoc value) {
238         return EmailValidator.getInstance().isValid(value);
239     }
240
241     /**
242      * <p>Checks if a field is a valid url address.</p>
243      * If you need to modify what is considered valid then
244      * consider using the UrlValidator directly.
245      *
246      * @param value The value validation is being performed on.
247      */

248     public static boolean isUrl(String JavaDoc value) {
249         return urlValidator.isValid(value);
250     }
251
252     /**
253      * <p>Checks if the value's length is less than or equal to the max.</p>
254      *
255      * @param value The value validation is being performed on.
256      * @param max The maximum length.
257      */

258     public static boolean maxLength(String JavaDoc value, int max) {
259         return (value.length() <= max);
260     }
261
262     /**
263      * <p>Checks if the value's length is greater than or equal to the min.</p>
264      *
265      * @param value The value validation is being performed on.
266      * @param min The minimum length.
267      */

268     public static boolean minLength(String JavaDoc value, int min) {
269         return (value.length() >= min);
270     }
271     
272     // See http://issues.apache.org/bugzilla/show_bug.cgi?id=29015 WRT the "value" methods
273

274     /**
275      * <p>Checks if the value is greater than or equal to the min.</p>
276      *
277      * @param value The value validation is being performed on.
278      * @param min The minimum numeric value.
279      */

280     public static boolean minValue(int value, int min) {
281         return (value >= min);
282     }
283
284     /**
285      * <p>Checks if the value is greater than or equal to the min.</p>
286      *
287      * @param value The value validation is being performed on.
288      * @param min The minimum numeric value.
289      */

290     public static boolean minValue(long value, long min) {
291         return (value >= min);
292     }
293
294     /**
295      * <p>Checks if the value is greater than or equal to the min.</p>
296      *
297      * @param value The value validation is being performed on.
298      * @param min The minimum numeric value.
299      */

300     public static boolean minValue(double value, double min) {
301         return (value >= min);
302     }
303
304     /**
305      * <p>Checks if the value is greater than or equal to the min.</p>
306      *
307      * @param value The value validation is being performed on.
308      * @param min The minimum numeric value.
309      */

310     public static boolean minValue(float value, float min) {
311         return (value >= min);
312     }
313
314     /**
315      * <p>Checks if the value is less than or equal to the max.</p>
316      *
317      * @param value The value validation is being performed on.
318      * @param max The maximum numeric value.
319      */

320     public static boolean maxValue(int value, int max) {
321         return (value <= max);
322     }
323
324     /**
325      * <p>Checks if the value is less than or equal to the max.</p>
326      *
327      * @param value The value validation is being performed on.
328      * @param max The maximum numeric value.
329      */

330     public static boolean maxValue(long value, long max) {
331         return (value <= max);
332     }
333
334     /**
335      * <p>Checks if the value is less than or equal to the max.</p>
336      *
337      * @param value The value validation is being performed on.
338      * @param max The maximum numeric value.
339      */

340     public static boolean maxValue(double value, double max) {
341         return (value <= max);
342     }
343
344     /**
345      * <p>Checks if the value is less than or equal to the max.</p>
346      *
347      * @param value The value validation is being performed on.
348      * @param max The maximum numeric value.
349      */

350     public static boolean maxValue(float value, float max) {
351         return (value <= max);
352     }
353
354 }
355
Popular Tags