2
* $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v 1.34 2004/02/21 17:10:29 rleland Exp $
4
* $Date: 2004/02/21 17:10:29 $
6
* ====================================================================
7
* Copyright 2001-2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
22
package org.apache.commons.validator;
24
import java.io.Serializable;
25
import java.util.HashMap;
26
import java.util.Locale;
29
import org.apache.commons.logging.Log;
30
import org.apache.commons.logging.LogFactory;
33
* Validations are processed by the validate method. An instance of
34
* <code>ValidatorResources</code> is used to define the validators
35
* (validation methods) and the validation rules for a JavaBean.
37
public class Validator implements Serializable {
41
* @deprecated Subclasses should use their own logging instance.
43
protected static Log log = LogFactory.getLog(Validator.class);
46
* Resources key the JavaBean is stored to perform validation on.
48
public static final String BEAN_PARAM = "java.lang.Object";
51
* Resources key the JavaBean is stored to perform validation on.
52
* @deprecated Use BEAN_PARAM instead.
54
public static final String BEAN_KEY = BEAN_PARAM;
57
* Resources key the <code>ValidatorAction</code> is stored under.
58
* This will be automatically passed into a validation method
59
* with the current <code>ValidatorAction</code> if it is
60
* specified in the method signature.
62
public static final String VALIDATOR_ACTION_PARAM =
63
"org.apache.commons.validator.ValidatorAction";
66
* Resources key the <code>ValidatorAction</code> is stored under.
67
* This will be automatically passed into a validation method
68
* with the current <code>ValidatorAction</code> if it is
69
* specified in the method signature.
70
* @deprecated Use VALIDATOR_ACTION_PARAM instead.
72
public static final String VALIDATOR_ACTION_KEY = VALIDATOR_ACTION_PARAM;
75
* Resources key the <code>Field</code> is stored under.
76
* This will be automatically passed into a validation method
77
* with the current <code>Field</code> if it is
78
* specified in the method signature.
80
public static final String FIELD_PARAM = "org.apache.commons.validator.Field";
83
* Resources key the <code>Field</code> is stored under.
84
* This will be automatically passed into a validation method
85
* with the current <code>Field</code> if it is
86
* specified in the method signature.
87
* @deprecated Use FIELD_PARAM instead.
89
public static final String FIELD_KEY = FIELD_PARAM;
92
* Resources key the <code>Validator</code> is stored under.
93
* This will be automatically passed into a validation method
94
* with the current <code>Validator</code> if it is
95
* specified in the method signature.
97
public static final String VALIDATOR_PARAM =
98
"org.apache.commons.validator.Validator";
101
* Resources key the <code>Validator</code> is stored under.
102
* This will be automatically passed into a validation method
103
* with the current <code>Validator</code> if it is
104
* specified in the method signature.
105
* @deprecated Use VALIDATOR_PARAM instead.
107
public static final String VALIDATOR_KEY = VALIDATOR_PARAM;
110
* Resources key the <code>Locale</code> is stored.
111
* This will be used to retrieve the appropriate
112
* <code>FormSet</code> and <code>Form</code> to be
115
public static final String LOCALE_PARAM = "java.util.Locale";
118
* Resources key the <code>Locale</code> is stored.
119
* This will be used to retrieve the appropriate
120
* <code>FormSet</code> and <code>Form</code> to be
122
* @deprecated Use LOCALE_PARAM instead.
124
public static final String LOCALE_KEY = LOCALE_PARAM;
126
protected ValidatorResources resources = null;
128
protected String formName = null;
131
* Maps validation method parameter class names to the objects to be passed
134
protected Map parameters = new HashMap();
137
* @deprecated Use parameters instead.
139
protected HashMap hResources = (HashMap) this.parameters;
142
* The current page number to validate.
144
protected int page = 0;
147
* The class loader to use for instantiating application objects.
148
* If not specified, the context class loader, or the class loader
149
* used to load Digester itself, is used, based on the value of the
150
* <code>useContextClassLoader</code> variable.
152
protected ClassLoader classLoader = null;
155
* Whether or not to use the Context ClassLoader when loading classes
156
* for instantiating new objects. Default is <code>false</code>.
158
protected boolean useContextClassLoader = false;
161
* Set this to true to not return Fields that pass validation. Only return failures.
163
protected boolean onlyReturnErrors = false;
166
* Construct a <code>Validator</code> that will
167
* use the <code>ValidatorResources</code>
168
* passed in to retrieve pluggable validators
169
* the different sets of validation rules.
171
* @param resources <code>ValidatorResources</code> to use during validation.
173
public Validator(ValidatorResources resources) {
174
this(resources, null);
178
* Construct a <code>Validator</code> that will
179
* use the <code>ValidatorResources</code>
180
* passed in to retrieve pluggable validators
181
* the different sets of validation rules.
183
* @param resources <code>ValidatorResources</code> to use during validation.
184
* @param formName Key used for retrieving the set of validation rules.
186
public Validator(ValidatorResources resources, String formName) {
187
if (resources == null) {
188
throw new IllegalArgumentException("Resources cannot be null.");
191
this.resources = resources;
192
this.formName = formName;
196
* Add a resource to be used during the processing
199
* @param parameterClassName The full class name of the parameter of the
200
* validation method that corresponds to the value/instance passed in with it.
202
* @param parameterValue The instance that will be passed into the
204
* @deprecated Use setParameter(String, Object) instead.
206
public void addResource(String parameterClassName, Object parameterValue) {
207
this.setParameter(parameterClassName, parameterValue);
211
* Set a parameter of a pluggable validation method.
213
* @param parameterClassName The full class name of the parameter of the
214
* validation method that corresponds to the value/instance passed in with it.
216
* @param parameterValue The instance that will be passed into the
219
public void setParameter(String parameterClassName, Object parameterValue) {
220
this.parameters.put(parameterClassName, parameterValue);
224
* Get a resource to be used during the processing of validations.
226
* @param parameterClassName The full class name of the parameter of the
227
* validation method that corresponds to the value/instance passed in with it.
228
* @deprecated Use getParameterValue(String) instead.
230
public Object getResource(String parameterClassName) {
231
return this.getParameterValue(parameterClassName);
235
* Returns the value of the specified parameter that will be used during the
236
* processing of validations.
238
* @param parameterClassName The full class name of the parameter of the
239
* validation method that corresponds to the value/instance passed in with it.
241
public Object getParameterValue(String parameterClassName) {
242
return this.parameters.get(parameterClassName);
246
* Gets the form name which is the key to a set of validation rules.
248
public String getFormName() {
253
* Sets the form name which is the key to a set of validation rules.
255
public void setFormName(String formName) {
256
this.formName = formName;
260
* Gets the page. This in conjunction with the page property of
261
* a <code>Field<code> can control the processing of fields. If the field's
262
* page is less than or equal to this page value, it will be processed.
264
public int getPage() {
269
* Sets the page. This in conjunction with the page property of
270
* a <code>Field<code> can control the processing of fields. If the field's page
271
* is less than or equal to this page value, it will be processed.
273
public void setPage(int page) {
278
* Clears the form name, resources that were added, and the page that was
279
* set (if any). This can be called to reinitialize the Validator instance
280
* so it can be reused. The form name (key to set of validation rules) and any
281
* resources needed, like the JavaBean being validated, will need to
282
* set and/or added to this instance again. The
283
* <code>ValidatorResources</code> will not be removed since it can be used
284
* again and is thread safe.
286
public void clear() {
287
this.formName = null;
288
this.parameters = new HashMap();
289
this.hResources = (HashMap) this.parameters;
294
* Return the boolean as to whether the context classloader should be used.
296
public boolean getUseContextClassLoader() {
297
return this.useContextClassLoader;
301
* Determine whether to use the Context ClassLoader (the one found by
302
* calling <code>Thread.currentThread().getContextClassLoader()</code>)
303
* to resolve/load classes that are defined in various rules. If not
304
* using Context ClassLoader, then the class-loading defaults to
305
* using the calling-class' ClassLoader.
307
* @param use determines whether to use Context ClassLoader.
309
public void setUseContextClassLoader(boolean use) {
310
this.useContextClassLoader = use;
314
* Return the class loader to be used for instantiating application objects
315
* when required. This is determined based upon the following rules:
317
* <li>The class loader set by <code>setClassLoader()</code>, if any</li>
318
* <li>The thread context class loader, if it exists and the
319
* <code>useContextClassLoader</code> property is set to true</li>
320
* <li>The class loader used to load the Digester class itself.
323
public ClassLoader getClassLoader() {
324
if (this.classLoader != null) {
325
return this.classLoader;
328
if (this.useContextClassLoader) {
329
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
330
if (contextLoader != null) {
331
return contextLoader;
335
return this.getClass().getClassLoader();
339
* Set the class loader to be used for instantiating application objects
342
* @param classLoader The new class loader to use, or <code>null</code>
343
* to revert to the standard rules
345
public void setClassLoader(ClassLoader classLoader) {
346
this.classLoader = classLoader;
350
* Performs validations based on the configured resources.
352
* @return The <code>Map</code> returned uses the property of the
353
* <code>Field</code> for the key and the value is the number of error the
356
public ValidatorResults validate() throws ValidatorException {
357
Locale locale = (Locale) this.getParameterValue(LOCALE_PARAM);
359
if (locale == null) {
360
locale = Locale.getDefault();
363
this.setParameter(VALIDATOR_PARAM, this);
365
Form form = this.resources.getForm(locale, this.formName);
367
return form.validate(
369
this.resources.getValidatorActions(),
373
return new ValidatorResults();
377
* Returns true if the Validator is only returning Fields that fail validation.
379
public boolean getOnlyReturnErrors() {
380
return onlyReturnErrors;
384
* Configures which Fields the Validator returns from the validate() method. Set this
385
* to true to only return Fields that failed validation. By default, validate() returns
388
public void setOnlyReturnErrors(boolean onlyReturnErrors) {
389
this.onlyReturnErrors = onlyReturnErrors;