View Javadoc

1   package org.apache.turbine.services.intake.validator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.turbine.services.intake.model.Field;
28  
29  /***
30   * DefaultValidator that will compare a testValue against the following
31   * constraints:
32   *
33   * <table>
34   * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
35   * <tr><td>required</td><td>true|false</td><td>false</td></tr>
36   * <tr><td>mask</td><td>regexp</td><td>&nbsp;</td></tr>
37   * <tr><td>minLength</td><td>integer</td><td>0</td></tr>
38   * <tr><td>maxLength</td><td>integer</td><td>&nbsp;</td></tr>
39   * </table>
40   *
41   * This validator can serve as the base class for more specific validators
42   *
43   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
44   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
45   * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
46   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
47   * @version $Id: DefaultValidator.java 538796 2007-05-17 03:19:16Z seade $
48   */
49  abstract public class DefaultValidator
50          implements Validator, InitableByConstraintMap
51  {
52      /*** A boolean value to signify if the field is definately required or not */
53      protected boolean required = false;
54  
55      /*** The message to show if field fails required test */
56      protected String requiredMessage = null;
57  
58      /*** The minimum length of the field */
59      protected int minLength = 0;
60  
61      /*** The message to show if field fails min-length test */
62      protected String minLengthMessage = null;
63  
64      /*** The maximum length of the field */
65      protected int maxLength = 0;
66  
67      /*** The message to show if field fails max-length test */
68      protected String maxLengthMessage = null;
69  
70      /*** Error message pertaining to Rule that was broken */
71      protected String errorMessage = null;
72  
73      /*** Logging */
74      protected Log log = LogFactory.getLog(this.getClass());
75  
76      /***
77       * Constructor
78       *
79       * @param paramMap a <code>Map</code> of <code>Rule</code>'s
80       * containing constraints on the input.
81       * @exception InvalidMaskException An invalid mask was specified for one of the rules
82  
83      */
84      public DefaultValidator(Map paramMap)
85              throws InvalidMaskException
86      {
87          init(paramMap);
88      }
89  
90      /***
91       * Default constructor
92       */
93      public DefaultValidator()
94      {
95      }
96  
97      /***
98       * Extract the relevant parameters from the constraints listed
99       * in <rule> tags within the intake.xml file.
100      *
101      * @param paramMap a <code>Map</code> of <code>Rule</code>'s
102      * containing constraints on the input.
103      * @exception InvalidMaskException An invalid mask was specified for one of the rules
104      */
105     public void init(Map paramMap)
106             throws InvalidMaskException
107     {
108         Constraint constraint = (Constraint) paramMap.get(REQUIRED_RULE_NAME);
109         if (constraint != null)
110         {
111             String param = constraint.getValue();
112             required = Boolean.valueOf(param).booleanValue();
113             requiredMessage = constraint.getMessage();
114         }
115 
116         constraint = (Constraint) paramMap.get(MIN_LENGTH_RULE_NAME);
117         if (constraint != null)
118         {
119             String param = constraint.getValue();
120             minLength = Integer.parseInt(param);
121             minLengthMessage = constraint.getMessage();
122         }
123 
124         constraint = (Constraint) paramMap.get(MAX_LENGTH_RULE_NAME);
125         if (constraint != null)
126         {
127             String param = constraint.getValue();
128             maxLength = Integer.parseInt(param);
129             maxLengthMessage = constraint.getMessage();
130         }
131     }
132 
133     /***
134      * Determine whether a field meets the criteria specified
135      * in the constraints defined for this validator
136      *
137      * @param field a <code>Field</code> to be tested
138      * @return true if valid, false otherwise
139      */
140     public boolean isValid(Field field)
141     {
142         boolean valid = false;
143         try
144         {
145             assertValidity(field);
146             valid = true;
147         }
148         catch (ValidationException ve)
149         {
150             valid = false;
151         }
152         return valid;
153     }
154 
155     /***
156      * Determine whether a field meets the criteria specified
157      * in the constraints defined for this validator
158      *
159      * @param field a <code>Field</code> to be tested
160      * @exception ValidationException containing an error message if the
161      * testValue did not pass the validation tests.
162      */
163     public void assertValidity(Field field)
164             throws ValidationException
165     {
166     	if (field.isMultiValued())
167     	{
168     		String[] stringValues = (String[])field.getTestValue();
169 
170             if (stringValues == null)
171             {
172                 // We still want to pick up required fields.
173                 assertValidity((String) null);
174             }
175             else
176             {
177                 for (int i = 0; i < stringValues.length; i++)
178                 {
179                     assertValidity(stringValues[i]);
180                 }
181             }
182     	}
183     	else
184     	{
185     		assertValidity((String)field.getTestValue());
186     	}
187     }
188 
189     /***
190      * Determine whether a testValue meets the criteria specified
191      * in the constraints defined for this validator
192      *
193      * @param testValue a <code>String</code> to be tested
194      * @return true if valid, false otherwise
195      *
196      * @deprecated use isValid(Field) instead
197      */
198     public boolean isValid(String testValue)
199     {
200         boolean valid = false;
201         try
202         {
203             assertValidity(testValue);
204             valid = true;
205         }
206         catch (ValidationException ve)
207         {
208             valid = false;
209         }
210         return valid;
211     }
212 
213     /***
214      * Determine whether a testValue meets the criteria specified
215      * in the constraints defined for this validator
216      *
217      * @param testValue a <code>String</code> to be tested
218      * @exception ValidationException containing an error message if the
219      * testValue did not pass the validation tests.
220      */
221     public void assertValidity(String testValue)
222             throws ValidationException
223     {
224         if (!required && StringUtils.isEmpty(testValue))
225         {
226             return;
227         }
228         if (required && StringUtils.isEmpty(testValue))
229         {
230             errorMessage = requiredMessage;
231             throw new ValidationException(requiredMessage);
232         }
233 
234         if (minLength > 0 && testValue.length() < minLength)
235         {
236             errorMessage = minLengthMessage;
237             throw new ValidationException(minLengthMessage);
238         }
239         if (maxLength > 0 && testValue.length() > maxLength)
240         {
241             errorMessage = maxLengthMessage;
242             throw new ValidationException(maxLengthMessage);
243         }
244     }
245 
246 
247     /***
248      * Get the error message resulting from invalid input.
249      *
250      * @return a <code>String</code> message, or the empty String "".
251      */
252     public String getMessage()
253     {
254         String retValue = "";
255 
256         if(errorMessage != null)
257         {
258             retValue = errorMessage;
259         }
260 
261         return retValue;
262     }
263 
264     // ************************************************************
265     // **                Bean accessor methods                   **
266     // ************************************************************
267 
268     /***
269      * Get the value of required.
270      *
271      * @return value of required.
272      */
273     public boolean isRequired()
274     {
275         return required;
276     }
277 
278     /***
279      * Set the value of required.
280      *
281      * @param required  Value to assign to required.
282      */
283     public void setRequired(boolean required)
284     {
285         this.required = required;
286     }
287 
288     /***
289      * Get the value of requiredMessage.
290      *
291      * @return value of requiredMessage.
292      */
293     public String getRequiredMessage()
294     {
295         return requiredMessage;
296     }
297 
298     /***
299      * Set the value of requiredMessage.
300      *
301      * @param requiredMessage  Value to assign to requiredMessage.
302      */
303     public void setRequiredMessage(String requiredMessage)
304     {
305         this.requiredMessage = requiredMessage;
306     }
307 
308     /***
309      * Get the value of minLength.
310      *
311      * @return value of minLength.
312      */
313     public int getMinLength()
314     {
315         return minLength;
316     }
317 
318     /***
319      * Set the value of minLength.
320      *
321      * @param minLength  Value to assign to minLength.
322      */
323     public void setMinLength(int minLength)
324     {
325         this.minLength = minLength;
326     }
327 
328     /***
329      * Get the value of minLengthMessage.
330      *
331      * @return value of minLengthMessage.
332      */
333     public String getMinLengthMessage()
334     {
335         return minLengthMessage;
336     }
337 
338     /***
339      * Set the value of minLengthMessage.
340      *
341      * @param minLengthMessage  Value to assign to minLengthMessage.
342      */
343     public void setMinLengthMessage(String minLengthMessage)
344     {
345         this.minLengthMessage = minLengthMessage;
346     }
347 
348     /***
349      * Get the value of maxLength.
350      *
351      * @return value of maxLength.
352      */
353     public int getMaxLength()
354     {
355         return maxLength;
356     }
357 
358     /***
359      * Set the value of maxLength.
360      *
361      * @param maxLength  Value to assign to maxLength.
362      */
363     public void setMaxLength(int maxLength)
364     {
365         this.maxLength = maxLength;
366     }
367 
368     /***
369      * Get the value of maxLengthMessage.
370      *
371      * @return value of maxLengthMessage.
372      */
373     public String getMaxLengthMessage()
374     {
375         return maxLengthMessage;
376     }
377 
378     /***
379      * Set the value of maxLengthMessage.
380      *
381      * @param maxLengthMessage  Value to assign to maxLengthMessage.
382      */
383     public void setMaxLengthMessage(String maxLengthMessage)
384     {
385         this.maxLengthMessage = maxLengthMessage;
386     }
387 }