Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/griffin/validation/validators/simple/SimpleValidation.java


1   /*
2    * SimpleValidation.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.griffin.validation.validators.simple;
11  
12  import java.util.Date;
13  import java.text.SimpleDateFormat;
14  import java.text.MessageFormat;
15  
16  import java.util.*;
17  
18  import com.aendvari.common.util.Debug;
19  
20  
21  /**
22   * Several utility functions for validation field values.
23   *
24   * @author  Trevor Milne
25   * @author  Scott Milne
26   *
27   */
28  
29  public class SimpleValidation
30  {
31    /**
32     * Checks that the value is a non-empty string.
33     *
34     * @param    value            The value to check.
35     *
36     */
37  
38    public static boolean isEmptyString(String value)
39    {
40      // check for null string
41      if (value == null) return true;
42  
43      // trim any leading/trailing whitespace, then check for empty string
44      return (value.trim().equals(""));
45    }
46  
47    /**
48     * Checks that the value is a valid string. A valid string has at most 'max' characters.
49     * Whether empty strings are considered valid is specified by 'allowEmpty'.
50     *
51     * @param    value            The value to validate.
52     * @param    max              The maximum length of the string.
53     * @param    allowEmpty          True if empty strings are valid, false if not.
54     *
55     */
56  
57    public static boolean isValidString(String value, int max, boolean allowEmpty)
58    {
59      // check for null value
60      if (value == null)
61      {
62        if (allowEmpty)
63        {
64          return true;
65        }
66        else
67        {
68          return false;
69        }
70      }
71  
72      // trim any leading/trailing whitespace
73      String trim = value.trim();
74  
75      // accept empty strings if specified
76      if (trim.length() == 0)
77      {
78        if (allowEmpty)
79        {
80          return true;
81        }
82        else
83        {
84          return false;
85        }
86      }
87  
88      // check length of string
89      if (trim.length() > max)
90      {
91        return false;
92      }
93  
94      return true;
95    }
96  
97    /**
98     * Checks that the value is a valid string. A valid string has at least 'min'
99     * characters and at most 'max' characters. Whether empty strings are considered
100    * valid is specified by 'allowEmpty'.
101    *
102    * @param    value            The value to validate.
103    * @param    min              The minimum length of the string.
104    * @param    max              The maximum length of the string.
105    * @param    allowEmpty          True if empty strings are valid, false if not.
106    *
107    */
108 
109   public static boolean isValidString(String value, int min, int max, boolean allowEmpty)
110   {
111     // check for null value
112     if (value == null)
113     {
114       if (allowEmpty)
115       {
116         return true;
117       }
118       else
119       {
120         return false;
121       }
122     }
123 
124     // trim any leading/trailing whitespace
125     String trim = value.trim();
126 
127     // accept empty strings if specified
128     if (trim.length() == 0)
129     {
130       if (allowEmpty)
131       {
132         return true;
133       }
134       else
135       {
136         return false;
137       }
138     }
139 
140     // check length of string
141     if (trim.length() < min)
142     {
143       return false;
144     }
145 
146     if (trim.length() > max)
147     {
148       return false;
149     }
150 
151     return true;
152   }
153 
154   /**
155    * Checks that the value is a valid email address. A valid email address must be non-empty,
156    * have a '@' and at least one '.'. Whether empty strings are considered valid is specified
157    * by 'allowEmpty'.
158    *
159    * @param    value            The value to validate.
160    * @param    allowEmpty          True if empty strings are valid, false if not.
161    *
162    */
163 
164   public static boolean isValidEmail(String value, boolean allowEmpty)
165   {
166     // check for null value
167     if (value == null)
168     {
169       if (allowEmpty)
170       {
171         return true;
172       }
173       else
174       {
175         return false;
176       }
177     }
178 
179     // trim any leading/trailing whitespace
180     String trim = value.trim();
181 
182     // accept empty strings if specified
183     if (trim.length() == 0)
184     {
185       if (allowEmpty)
186       {
187         return true;
188       }
189       else
190       {
191         return false;
192       }
193     }
194 
195     // check for '@'
196     if (trim.indexOf('@') == -1)
197     {
198       return false;
199     }
200 
201     // check for '.'
202     if (trim.indexOf('.') == -1)
203     {
204       return false;
205     }
206 
207     return true;
208   }
209 
210   /**
211    * Checks that the value is a valid email address. A valid email address must be non-empty,
212    * have a '@', at least one '.', and have a maximum of 'max' characters. Whether empty strings
213    * are considered valid is specified by 'allowEmpty'.
214    *
215    * @param    value            The value to validate.
216    * @param    allowEmpty          True if empty strings are valid, false if not.
217    *
218    */
219 
220   public static boolean isValidEmail(String value, int max, boolean allowEmpty)
221   {
222     // check for null value
223     if (value == null)
224     {
225       if (allowEmpty)
226       {
227         return true;
228       }
229       else
230       {
231         return false;
232       }
233     }
234 
235     // trim any leading/trailing whitespace
236     String trim = value.trim();
237 
238     // check length
239     if (trim.length() > max)
240     {
241       return false;
242     }
243 
244     // validate email address
245     return isValidEmail(value, allowEmpty);
246   }
247 
248   /**
249    * Checks that the value is a valid number.
250    *
251    * @param    value            The value to validate.
252    * @param    precision          The precision of the number.
253    * @param    scale            The scale of the number.
254    * @param    allowEmpty          True if empty strings are valid, false if not.
255    *
256    */
257 
258   public static boolean isValidNumber(String value, int precision, int scale, boolean allowEmpty)
259   {
260     // check for null value
261     if (value == null)
262     {
263       if (allowEmpty)
264       {
265         return true;
266       }
267       else
268       {
269         return false;
270       }
271     }
272 
273     // trim any leading/trailing whitespace
274     String trim = value.trim();
275 
276     // accept empty strings if specified
277     if (trim.length() == 0)
278     {
279       if (allowEmpty)
280       {
281         return true;
282       }
283       else
284       {
285         return false;
286       }
287     }
288 
289     // check for valid number
290     try
291     {
292       Double.parseDouble(trim);
293     }
294     catch (NumberFormatException exception)
295     {
296       return false;
297     }
298 
299     // determine decimal place
300     int decimalPlace = trim.indexOf(".");
301 
302     // no decimal place was given
303     if( decimalPlace == -1 )
304     {
305       // check the number of digits
306       if (trim.length() > (precision - scale))
307       {
308         return false;
309       }
310 
311       return true;
312     }
313 
314     // check for multiple decimal places
315     if (trim.lastIndexOf(".") != decimalPlace)
316     {
317       return false;
318     }
319 
320     // check number of digits before decimal place
321     if (decimalPlace > (precision - scale))
322     {
323       return false;
324     }
325 
326     // check number of digits after decimal place
327     if ((trim.length() - decimalPlace - 1) > scale)
328     {
329       return false;
330     }
331 
332     return true;
333   }
334 
335   /**
336    * Checks that the value is a valid integer.
337    *
338    * @param    value            The value to validate.
339    * @param    allowEmpty          True if empty strings are valid, false if not.
340    *
341    */
342 
343   public static boolean isValidInteger(String value, boolean allowEmpty)
344   {
345     // check for null value
346     if (value == null)
347     {
348       if (allowEmpty)
349       {
350         return true;
351       }
352       else
353       {
354         return false;
355       }
356     }
357 
358     // trim any leading/trailing whitespace
359     String trim = value.trim();
360 
361     // accept empty strings if specified
362     if (trim.length() == 0)
363     {
364       if (allowEmpty)
365       {
366         return true;
367       }
368       else
369       {
370         return false;
371       }
372     }
373 
374     try
375     {
376       Integer.parseInt(value);
377     }
378     catch (NumberFormatException exception)
379     {
380       return false;
381     }
382 
383     return true;
384   }
385 
386   /**
387    * Checks that the value is a valid integer that does not exceed the specified number of digits.
388    *
389    * @param    value            The value to validate.
390    * @param    digits            The maximum number of digits.
391    * @param    allowEmpty          True if empty strings are valid, false if not.
392    *
393    */
394 
395   public static boolean isValidInteger(String value, int digits, boolean allowEmpty)
396   {
397     // check if valid integer
398     if (!isValidInteger(value, allowEmpty))
399     {
400       return false;
401     }
402 
403     // check length
404     if (value.trim().length() > digits)
405     {
406       return false;
407     }
408 
409     return true;
410   }
411 
412   /**
413    * Checks that the value is a valid integer and is within the specified range.
414    *
415    * @param    value                The value to validate.
416    * @param    min                  The minimum acceptable value
417    * @param    max                  The maximum acceptable value
418    * @param    allowEmpty              True if empty strings are valid, false if not.
419    *
420    */
421 
422   public static boolean isIntegerWithinRange(String value, int min, int max, boolean allowEmpty)
423   {
424     // check for null value
425     if (value == null)
426     {
427       if (allowEmpty)
428       {
429         return true;
430       }
431       else
432       {
433         return false;
434       }
435     }
436 
437     // trim any leading/trailing whitespace
438     String trim = value.trim();
439 
440     // accept empty strings if specified
441     if (trim.length() == 0)
442     {
443       if (allowEmpty)
444       {
445         return true;
446       }
447       else
448       {
449         return false;
450       }
451     }
452 
453     int intValue = 0;
454 
455     try
456     {
457       intValue = Integer.parseInt(value);
458     }
459     catch (NumberFormatException exception)
460     {
461       return false;
462     }
463 
464     // check range
465     if ((intValue < min) || (intValue > max))
466     {
467       return false;
468     }
469 
470     return true;
471   }
472 
473   /**
474    * Checks that the value is a valid date (mm/dd/yyyy).
475    *
476    * @param    value            The value to validate.
477    * @param    allowEmpty          True if empty strings are valid, false if not.
478    *
479    */
480 
481   public static boolean isValidDateMMDDYYYY(String value, boolean allowEmpty)
482   {
483     // check for null value
484     if (value == null)
485     {
486       if (allowEmpty)
487       {
488         return true;
489       }
490       else
491       {
492         return false;
493       }
494     }
495 
496     // trim any leading/trailing whitespace
497     String trim = value.trim();
498 
499     // accept empty strings if specified
500     if (trim.length() == 0)
501     {
502       if (allowEmpty)
503       {
504         return true;
505       }
506       else
507       {
508         return false;
509       }
510     }
511 
512     // check date format
513     SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
514     formatter.setLenient(false);
515 
516     try
517     {
518       // convert from string to Date
519       Date nativeDate = formatter.parse(value);
520 
521       // if "valid" then check (4 digit) year
522       int last = value.lastIndexOf("/");
523 
524       if ((value.length() - last) < 4)
525         return false;
526     }
527     catch (java.text.ParseException exception)
528     {
529       return false;
530     }
531 
532     return true;
533   }
534 
535   /**
536    * Checks that the value is a valid date (dd/mm/yy).
537    *
538    * @param    value            The value to validate.
539    * @param    allowEmpty          True if empty strings are valid, false if not.
540    *
541    */
542 
543   public static boolean isValidDateDDMMYY(String value, boolean allowEmpty)
544   {
545     // check for null value
546     if (value == null)
547     {
548       if (allowEmpty)
549       {
550         return true;
551       }
552       else
553       {
554         return false;
555       }
556     }
557 
558     // trim any leading/trailing whitespace
559     String trim = value.trim();
560 
561     // accept empty strings if specified
562     if (trim.length() == 0)
563     {
564       if (allowEmpty)
565       {
566         return true;
567       }
568       else
569       {
570         return false;
571       }
572     }
573 
574     // check date format
575     SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");
576     formatter.setLenient(false);
577 
578     try
579     {
580       // convert from string to Date
581       Date nativeDate = formatter.parse(value);
582 
583       // if "valid" then check (2 digit) year
584       int last = value.lastIndexOf("/");
585 
586       if ((value.length() - last) < 2)
587         return false;
588     }
589     catch (java.text.ParseException exception)
590     {
591       return false;
592     }
593 
594     return true;
595   }
596 
597   /**
598    * Checks that the value is a valid date (yyyy/mm/dd).
599    *
600    * @param    value            The value to validate.
601    * @param    allowEmpty          True if empty strings are valid, false if not.
602    *
603    */
604 
605   public static boolean isValidDateYYYYMMDD(String value, boolean allowEmpty)
606   {
607     // check for null value
608     if (value == null)
609     {
610       if (allowEmpty)
611       {
612         return true;
613       }
614       else
615       {
616         return false;
617       }
618     }
619 
620     // trim any leading/trailing whitespace
621     String trim = value.trim();
622 
623     // accept empty strings if specified
624     if (trim.length() == 0)
625     {
626       if (allowEmpty)
627       {
628         return true;
629       }
630       else
631       {
632         return false;
633       }
634     }
635 
636     // check date format
637     SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
638     formatter.setLenient(false);
639 
640     try
641     {
642       // convert from string to Date
643       Date nativeDate = formatter.parse(value);
644 
645       // if "valid" then check (4 digit) year
646       int first = value.indexOf("/");
647 
648       if (first > 4)
649         return false;
650     }
651     catch (java.text.ParseException exception)
652     {
653       return false;
654     }
655 
656     return true;
657   }
658 }
659