KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > validator > DefaultValidator


1 package org.apache.turbine.services.intake.validator;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Map JavaDoc;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26  * DefaultValidator that will compare a testValue against the following
27  * constraints:
28  *
29  * <table>
30  * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
31  * <tr><td>required</td><td>true|false</td><td>false</td></tr>
32  * <tr><td>mask</td><td>regexp</td><td>&nbsp;</td></tr>
33  * <tr><td>minLength</td><td>integer</td><td>0</td></tr>
34  * <tr><td>maxLength</td><td>integer</td><td>&nbsp;</td></tr>
35  * </table>
36  *
37  * This validator can serve as the base class for more specific validators
38  *
39  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
40  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
41  * @author <a HREF="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
42  * @version $Id: DefaultValidator.java,v 1.9.2.4 2004/05/20 03:06:47 seade Exp $
43  */

44 abstract public class DefaultValidator
45         implements Validator, InitableByConstraintMap
46 {
47     /** A boolean value to signify if the field is definately required or not */
48     protected boolean required = false;
49
50     /** The message to show if field fails required test */
51     protected String JavaDoc requiredMessage = null;
52
53     /** The minimum length of the field */
54     protected int minLength = 0;
55
56     /** The message to show if field fails min-length test */
57     protected String JavaDoc minLengthMessage = null;
58
59     /** The maximum length of the field */
60     protected int maxLength = 0;
61
62     /** The message to show if field fails max-length test */
63     protected String JavaDoc maxLengthMessage = null;
64
65     /** Error message pertaining to Rule that was broken */
66     protected String JavaDoc errorMessage = null;
67
68     /** Logging */
69     protected Log log = LogFactory.getLog(this.getClass());
70
71     /**
72      * Constructor
73      *
74      * @param paramMap a <code>Map</code> of <code>Rule</code>'s
75      * containing constraints on the input.
76      * @exception InvalidMaskException An invalid mask was specified for one of the rules
77
78     */

79     public DefaultValidator(Map JavaDoc paramMap)
80             throws InvalidMaskException
81     {
82         init(paramMap);
83     }
84
85     /**
86      * Default constructor
87      */

88     public DefaultValidator()
89     {
90     }
91
92     /**
93      * Extract the relevant parameters from the constraints listed
94      * in <rule> tags within the intake.xml file.
95      *
96      * @param paramMap a <code>Map</code> of <code>Rule</code>'s
97      * containing constraints on the input.
98      * @exception InvalidMaskException An invalid mask was specified for one of the rules
99      */

100     public void init(Map JavaDoc paramMap)
101             throws InvalidMaskException
102     {
103         Constraint constraint = (Constraint) paramMap.get(REQUIRED_RULE_NAME);
104         if (constraint != null)
105         {
106             String JavaDoc param = constraint.getValue();
107             required = new Boolean JavaDoc(param).booleanValue();
108             requiredMessage = constraint.getMessage();
109         }
110
111         constraint = (Constraint) paramMap.get(MIN_LENGTH_RULE_NAME);
112         if (constraint != null)
113         {
114             String JavaDoc param = constraint.getValue();
115             minLength = Integer.parseInt(param);
116             minLengthMessage = constraint.getMessage();
117         }
118
119         constraint = (Constraint) paramMap.get(MAX_LENGTH_RULE_NAME);
120         if (constraint != null)
121         {
122             String JavaDoc param = constraint.getValue();
123             maxLength = Integer.parseInt(param);
124             maxLengthMessage = constraint.getMessage();
125         }
126     }
127
128     /**
129      * Determine whether a testValue meets the criteria specified
130      * in the constraints defined for this validator
131      *
132      * @param testValue a <code>String</code> to be tested
133      * @return true if valid, false otherwise
134      */

135     public boolean isValid(String JavaDoc testValue)
136     {
137         boolean valid = false;
138         try
139         {
140             assertValidity(testValue);
141             valid = true;
142         }
143         catch (ValidationException ve)
144         {
145             valid = false;
146         }
147         return valid;
148     }
149
150     /**
151      * Determine whether a testValue meets the criteria specified
152      * in the constraints defined for this validator
153      *
154      * @param testValue a <code>String</code> to be tested
155      * @exception ValidationException containing an error message if the
156      * testValue did not pass the validation tests.
157      */

158     public void assertValidity(String JavaDoc testValue)
159             throws ValidationException
160     {
161         if (!required && StringUtils.isEmpty(testValue))
162         {
163             return;
164         }
165         if (required && StringUtils.isEmpty(testValue))
166         {
167             errorMessage = requiredMessage;
168             throw new ValidationException(requiredMessage);
169         }
170
171         if (minLength > 0 && testValue.length() < minLength)
172         {
173             errorMessage = minLengthMessage;
174             throw new ValidationException(minLengthMessage);
175         }
176         if (maxLength > 0 && testValue.length() > maxLength)
177         {
178             errorMessage = maxLengthMessage;
179             throw new ValidationException(maxLengthMessage);
180         }
181     }
182
183
184     /**
185      * Get the error message resulting from invalid input.
186      *
187      * @return a <code>String</code> message, or the empty String "".
188      */

189     public String JavaDoc getMessage()
190     {
191         String JavaDoc retValue = "";
192
193         if(errorMessage != null)
194         {
195             retValue = errorMessage;
196         }
197
198         return retValue;
199     }
200
201     // ************************************************************
202
// ** Bean accessor methods **
203
// ************************************************************
204

205     /**
206      * Get the value of required.
207      *
208      * @return value of required.
209      */

210     public boolean isRequired()
211     {
212         return required;
213     }
214
215     /**
216      * Set the value of required.
217      *
218      * @param required Value to assign to required.
219      */

220     public void setRequired(boolean required)
221     {
222         this.required = required;
223     }
224
225     /**
226      * Get the value of requiredMessage.
227      *
228      * @return value of requiredMessage.
229      */

230     public String JavaDoc getRequiredMessage()
231     {
232         return requiredMessage;
233     }
234
235     /**
236      * Set the value of requiredMessage.
237      *
238      * @param requiredMessage Value to assign to requiredMessage.
239      */

240     public void setRequiredMessage(String JavaDoc requiredMessage)
241     {
242         this.requiredMessage = requiredMessage;
243     }
244
245     /**
246      * Get the value of minLength.
247      *
248      * @return value of minLength.
249      */

250     public int getMinLength()
251     {
252         return minLength;
253     }
254
255     /**
256      * Set the value of minLength.
257      *
258      * @param minLength Value to assign to minLength.
259      */

260     public void setMinLength(int minLength)
261     {
262         this.minLength = minLength;
263     }
264
265     /**
266      * Get the value of minLengthMessage.
267      *
268      * @return value of minLengthMessage.
269      */

270     public String JavaDoc getMinLengthMessage()
271     {
272         return minLengthMessage;
273     }
274
275     /**
276      * Set the value of minLengthMessage.
277      *
278      * @param minLengthMessage Value to assign to minLengthMessage.
279      */

280     public void setMinLengthMessage(String JavaDoc minLengthMessage)
281     {
282         this.minLengthMessage = minLengthMessage;
283     }
284
285     /**
286      * Get the value of maxLength.
287      *
288      * @return value of maxLength.
289      */

290     public int getMaxLength()
291     {
292         return maxLength;
293     }
294
295     /**
296      * Set the value of maxLength.
297      *
298      * @param maxLength Value to assign to maxLength.
299      */

300     public void setMaxLength(int maxLength)
301     {
302         this.maxLength = maxLength;
303     }
304
305     /**
306      * Get the value of maxLengthMessage.
307      *
308      * @return value of maxLengthMessage.
309      */

310     public String JavaDoc getMaxLengthMessage()
311     {
312         return maxLengthMessage;
313     }
314
315     /**
316      * Set the value of maxLengthMessage.
317      *
318      * @param maxLengthMessage Value to assign to maxLengthMessage.
319      */

320     public void setMaxLengthMessage(String JavaDoc maxLengthMessage)
321     {
322         this.maxLengthMessage = maxLengthMessage;
323     }
324 }
325
Popular Tags