Coverage details for org.chwf.filter.GenericValidator

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.filter;
34  
35 import java.util.Map;
36  
37 /**
38  * Generic validator. Performs basic validation using {@link BeanFilter}
39  * attributes.
40  *
41  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
42  */
431public class GenericValidator extends Validator {
44  
45   /** Empty string. */
46   private static final String EMPTY_STRING = "";
47  
48   /** The filter of the validated bean. */
49   private BeanFilter filter;
50  
51   /**
52    * Constructor.<p>
53    *
54    * @param beanClass The bean class being validated.
55    * @throws InitializationException If the filter cannot be initialized.
56    */
57   public GenericValidator(Class beanClass) throws InitializationException {
582    super(beanClass);
592  }
60  
61   /**
62    * Validate property value.<p>
63    *
64    * @param object The object.
65    * @param property The property.
66    * @param value The value.
67    * @throws PropertyNotFoundException If property does not exist.
68    * @throws ValidationException If invalid.
69    */
70   public void validate(Object object, String property, Object value)
71     throws PropertyNotFoundException, ValidationException {
7227    PropertyFilter propertyFilter = getFilter().getPropertyFilter(property);
7327    checkRequired(propertyFilter, value);
7425    checkExtrema(propertyFilter, value);
7521    checkLength(propertyFilter, value);
7617    checkOptions(propertyFilter, value);
7716  }
78  
79   /**
80    * Check required fields.
81    *
82    * @param property The property checked.
83    * @param value The value checked.
84    * @throws ValidationException If required and null (or empty string).
85    */
86   private static void checkRequired(PropertyFilter property, Object value)
87     throws ValidationException {
8827    Boolean required =
89       (Boolean) property.getAttribute(BeanFilter.ATTRIBUTE_REQUIRED);
9027    if ((required == null) || required.booleanValue()) {
9127      if (value == null) {
921        throw requiredException(property);
9326      } else if (property.getType().equals(String.class)) {
9410        if (String.valueOf(value).equals(EMPTY_STRING)) {
951          throw requiredException(property);
96         }
97       }
98     }
9925  }
100  
101   /**
102    * Create exception for required failures.
103    *
104    * @param prop The property checked.
105    * @return The error.
106    */
107   private static ValidationException requiredException(PropertyFilter prop) {
1082    String label = (String) prop.getAttribute(BeanFilter.ATTRIBUTE_LABEL);
1092    ValidationException ex =
110       new ValidationException(
111         ValidationException.MESSAGE_REQUIRED_FIELD,
112         new Object[] { label });
1132    return ex;
114   }
115  
116   /**
117    * Check max/min for value.
118    *
119    * @param property The property checked.
120    * @param value The value checked.
121    * @throws ValidationException If out of range.
122    */
123   private static void checkExtrema(PropertyFilter property, Object value)
124     throws ValidationException {
12525    if (value instanceof Comparable) {
12624      Comparable number = (Comparable) value;
12724      Comparable min =
128         (Comparable) property.getAttribute(BeanFilter.ATTRIBUTE_MIN);
12924      if ((min != null) && (number.compareTo(min) < 0)) {
1302        throw extremaException(property);
131       }
13222      Comparable max =
133         (Comparable) property.getAttribute(BeanFilter.ATTRIBUTE_MAX);
13422      if ((max != null) && (number.compareTo(max) > 0)) {
1352        throw extremaException(property);
136       }
137     }
13821  }
139  
140   /**
141    * Create exception for max/min failures.
142    *
143    * @param property The property checked.
144    * @return The error.
145    */
146   private static ValidationException extremaException(PropertyFilter property) {
1474    Object min = property.getAttribute(BeanFilter.ATTRIBUTE_MIN);
1484    Object max = property.getAttribute(BeanFilter.ATTRIBUTE_MAX);
1494    String label = (String) property.getAttribute(BeanFilter.ATTRIBUTE_LABEL);
1504    if (min == null) {
1512      return new ValidationException(
152         ValidationException.MESSAGE_FAILED_MAX,
153         new Object[] { label, max });
154     }
1552    if (max == null) {
1561      return new ValidationException(
157         ValidationException.MESSAGE_FAILED_MIN,
158         new Object[] { label, min });
159     }
1601    return new ValidationException(
161       ValidationException.MESSAGE_FAILED_MIN_AND_MAX,
162       new Object[] { label, min, max });
163   }
164  
165   /**
166    * Check max/min length for value.
167    *
168    * @param property The property checked.
169    * @param value The value checked.
170    * @throws ValidationException If out of range.
171    */
172   private static void checkLength(PropertyFilter property, Object value)
173     throws ValidationException {
17421    if (value instanceof String) {
1759      String string = (String) value;
1769      Integer minlength =
177         (Integer) property.getAttribute(BeanFilter.ATTRIBUTE_MINLENGTH);
1789      if ((minlength != null) && (string.length()) < minlength.intValue()) {
1792        throw lengthException(property);
180       }
1817      Integer maxlength =
182         (Integer) property.getAttribute(BeanFilter.ATTRIBUTE_MAXLENGTH);
1837      if ((maxlength != null) && (string.length()) > maxlength.intValue()) {
1842        throw lengthException(property);
185       }
186     }
18717  }
188  
189   /**
190    * Create exception for max/min length failures.
191    *
192    * @param property The property checked.
193    * @return The error.
194    */
195   private static ValidationException lengthException(PropertyFilter property) {
1964    Object minlength = property.getAttribute(BeanFilter.ATTRIBUTE_MINLENGTH);
1974    Object maxlength = property.getAttribute(BeanFilter.ATTRIBUTE_MAXLENGTH);
1984    String label = (String) property.getAttribute(BeanFilter.ATTRIBUTE_LABEL);
1994    if (minlength == null) {
2002      return new ValidationException(
201         ValidationException.MESSAGE_FAILED_MAXLENGTH,
202         new Object[] { label, maxlength });
203     }
2042    if (maxlength == null) {
2051      return new ValidationException(
206         ValidationException.MESSAGE_FAILED_MINLENGTH,
207         new Object[] { label, minlength });
208     }
2091    return new ValidationException(
210       ValidationException.MESSAGE_FAILED_MIN_AND_MAXLENGTH,
211       new Object[] { label, minlength, maxlength });
212   }
213  
214   /**
215    * Check that value is in the options list.
216    *
217    * @param property The property checked.
218    * @param value The value checked.
219    * @throws ValidationException If not in list.
220    */
221   private static void checkOptions(PropertyFilter property, Object value)
222     throws ValidationException {
22317    Map options = (Map) property.getAttribute(BeanFilter.ATTRIBUTE_OPTIONS);
22417    if ((options != null) && !options.keySet().contains(value)) {
2251      String label = (String) property.getAttribute(BeanFilter.ATTRIBUTE_LABEL);
2261      throw new ValidationException(
227         ValidationException.MESSAGE_INVALID_OPTION,
228         new Object[] { value, label });
229     }
23016  }
231  
232   /** Object used for synchronization locks on bean filter creation. */
2332  private final Object lock = new Object();
234  
235   /**
236    * Lazy-load bean filter. This lazy-loading is used because validators may
237    * be used by the BeanFilter, so that the BeanFilter for a given validator
238    * may not be properly initialized when the Validator is constructed.
239    *
240    * @return The filter.
241    * @throws InitializationException If filter cannot be initialized.
242    */
243   private BeanFilter getFilter() throws InitializationException {
24427    if (this.filter == null) {
2452      synchronized (lock) {
2462        this.filter = BeanFilter.findFilter(getBeanClass());
2472      }
248     }
24927    return filter;
250   }
251 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.