KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > Validator


1 /*
2  * $Id: Validator.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 The Apache Software Foundation
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  */

21
22 package org.apache.commons.validator;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * Validations are processed by the validate method. An instance of
31  * <code>ValidatorResources</code> is used to define the validators
32  * (validation methods) and the validation rules for a JavaBean.
33  */

34 public class Validator implements Serializable JavaDoc {
35
36     /**
37      * Resources key the JavaBean is stored to perform validation on.
38      */

39     public static final String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc LOCALE_PARAM = "java.util.Locale";
91
92     protected ValidatorResources resources = null;
93
94     protected String JavaDoc formName = null;
95
96     /**
97      * Maps validation method parameter class names to the objects to be passed
98      * into the method.
99      */

100     protected Map JavaDoc parameters = new HashMap JavaDoc();
101
102     /**
103      * The current page number to validate.
104      */

105     protected int page = 0;
106
107     /**
108      * The class loader to use for instantiating application objects.
109      * If not specified, the context class loader, or the class loader
110      * used to load Digester itself, is used, based on the value of the
111      * <code>useContextClassLoader</code> variable.
112      */

113     protected ClassLoader JavaDoc classLoader = null;
114
115     /**
116      * Whether or not to use the Context ClassLoader when loading classes
117      * for instantiating new objects. Default is <code>false</code>.
118      */

119     protected boolean useContextClassLoader = false;
120
121     /**
122      * Set this to true to not return Fields that pass validation. Only return failures.
123      */

124     protected boolean onlyReturnErrors = false;
125
126     /**
127      * Construct a <code>Validator</code> that will
128      * use the <code>ValidatorResources</code>
129      * passed in to retrieve pluggable validators
130      * the different sets of validation rules.
131      *
132      * @param resources <code>ValidatorResources</code> to use during validation.
133      */

134     public Validator(ValidatorResources resources) {
135         this(resources, null);
136     }
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      * @param formName Key used for retrieving the set of validation rules.
146      */

147     public Validator(ValidatorResources resources, String JavaDoc formName) {
148         if (resources == null) {
149             throw new IllegalArgumentException JavaDoc("Resources cannot be null.");
150         }
151
152         this.resources = resources;
153         this.formName = formName;
154     }
155
156     /**
157      * Set a parameter of a pluggable validation method.
158      *
159      * @param parameterClassName The full class name of the parameter of the
160      * validation method that corresponds to the value/instance passed in with it.
161      *
162      * @param parameterValue The instance that will be passed into the
163      * validation method.
164      */

165     public void setParameter(String JavaDoc parameterClassName, Object JavaDoc parameterValue) {
166         this.parameters.put(parameterClassName, parameterValue);
167     }
168
169     /**
170      * Returns the value of the specified parameter that will be used during the
171      * processing of validations.
172      *
173      * @param parameterClassName The full class name of the parameter of the
174      * validation method that corresponds to the value/instance passed in with it.
175      */

176     public Object JavaDoc getParameterValue(String JavaDoc parameterClassName) {
177         return this.parameters.get(parameterClassName);
178     }
179
180     /**
181      * Gets the form name which is the key to a set of validation rules.
182      */

183     public String JavaDoc getFormName() {
184         return formName;
185     }
186
187     /**
188      * Sets the form name which is the key to a set of validation rules.
189      */

190     public void setFormName(String JavaDoc formName) {
191         this.formName = formName;
192     }
193
194     /**
195      * Gets the page. This in conjunction with the page property of
196      * a <code>Field<code> can control the processing of fields. If the field's
197      * page is less than or equal to this page value, it will be processed.
198      */

199     public int getPage() {
200         return page;
201     }
202
203     /**
204      * Sets the page. This in conjunction with the page property of
205      * a <code>Field<code> can control the processing of fields. If the field's page
206      * is less than or equal to this page value, it will be processed.
207      */

208     public void setPage(int page) {
209         this.page = page;
210     }
211
212     /**
213      * Clears the form name, resources that were added, and the page that was
214      * set (if any). This can be called to reinitialize the Validator instance
215      * so it can be reused. The form name (key to set of validation rules) and any
216      * resources needed, like the JavaBean being validated, will need to
217      * set and/or added to this instance again. The
218      * <code>ValidatorResources</code> will not be removed since it can be used
219      * again and is thread safe.
220      */

221     public void clear() {
222         this.formName = null;
223         this.parameters = new HashMap JavaDoc();
224         this.page = 0;
225     }
226
227     /**
228      * Return the boolean as to whether the context classloader should be used.
229      */

230     public boolean getUseContextClassLoader() {
231         return this.useContextClassLoader;
232     }
233
234     /**
235      * Determine whether to use the Context ClassLoader (the one found by
236      * calling <code>Thread.currentThread().getContextClassLoader()</code>)
237      * to resolve/load classes that are defined in various rules. If not
238      * using Context ClassLoader, then the class-loading defaults to
239      * using the calling-class' ClassLoader.
240      *
241      * @param use determines whether to use Context ClassLoader.
242      */

243     public void setUseContextClassLoader(boolean use) {
244         this.useContextClassLoader = use;
245     }
246
247     /**
248      * Return the class loader to be used for instantiating application objects
249      * when required. This is determined based upon the following rules:
250      * <ul>
251      * <li>The class loader set by <code>setClassLoader()</code>, if any</li>
252      * <li>The thread context class loader, if it exists and the
253      * <code>useContextClassLoader</code> property is set to true</li>
254      * <li>The class loader used to load the Digester class itself.
255      * </ul>
256      */

257     public ClassLoader JavaDoc getClassLoader() {
258         if (this.classLoader != null) {
259             return this.classLoader;
260         }
261
262         if (this.useContextClassLoader) {
263             ClassLoader JavaDoc contextLoader = Thread.currentThread().getContextClassLoader();
264             if (contextLoader != null) {
265                 return contextLoader;
266             }
267         }
268
269         return this.getClass().getClassLoader();
270     }
271
272     /**
273      * Set the class loader to be used for instantiating application objects
274      * when required.
275      *
276      * @param classLoader The new class loader to use, or <code>null</code>
277      * to revert to the standard rules
278      */

279     public void setClassLoader(ClassLoader JavaDoc classLoader) {
280         this.classLoader = classLoader;
281     }
282
283     /**
284      * Performs validations based on the configured resources.
285      *
286      * @return The <code>Map</code> returned uses the property of the
287      * <code>Field</code> for the key and the value is the number of error the
288      * field had.
289      */

290     public ValidatorResults validate() throws ValidatorException {
291         Locale JavaDoc locale = (Locale JavaDoc) this.getParameterValue(LOCALE_PARAM);
292
293         if (locale == null) {
294             locale = Locale.getDefault();
295         }
296
297         this.setParameter(VALIDATOR_PARAM, this);
298
299         Form form = this.resources.getForm(locale, this.formName);
300         if (form != null) {
301             this.setParameter(FORM_PARAM, this);
302             return form.validate(
303                 this.parameters,
304                 this.resources.getValidatorActions(),
305                 this.page);
306         }
307
308         return new ValidatorResults();
309     }
310
311     /**
312      * Returns true if the Validator is only returning Fields that fail validation.
313      */

314     public boolean getOnlyReturnErrors() {
315         return onlyReturnErrors;
316     }
317
318     /**
319      * Configures which Fields the Validator returns from the validate() method. Set this
320      * to true to only return Fields that failed validation. By default, validate() returns
321      * all fields.
322      */

323     public void setOnlyReturnErrors(boolean onlyReturnErrors) {
324         this.onlyReturnErrors = onlyReturnErrors;
325     }
326
327 }
328
Popular Tags