KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typevalidator > ZipcodeTypeValidator


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.typevalidator;
8
9
10 import java.util.Locale JavaDoc;
11
12
13 /**
14  * <p>
15  * This class does validation of zipcodes.
16  * </p>
17  *
18  * @author bpontarelli
19  * @version 2.0
20  * @since 2.0
21  */

22 public class ZipcodeTypeValidator extends BaseTypeValidator {
23
24     /**
25      * Default error message returned from validate methods (inside Basic and
26      * PropertyError objects)
27      */

28     public static final String JavaDoc DEFAULT_MESSAGE = "Invalid zip code";
29
30
31     /**
32      * Constructs a new <code>ZipcodeTypeValidator</code>
33      */

34     public ZipcodeTypeValidator() {
35         // Empty
36
}
37
38
39     /**
40      * Does the work of validating the zipcode. This validates zipcode in multiple
41      * locales by calling the various other internalValidate methods
42      */

43     protected String JavaDoc internalValidate(Object JavaDoc value, Object JavaDoc params, String JavaDoc message,
44             Locale JavaDoc locale, Object JavaDoc[] mesgParams) {
45         if (locale == null) {
46             locale = Locale.getDefault();
47         }
48
49         String JavaDoc error = null;
50         if (locale.getCountry().equals("US") || locale.getCountry().equals("UM")) {
51             error = internalValidateUS(value, params, message, mesgParams);
52         }
53
54         return error;
55     }
56
57     /**
58      * Does the actual work of validation and error message formatting. This also
59      * checks if the message is null and uses the DEFAULT_MESSAGE
60      */

61     private String JavaDoc internalValidateUS(Object JavaDoc value, Object JavaDoc params, String JavaDoc message,
62             Object JavaDoc [] mesgParams) {
63         String JavaDoc error = null;
64
65         // Do the validation of the zipcode
66
StringBuffer JavaDoc numberBuf = new StringBuffer JavaDoc();
67         String JavaDoc localNumber = value.toString().trim();
68         char ch;
69         boolean valid = true;
70         boolean dashFound = false;
71         for (int i = 0; i < localNumber.length(); i++) {
72             ch = localNumber.charAt(i);
73             if (Character.isDigit(ch)) {
74                 numberBuf.append(ch);
75             } else if (ch == '-') {
76                 numberBuf.append(ch);
77                 if (dashFound) {
78                     valid = false;
79                     break;
80                 }
81                 dashFound = true;
82             } else if (ch != ' ') {
83                 valid = false;
84                 break;
85             }
86         }
87
88         if (valid) {
89             int length = numberBuf.length();
90             int dash = numberBuf.indexOf("-");
91             if (length == 5 || length == 9) {
92                valid = (dash == -1);
93             } else if (length == 10) {
94                 valid = (dash == 5);
95             } else {
96                 valid = false;
97             }
98         }
99
100         if (!valid) {
101             error = getErrorMessage(message, DEFAULT_MESSAGE, mesgParams);
102         }
103         
104         return error;
105     }
106 }
Popular Tags