KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > validators > DigitValidator


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DigitValidator.java,v 1.18 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms.validators;
21
22 import java.util.*;
23
24 import org.enhydra.barracuda.core.forms.*;
25 import org.enhydra.barracuda.plankton.*;
26 import org.apache.log4j.*;
27
28 /**
29  * This validator ensures that the value contains only digits tested against
30  * the field size length (len). If the value's length is not equal to the length
31  * or a character other than the numbers 0-9 is found, an exception will be throw.
32  *
33  * <p>This class can be used 3 ways
34  * <p>1) As a digit validator, confirming that only digits exist for the value. The field value
35  * "is not required", and will only be tested for digits when the field value "is not null".
36  * Use default constructor() or constructor(errorMessage) for this case.
37  * <p>2) As a digit/length validator where phoneNumber will default to false. This can be used
38  * to test set lengths of digits (credit cards, quantities, soc sec numbers etc.) The field value "is required".
39  * Use constructor(len) for this case.
40  * <p>3) As a phone number validator (domestic/international) where certain phone number rules apply. Use
41  * constructor(len, isDomesticPhone, errMessage) for this case.
42  *
43  * <p>Note: There is no current support for '.' or '-' in the value for phone numbers.
44  *
45  * <p>This class works very nicely where the input fields are separated into their respective
46  * types (i.e Separating a Domestic Phone Number into 3 'input type=text' boxes -- Area Code len=3,
47  * Prefix len = 3, and Number len =4) will permit easier implementation of the rules as more are
48  * added.
49  *
50  * <p><p>Rules:<br>
51  * --domesticPhone = true<br>
52  * if length = 3, then the value may not start with a zero (0)
53  *
54  * <br>
55  * --domesticPhone = false (international)<br>
56  * none
57  *
58  * @author Paul G. Markovich <pmarkovich@acpinteractive.com>
59  * @author Christian Cryder [christianc@granitepeaks.com]
60  * @author Jacob Kjome <hoju@visi.com>
61  * @author Bill Wallace <Bill_Wallace@elementk.com>
62  * @author Diez B. Roggisch <diez.roggisch@artnology.com>
63  * @version %I%, %G%
64  * @since 1.0
65  */

66 public class DigitValidator extends DefaultFormValidator {
67
68     protected int len = 0;
69     protected boolean requireLength = false;
70     protected boolean phoneNumber = false;
71     protected boolean domesticPhone = false;
72
73     /**
74      * Public constructor.
75      */

76     public DigitValidator() {
77         this(null);
78     }
79
80     /**
81      * Public constructor.
82      *
83      * @param ierrmsg the message associated with this validation error
84      */

85     public DigitValidator(String JavaDoc ierrmsg) {
86         super(ierrmsg);
87     }
88
89     /**
90      * Public constructor.
91      *
92      * Utilizing this constructor, the field value will be required
93      *
94      * @param ilen the length of the field to validate all digits
95      */

96     public DigitValidator(int ilen) {
97         //pgm changed from: this(ilen,false,null);
98
this(ilen, null);
99         requireLength = true;
100     }
101
102     /**
103      * Public constructor.
104      *
105      * @param ilen the length of the field to validate all digits
106      * @param ierrmsg the message associated with this validation error
107      */

108     public DigitValidator(int ilen, String JavaDoc ierrmsg) {
109         //pgm changed from: this(ilen,false,errmsg);
110
super(ierrmsg);
111         len = ilen;
112         requireLength = true;
113     }
114
115     /**
116      * Public constructor.
117      *
118      * @param ilen the length of the field to validate all digits
119      * @param idomesticPhone when true will validate based on domestic
120      * rules. At this time the only rule is for a length
121      * of 3 (area codes/prefixes) that the value can't start with 0
122      * @param ierrmsg the message associated with this validation error
123      */

124     public DigitValidator(int ilen, boolean idomesticPhone, String JavaDoc ierrmsg) {
125         super(ierrmsg);
126         len = ilen;
127         domesticPhone = idomesticPhone;
128         phoneNumber = true;
129         requireLength = true;
130     }
131
132     //bw_102501.1 - added
133
/**
134      * Return true if this must be a domestic phone#
135      *
136      * @return boolean true if domestic phone#
137      */

138     public boolean isDomesticPhone() {
139        return domesticPhone;
140     }
141
142     /**
143      * Validate a FormElement to make see if the element equals() a
144      * given object
145      *
146      * @param val the value to compare the element value to
147      * @param element the form element that contains the val
148      * to validate elements by comparing them with other elements)
149      * @param deferExceptions do we want to deferValidation exceptions
150      * and attempt to validate all elements so that we can process
151      * all the exceptions at once
152      * @throws ValidationException if the element is not valid
153      */

154     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
155         //pgm moved the following code from here to the else condition below
156
//test to see if field value is null, if so no further tests required
157
//if (val==null || val.toString().trim().length()<1) {
158
//if (errmsg==null) errmsg = "Null value fails to meet minimum length of "+len;
159
//throw this.generateException(element, deferExceptions);
160
//}
161

162         // ilc_022502.1_start
163
// use NotNullValidator for nulls
164
if (this.isNull(val, element))
165           return;
166         // ilc_022502.1_start
167

168         // ilc_022502.2_start
169
// remove the null test, see above
170
String JavaDoc s = null;
171         char c[] = null;
172         //if (val!=null) {
173
s = val.toString().trim();
174         c = s.toCharArray();
175         //}
176
// ilc_022502.2_end
177

178         // ilc_022502.3_start
179
// val is always origVal, and no need to test nullness
180
// dbr_122801.1_start If we have a ParseException but the user entered something, this can't possibly be a digit
181
//Object oVal = element.getOrigVal();
182
if (//oVal!=null
183
//&& oVal instanceof String
184
//&& !((String)oVal).trim().equals("")
185
//&&
186
element.getParseException()!=null) {
187             throw this.generateException(element,
188                                          deferExceptions,
189                                          s + " contains characters other than digits because " + element.getParseException());
190
191         }
192         // dbr_122801.1_end
193
// ilc_022502.4_start
194

195         //Field Value is not required:
196
// only test when field value is not null, confirm that field value contains
197
// only digits 0-9, from: contructor()/constructor(errorMessage)
198
if (!requireLength) {
199             if (s!=null && s.length()>0) {
200                 //validate that val is made up of only characters 0-9
201
if (localLogger.isInfoEnabled()) localLogger.info("validating to see if val {"+s+"} contains only the numbers 0-9");
202                 for (int i=0; i<c.length; i++) {
203                     if ((c[i] < '0') || (c[i] > '9')) {
204                         throw this.generateException(element, deferExceptions, "Val {"+val+"} contains characters other than 0123456789");
205                     }
206                 }
207             }
208
209         //Field Value is required:
210
// field value tests for null/length of field matches requested length/ and/or
211
// phone number requirement tests
212
} else {
213             //test to see if field value is null, if so no further tests required
214
if (s==null || s.length()<1) {
215                 throw this.generateException(element, deferExceptions, "Null value fails to meet minimum length of "+len);
216             }
217
218             //validate that the length of val = len
219
if (localLogger.isInfoEnabled()) localLogger.info("validating to see if length of val {"+s+"} = "+len);
220             if (s.length()!=len) {
221                 throw this.generateException(element, deferExceptions, "Length of val {"+val+"} is not equal to the field length of "+len);
222             }
223
224             //validate that val is made up of only characters 0-9
225
if (localLogger.isInfoEnabled()) localLogger.info("validating to see if val {"+s+"} contains only the numbers 0-9");
226             for (int i=0; i<c.length; i++) {
227                 if ((c[i] < '0') || (c[i] > '9')) {
228                     throw this.generateException(element, deferExceptions, "Val {"+val+"} contains characters other than 0123456789");
229                 }
230             }
231
232             //phone number validations
233
if (phoneNumber) {
234                 //Domestic
235
if (domesticPhone) {
236                     if (localLogger.isInfoEnabled()) localLogger.info("Validating to see if val {"+s+"} for domestic phone requirments");
237                     if ((c.length==3) && (c[0]=='0')) {
238                         throw this.generateException(element, deferExceptions, "Val {"+val+"} starts with a zero for a length of 3");
239                     }
240                 //International
241
} else {
242                     if (localLogger.isInfoEnabled()) localLogger.info("Validating val {"+s+"} for international phone requirments");
243                     //todo - we don't do anything special for international numbers now
244
}
245             }
246         }
247     }
248 }
249
Popular Tags