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