Coverage details for org.chwf.filter.Validator

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.HashMap;
36 import java.util.Map;
37  
38 /**
39  * The <code>Validator</code> class is the abstract superclass of all
40  * validators. Validators serve as an abstraction layer supporting common
41  * validation logic. You can define custom <code>Validator</code> classes
42  * for a given JavaBean by defining a new class that has the same name as
43  * the JavaBean, with "Validator" added. If no custom validators are defined,
44  * Chrysalis will use the {@link GenericValidator} instead.
45  *
46  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
47  */
48 public abstract class Validator {
49  
50   /** Suffix for validator classes. */
51   private static final String VALIDATOR_SUFFIX = "Validator";
52  
53   /** Static cache of all validators used by the system. */
541  private static final Map VALIDATORS = new HashMap();
55  
56   /** The class of the validated bean. */
57   private final Class beanClass;
58  
59   /**
60    * Constructor to specify the class of validated bean. This constructor is
61    * called by subclasses: <code>super(beanClass);</code><p>
62    *
63    * @param beanClass The class of the validated bean.
64    */
653  protected Validator(Class beanClass) {
663    this.beanClass = beanClass;
673  }
68  
69   /**
70    * Find the validator for a given object.<p>
71    *
72    * @param object The object being validated.
73    * @return The validator.
74    * @throws InitializationException If it cannot be initialized.
75    */
76   public static Validator findValidator(Object object)
77     throws InitializationException {
781    return findValidator(object.getClass());
79   }
80  
81   /**
82    * Find the validator for a given class.<p>
83    *
84    * @param beanClass The class of the validated bean.
85    * @return The validator.
86    * @throws InitializationException If it cannot be initialized.
87    */
88   public static Validator findValidator(Class beanClass)
89     throws InitializationException {
9015    return findValidator(beanClass.getName());
91   }
92  
93   /**
94    * Find the validator for the specified class name. This method first searches
95    * for the validator class <code>beanClass + "Validator"</code>. If it cannot
96    * create this object it creates a {@link org.chwf.filter.GenericValidator}
97    * for the bean class using reflection.<p>
98    *
99    * @param className This must be the fully qualified name of the class.
100    * @return The validator.
101    * @throws InitializationException If it cannot be initialized.
102    */
103   public static Validator findValidator(String className)
104     throws InitializationException {
10516    Validator validator = null;
10616    synchronized (VALIDATORS) {
10716      validator = (Validator) VALIDATORS.get(className);
10816      if (validator == null) {
109         try {
1104          String validatorName = className + VALIDATOR_SUFFIX;
1114          validator = (Validator) Class.forName(validatorName).newInstance();
1121        } catch (Exception ex) {
113           // Custom validator cannot be created. Construct generic validator:
1143          try {
1153            validator = new GenericValidator(Class.forName(className));
1162          } catch (ClassNotFoundException ex2) {
1171            throw new InitializationException(
118               InitializationException.MESSAGE_CLASS_NOT_FOUND,
119               new Object[] { className });
120           }
121         }
1223        VALIDATORS.put(className, validator);
123       }
12415    }
12515    return validator;
126   }
127  
128   /**
129    * The validated class.<p>
130    *
131    * @return The validated class.
132    */
133   public Class getBeanClass() {
1343    return beanClass;
135   }
136  
137   /**
138    * <p>Validate property value.</p>
139    *
140    * @param object The object.
141    * @param property The property.
142    * @param value The value.
143    * @throws PropertyNotFoundException If property does not exist.
144    * @throws ValidationException If invalid.
145    */
146   public abstract void validate(Object object, String property, Object value)
147     throws PropertyNotFoundException, ValidationException;
148  
149   /**
150    * <p>Validate property value.</p>
151    *
152    * @param object The object.
153    * @param property The property.
154    * @param value The value.
155    * @throws PropertyNotFoundException If property does not exist.
156    * @throws ValidationException If invalid.
157    */
158   public void validate(Object object, String property, boolean value)
159     throws PropertyNotFoundException, ValidationException {
1601    validate(object, property, new Boolean(value));
1611  }
162  
163   /**
164    * <p>Validate property value.</p>
165    *
166    * @param object The object.
167    * @param property The property.
168    * @param value The value.
169    * @throws PropertyNotFoundException If property does not exist.
170    * @throws ValidationException If invalid.
171    */
172   public void validate(Object object, String property, byte value)
173     throws PropertyNotFoundException, ValidationException {
1741    validate(object, property, new Byte(value));
1751  }
176  
177   /**
178    * <p>Validate property value.</p>
179    *
180    * @param object The object.
181    * @param property The property.
182    * @param value The value.
183    * @throws PropertyNotFoundException If property does not exist.
184    * @throws ValidationException If invalid.
185    */
186   public void validate(Object object, String property, short value)
187     throws PropertyNotFoundException, ValidationException {
1881    validate(object, property, new Short(value));
1891  }
190  
191   /**
192    * <p>Validate property value.</p>
193    *
194    * @param object The object.
195    * @param property The property.
196    * @param value The value.
197    * @throws PropertyNotFoundException If property does not exist.
198    * @throws ValidationException If invalid.
199    */
200   public void validate(Object object, String property, int value)
201     throws PropertyNotFoundException, ValidationException {
2021    validate(object, property, new Integer(value));
2031  }
204  
205   /**
206    * <p>Validate property value.</p>
207    *
208    * @param object The object.
209    * @param property The property.
210    * @param value The value.
211    * @throws PropertyNotFoundException If property does not exist.
212    * @throws ValidationException If invalid.
213    */
214   public void validate(Object object, String property, long value)
215     throws PropertyNotFoundException, ValidationException {
2161    validate(object, property, new Long(value));
2171  }
218  
219   /**
220    * <p>Validate property value.</p>
221    *
222    * @param object The object.
223    * @param property The property.
224    * @param value The value.
225    * @throws PropertyNotFoundException If property does not exist.
226    * @throws ValidationException If invalid.
227    */
228   public void validate(Object object, String property, float value)
229     throws PropertyNotFoundException, ValidationException {
2301    validate(object, property, new Float(value));
2311  }
232  
233   /**
234    * <p>Validate property value.</p>
235    *
236    * @param object The object.
237    * @param property The property.
238    * @param value The value.
239    * @throws PropertyNotFoundException If property does not exist.
240    * @throws ValidationException If invalid.
241    */
242   public void validate(Object object, String property, double value)
243     throws PropertyNotFoundException, ValidationException {
2441    validate(object, property, new Double(value));
2451  }
246 }

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.