| Line | Hits | Source |
|---|---|---|
| 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. */ | |
| 54 | 1 | 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 | */ | |
| 65 | 3 | protected Validator(Class beanClass) { |
| 66 | 3 | this.beanClass = beanClass; |
| 67 | 3 | } |
| 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 { | |
| 78 | 1 | 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 { | |
| 90 | 15 | 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 { | |
| 105 | 16 | Validator validator = null; |
| 106 | 16 | synchronized (VALIDATORS) { |
| 107 | 16 | validator = (Validator) VALIDATORS.get(className); |
| 108 | 16 | if (validator == null) { |
| 109 | try { | |
| 110 | 4 | String validatorName = className + VALIDATOR_SUFFIX; |
| 111 | 4 | validator = (Validator) Class.forName(validatorName).newInstance(); |
| 112 | 1 | } catch (Exception ex) { |
| 113 | // Custom validator cannot be created. Construct generic validator: | |
| 114 | 3 | try { |
| 115 | 3 | validator = new GenericValidator(Class.forName(className)); |
| 116 | 2 | } catch (ClassNotFoundException ex2) { |
| 117 | 1 | throw new InitializationException( |
| 118 | InitializationException.MESSAGE_CLASS_NOT_FOUND, | |
| 119 | new Object[] { className }); | |
| 120 | } | |
| 121 | } | |
| 122 | 3 | VALIDATORS.put(className, validator); |
| 123 | } | |
| 124 | 15 | } |
| 125 | 15 | return validator; |
| 126 | } | |
| 127 | ||
| 128 | /** | |
| 129 | * The validated class.<p> | |
| 130 | * | |
| 131 | * @return The validated class. | |
| 132 | */ | |
| 133 | public Class getBeanClass() { | |
| 134 | 3 | 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 { | |
| 160 | 1 | validate(object, property, new Boolean(value)); |
| 161 | 1 | } |
| 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 { | |
| 174 | 1 | validate(object, property, new Byte(value)); |
| 175 | 1 | } |
| 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 { | |
| 188 | 1 | validate(object, property, new Short(value)); |
| 189 | 1 | } |
| 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 { | |
| 202 | 1 | validate(object, property, new Integer(value)); |
| 203 | 1 | } |
| 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 { | |
| 216 | 1 | validate(object, property, new Long(value)); |
| 217 | 1 | } |
| 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 { | |
| 230 | 1 | validate(object, property, new Float(value)); |
| 231 | 1 | } |
| 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 { | |
| 244 | 1 | validate(object, property, new Double(value)); |
| 245 | 1 | } |
| 246 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |