| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Validator |
|
| 1.6666666666666667;1.667 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.validator; | |
| 18 | ||
| 19 | import java.io.Serializable; | |
| 20 | import java.util.HashMap; | |
| 21 | import java.util.Locale; | |
| 22 | import java.util.Map; | |
| 23 | ||
| 24 | /** | |
| 25 | * Validations are processed by the validate method. An instance of | |
| 26 | * <code>ValidatorResources</code> is used to define the validators | |
| 27 | * (validation methods) and the validation rules for a JavaBean. | |
| 28 | * | |
| 29 | * @version $Revision: 1651910 $ | |
| 30 | */ | |
| 31 | // TODO mutable fields should be made private and accessed via suitable methods only | |
| 32 | public class Validator implements Serializable { | |
| 33 | ||
| 34 | private static final long serialVersionUID = -7119418755208731611L; | |
| 35 | ||
| 36 | /** | |
| 37 | * Resources key the JavaBean is stored to perform validation on. | |
| 38 | */ | |
| 39 | public static final String BEAN_PARAM = "java.lang.Object"; | |
| 40 | ||
| 41 | /** | |
| 42 | * Resources key the <code>ValidatorAction</code> is stored under. | |
| 43 | * This will be automatically passed into a validation method | |
| 44 | * with the current <code>ValidatorAction</code> if it is | |
| 45 | * specified in the method signature. | |
| 46 | */ | |
| 47 | public static final String VALIDATOR_ACTION_PARAM = | |
| 48 | "org.apache.commons.validator.ValidatorAction"; | |
| 49 | ||
| 50 | /** | |
| 51 | * Resources key the <code>ValidatorResults</code> is stored under. | |
| 52 | * This will be automatically passed into a validation method | |
| 53 | * with the current <code>ValidatorResults</code> if it is | |
| 54 | * specified in the method signature. | |
| 55 | */ | |
| 56 | public static final String VALIDATOR_RESULTS_PARAM = | |
| 57 | "org.apache.commons.validator.ValidatorResults"; | |
| 58 | ||
| 59 | /** | |
| 60 | * Resources key the <code>Form</code> is stored under. | |
| 61 | * This will be automatically passed into a validation method | |
| 62 | * with the current <code>Form</code> if it is | |
| 63 | * specified in the method signature. | |
| 64 | */ | |
| 65 | public static final String FORM_PARAM = "org.apache.commons.validator.Form"; | |
| 66 | ||
| 67 | /** | |
| 68 | * Resources key the <code>Field</code> is stored under. | |
| 69 | * This will be automatically passed into a validation method | |
| 70 | * with the current <code>Field</code> if it is | |
| 71 | * specified in the method signature. | |
| 72 | */ | |
| 73 | public static final String FIELD_PARAM = "org.apache.commons.validator.Field"; | |
| 74 | ||
| 75 | /** | |
| 76 | * Resources key the <code>Validator</code> is stored under. | |
| 77 | * This will be automatically passed into a validation method | |
| 78 | * with the current <code>Validator</code> if it is | |
| 79 | * specified in the method signature. | |
| 80 | */ | |
| 81 | public static final String VALIDATOR_PARAM = | |
| 82 | "org.apache.commons.validator.Validator"; | |
| 83 | ||
| 84 | /** | |
| 85 | * Resources key the <code>Locale</code> is stored. | |
| 86 | * This will be used to retrieve the appropriate | |
| 87 | * <code>FormSet</code> and <code>Form</code> to be | |
| 88 | * processed. | |
| 89 | */ | |
| 90 | public static final String LOCALE_PARAM = "java.util.Locale"; | |
| 91 | ||
| 92 | /** | |
| 93 | * The Validator Resources. | |
| 94 | */ | |
| 95 | 122 | protected ValidatorResources resources = null; |
| 96 | ||
| 97 | /** | |
| 98 | * The name of the form to validate | |
| 99 | */ | |
| 100 | 122 | protected String formName = null; |
| 101 | ||
| 102 | /** | |
| 103 | * The name of the field on the form to validate | |
| 104 | * @since 1.2.0 | |
| 105 | */ | |
| 106 | 122 | protected String fieldName = null; |
| 107 | ||
| 108 | /** | |
| 109 | * Maps validation method parameter class names to the objects to be passed | |
| 110 | * into the method. | |
| 111 | */ | |
| 112 | 122 | protected Map<String, Object> parameters = new HashMap<String, Object>(); // <String, Object> |
| 113 | ||
| 114 | /** | |
| 115 | * The current page number to validate. | |
| 116 | */ | |
| 117 | 122 | protected int page = 0; |
| 118 | ||
| 119 | /** | |
| 120 | * The class loader to use for instantiating application objects. | |
| 121 | * If not specified, the context class loader, or the class loader | |
| 122 | * used to load Digester itself, is used, based on the value of the | |
| 123 | * <code>useContextClassLoader</code> variable. | |
| 124 | */ | |
| 125 | 122 | protected transient ClassLoader classLoader = null; |
| 126 | ||
| 127 | /** | |
| 128 | * Whether or not to use the Context ClassLoader when loading classes | |
| 129 | * for instantiating new objects. Default is <code>false</code>. | |
| 130 | */ | |
| 131 | 122 | protected boolean useContextClassLoader = false; |
| 132 | ||
| 133 | /** | |
| 134 | * Set this to true to not return Fields that pass validation. Only return failures. | |
| 135 | */ | |
| 136 | 122 | protected boolean onlyReturnErrors = false; |
| 137 | ||
| 138 | /** | |
| 139 | * Construct a <code>Validator</code> that will | |
| 140 | * use the <code>ValidatorResources</code> | |
| 141 | * passed in to retrieve pluggable validators | |
| 142 | * the different sets of validation rules. | |
| 143 | * | |
| 144 | * @param resources <code>ValidatorResources</code> to use during validation. | |
| 145 | */ | |
| 146 | public Validator(ValidatorResources resources) { | |
| 147 | 0 | this(resources, null); |
| 148 | 0 | } |
| 149 | ||
| 150 | /** | |
| 151 | * Construct a <code>Validator</code> that will | |
| 152 | * use the <code>ValidatorResources</code> | |
| 153 | * passed in to retrieve pluggable validators | |
| 154 | * the different sets of validation rules. | |
| 155 | * | |
| 156 | * @param resources <code>ValidatorResources</code> to use during validation. | |
| 157 | * @param formName Key used for retrieving the set of validation rules. | |
| 158 | */ | |
| 159 | 121 | public Validator(ValidatorResources resources, String formName) { |
| 160 | 121 | if (resources == null) { |
| 161 | 0 | throw new IllegalArgumentException("Resources cannot be null."); |
| 162 | } | |
| 163 | ||
| 164 | 121 | this.resources = resources; |
| 165 | 121 | this.formName = formName; |
| 166 | 121 | } |
| 167 | ||
| 168 | /** | |
| 169 | * Construct a <code>Validator</code> that will | |
| 170 | * use the <code>ValidatorResources</code> | |
| 171 | * passed in to retrieve pluggable validators | |
| 172 | * the different sets of validation rules. | |
| 173 | * | |
| 174 | * @param resources <code>ValidatorResources</code> to use during validation. | |
| 175 | * @param formName Key used for retrieving the set of validation rules. | |
| 176 | * @param fieldName Key used for retrieving the set of validation rules for a field | |
| 177 | * @since 1.2.0 | |
| 178 | */ | |
| 179 | 1 | public Validator(ValidatorResources resources, String formName, String fieldName) { |
| 180 | 1 | if (resources == null) { |
| 181 | 0 | throw new IllegalArgumentException("Resources cannot be null."); |
| 182 | } | |
| 183 | ||
| 184 | 1 | this.resources = resources; |
| 185 | 1 | this.formName = formName; |
| 186 | 1 | this.fieldName = fieldName; |
| 187 | 1 | } |
| 188 | ||
| 189 | /** | |
| 190 | * Set a parameter of a pluggable validation method. | |
| 191 | * | |
| 192 | * @param parameterClassName The full class name of the parameter of the | |
| 193 | * validation method that corresponds to the value/instance passed in with it. | |
| 194 | * | |
| 195 | * @param parameterValue The instance that will be passed into the | |
| 196 | * validation method. | |
| 197 | */ | |
| 198 | public void setParameter(String parameterClassName, Object parameterValue) { | |
| 199 | 383 | this.parameters.put(parameterClassName, parameterValue); |
| 200 | 383 | } |
| 201 | ||
| 202 | /** | |
| 203 | * Returns the value of the specified parameter that will be used during the | |
| 204 | * processing of validations. | |
| 205 | * | |
| 206 | * @param parameterClassName The full class name of the parameter of the | |
| 207 | * validation method that corresponds to the value/instance passed in with it. | |
| 208 | * @return value of the specified parameter. | |
| 209 | */ | |
| 210 | public Object getParameterValue(String parameterClassName) { | |
| 211 | 142 | return this.parameters.get(parameterClassName); |
| 212 | } | |
| 213 | ||
| 214 | /** | |
| 215 | * Gets the form name which is the key to a set of validation rules. | |
| 216 | * @return the name of the form. | |
| 217 | */ | |
| 218 | public String getFormName() { | |
| 219 | 0 | return formName; |
| 220 | } | |
| 221 | ||
| 222 | /** | |
| 223 | * Sets the form name which is the key to a set of validation rules. | |
| 224 | * @param formName the name of the form. | |
| 225 | */ | |
| 226 | public void setFormName(String formName) { | |
| 227 | 0 | this.formName = formName; |
| 228 | 0 | } |
| 229 | ||
| 230 | /** | |
| 231 | * Sets the name of the field to validate in a form (optional) | |
| 232 | * | |
| 233 | * @param fieldName The name of the field in a form set | |
| 234 | * @since 1.2.0 | |
| 235 | */ | |
| 236 | public void setFieldName(String fieldName) { | |
| 237 | 0 | this.fieldName = fieldName; |
| 238 | 0 | } |
| 239 | ||
| 240 | /** | |
| 241 | * Gets the page. | |
| 242 | * | |
| 243 | * <p> | |
| 244 | * This in conjunction with the page property of | |
| 245 | * a {@code Field} can control the processing of fields. If the field's | |
| 246 | * page is less than or equal to this page value, it will be processed. | |
| 247 | * </p> | |
| 248 | * | |
| 249 | * @return the page number. | |
| 250 | */ | |
| 251 | public int getPage() { | |
| 252 | 0 | return page; |
| 253 | } | |
| 254 | ||
| 255 | /** | |
| 256 | * Sets the page. | |
| 257 | * <p> | |
| 258 | * This in conjunction with the page property of | |
| 259 | * a {@code Field} can control the processing of fields. If the field's page | |
| 260 | * is less than or equal to this page value, it will be processed. | |
| 261 | * </p> | |
| 262 | * | |
| 263 | * @param page the page number. | |
| 264 | */ | |
| 265 | public void setPage(int page) { | |
| 266 | 0 | this.page = page; |
| 267 | 0 | } |
| 268 | ||
| 269 | /** | |
| 270 | * Clears the form name, resources that were added, and the page that was | |
| 271 | * set (if any). This can be called to reinitialize the Validator instance | |
| 272 | * so it can be reused. The form name (key to set of validation rules) and any | |
| 273 | * resources needed, like the JavaBean being validated, will need to | |
| 274 | * set and/or added to this instance again. The | |
| 275 | * <code>ValidatorResources</code> will not be removed since it can be used | |
| 276 | * again and is thread safe. | |
| 277 | */ | |
| 278 | public void clear() { | |
| 279 | 0 | this.formName = null; |
| 280 | 0 | this.fieldName = null; |
| 281 | 0 | this.parameters = new HashMap<String, Object>(); |
| 282 | 0 | this.page = 0; |
| 283 | 0 | } |
| 284 | ||
| 285 | /** | |
| 286 | * Return the boolean as to whether the context classloader should be used. | |
| 287 | * @return whether the context classloader should be used. | |
| 288 | */ | |
| 289 | public boolean getUseContextClassLoader() { | |
| 290 | 0 | return this.useContextClassLoader; |
| 291 | } | |
| 292 | ||
| 293 | /** | |
| 294 | * Determine whether to use the Context ClassLoader (the one found by | |
| 295 | * calling <code>Thread.currentThread().getContextClassLoader()</code>) | |
| 296 | * to resolve/load classes that are defined in various rules. If not | |
| 297 | * using Context ClassLoader, then the class-loading defaults to | |
| 298 | * using the calling-class' ClassLoader. | |
| 299 | * | |
| 300 | * @param use determines whether to use Context ClassLoader. | |
| 301 | */ | |
| 302 | public void setUseContextClassLoader(boolean use) { | |
| 303 | 0 | this.useContextClassLoader = use; |
| 304 | 0 | } |
| 305 | ||
| 306 | /** | |
| 307 | * Return the class loader to be used for instantiating application objects | |
| 308 | * when required. This is determined based upon the following rules: | |
| 309 | * <ul> | |
| 310 | * <li>The class loader set by <code>setClassLoader()</code>, if any</li> | |
| 311 | * <li>The thread context class loader, if it exists and the | |
| 312 | * <code>useContextClassLoader</code> property is set to true</li> | |
| 313 | * <li>The class loader used to load the Digester class itself. | |
| 314 | * </ul> | |
| 315 | * @return the class loader. | |
| 316 | */ | |
| 317 | public ClassLoader getClassLoader() { | |
| 318 | 131 | if (this.classLoader != null) { |
| 319 | 0 | return this.classLoader; |
| 320 | } | |
| 321 | ||
| 322 | 131 | if (this.useContextClassLoader) { |
| 323 | 0 | ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); |
| 324 | 0 | if (contextLoader != null) { |
| 325 | 0 | return contextLoader; |
| 326 | } | |
| 327 | } | |
| 328 | ||
| 329 | 131 | return this.getClass().getClassLoader(); |
| 330 | } | |
| 331 | ||
| 332 | /** | |
| 333 | * Set the class loader to be used for instantiating application objects | |
| 334 | * when required. | |
| 335 | * | |
| 336 | * @param classLoader The new class loader to use, or <code>null</code> | |
| 337 | * to revert to the standard rules | |
| 338 | */ | |
| 339 | public void setClassLoader(ClassLoader classLoader) { | |
| 340 | 0 | this.classLoader = classLoader; |
| 341 | 0 | } |
| 342 | ||
| 343 | /** | |
| 344 | * Performs validations based on the configured resources. | |
| 345 | * | |
| 346 | * @return The <code>Map</code> returned uses the property of the | |
| 347 | * <code>Field</code> for the key and the value is the number of error the | |
| 348 | * field had. | |
| 349 | * @throws ValidatorException If an error occurs during validation | |
| 350 | */ | |
| 351 | public ValidatorResults validate() throws ValidatorException { | |
| 352 | 125 | Locale locale = (Locale) this.getParameterValue(LOCALE_PARAM); |
| 353 | ||
| 354 | 125 | if (locale == null) { |
| 355 | 115 | locale = Locale.getDefault(); |
| 356 | } | |
| 357 | ||
| 358 | 125 | this.setParameter(VALIDATOR_PARAM, this); |
| 359 | ||
| 360 | 125 | Form form = this.resources.getForm(locale, this.formName); |
| 361 | 125 | if (form != null) { |
| 362 | 125 | this.setParameter(FORM_PARAM, form); |
| 363 | 125 | return form.validate( |
| 364 | this.parameters, | |
| 365 | this.resources.getValidatorActions(), | |
| 366 | this.page, | |
| 367 | this.fieldName); | |
| 368 | } | |
| 369 | ||
| 370 | 0 | return new ValidatorResults(); |
| 371 | } | |
| 372 | ||
| 373 | /** | |
| 374 | * Returns true if the Validator is only returning Fields that fail validation. | |
| 375 | * @return whether only failed fields are returned. | |
| 376 | */ | |
| 377 | public boolean getOnlyReturnErrors() { | |
| 378 | 105 | return onlyReturnErrors; |
| 379 | } | |
| 380 | ||
| 381 | /** | |
| 382 | * Configures which Fields the Validator returns from the validate() method. Set this | |
| 383 | * to true to only return Fields that failed validation. By default, validate() returns | |
| 384 | * all fields. | |
| 385 | * @param onlyReturnErrors whether only failed fields are returned. | |
| 386 | */ | |
| 387 | public void setOnlyReturnErrors(boolean onlyReturnErrors) { | |
| 388 | 1 | this.onlyReturnErrors = onlyReturnErrors; |
| 389 | 1 | } |
| 390 | ||
| 391 | } |