KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > DefaultFormValidator


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DefaultFormValidator.java,v 1.15 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms;
21
22 import java.util.*;
23
24 import org.enhydra.barracuda.plankton.*;
25 import org.apache.log4j.*;
26
27 /**
28  * <p>This class provides the default implementation of FormValidator.
29  *
30  * <p>A FormValidator is designed to validate one or more FormElements
31  * within a FormMap, OR a FormMap itself. The real work occurs in the
32  * validate() method. A validator is also capable of containing other
33  * validators, which makes it very easy to create a complex set of
34  * validation logic by assembling a "master" validator from a set of
35  * simpler validators.
36  *
37  * <p>When the validate method is invoked, we start by performing element
38  * level validation by invoking the validateFormElement() methods for each
39  * of the elements in the form. If that succeeds, we validate the whole
40  * map by invoking validateForm() method to validate the form itself. Finally,
41  * we invoke any child validators. In this way, a validator is not considered
42  * validated until all it's rules plus those for all of it's children have
43  * passed muster.
44  *
45  * <p>When an invalid condition occurs, typically the developer will throw
46  * a ValidationException, which stops the validation process immediately and
47  * returns the specified error. There are times, however, where the developer
48  * will want to try and validate as many of the rules as possible in order
49  * to return all known errors at once. In this case, the developer should throw
50  * DeferredValidationExceptions. These are caught internally and added to one
51  * master validation exception, which is thrown when the validation process
52  * completes.
53  *
54  * <p>In typical usage the developer would extend this class to
55  * override one of the three validateFormElement methods.
56  * FormValidators can also act as containers for other FormValidators,
57  * thereby allowing the developer to dynamically assemble complex
58  * validators from simpler validator objects.
59  */

60 public class DefaultFormValidator extends AbstractFormValidator {
61
62     protected static final Logger localLogger = Logger.getLogger(AbstractFormValidator.class.getName());
63
64     protected List validators = new ArrayList();
65
66     /**
67      * Public noargs constructor (errorMessage defaults to null)
68      */

69     public DefaultFormValidator() {
70         this(null);
71     }
72
73     /**
74      * Public constructor with a default errorMessage to be dispatched
75      * when an invalid state occurs. The deferExceptions parameter
76      * defaults to true.
77      *
78      * @param ierrorMessage the default error message for this validator
79      */

80     public DefaultFormValidator(String JavaDoc ierrorMessage) {
81         setErrorMessage(ierrorMessage);
82     }
83
84     /**
85      * Add a child validator
86      *
87      * @param validator the child validator to be added
88      */

89     public void addValidator(FormValidator validator) {
90         validators.add(validator);
91     }
92
93     /**
94      * Remove a child validator
95      *
96      * @param validator the child validator to be removed
97      */

98     public void removeValidator(FormValidator validator) {
99         validators.remove(validator);
100     }
101
102     /**
103      * Get a list of all child validators
104      *
105      * @return a list of all child validators
106      */

107     public List getValidators() {
108         return new ArrayList(validators);
109     }
110
111     /**
112      * Validate a FormElement locally and allow any child
113      * validators a chance to validate as well.
114      *
115      * @param element the form element to be validated (null indicates
116      * we want to perform form level validation)
117      * @param map the map to which the element belongs (sometimes necessary
118      * to validate elements by comparing them with other elements)
119      * @param deferExceptions do we want to deferValidation exceptions
120      * and attempt to validate all elements so that we can process
121      * all the exceptions at once
122      * @throws ValidationException if the element is not valid
123      */

124     public void validate(FormElement element, FormMap map, boolean deferExceptions) throws ValidationException {
125         ValidationException ve = null;
126         if (localLogger.isInfoEnabled()) localLogger.info("Validating form");
127
128         //perform local form validation
129
try {
130             //perform specific element validation (only occurs
131
//when we're looking at a specific element)
132
if (element!=null) {
133                 if (localLogger.isDebugEnabled()) localLogger.debug("Validating local form elements:"+element);
134                 // ilc:02-20-2000 Begin changes
135
// make sure validation takes place on the OrigVal
136
// Object val = element.getVal();
137
Object JavaDoc val = element.getOrigVal();
138                 // ilc:02-20-2000 End changes
139

140                 validateFormElement(val, element, deferExceptions);
141                 validateFormElement(val, element, map, deferExceptions);
142
143             }
144
145             //perform form level validation (only occurs
146
//when we're NOT looking at a specific element)
147
if (element==null) {
148                 if (localLogger.isDebugEnabled()) localLogger.debug("Validating form:"+map);
149                 validateForm(map, deferExceptions);
150             }
151         } catch (DeferredValidationException dve) {
152             if (localLogger.isDebugEnabled()) localLogger.debug("Deferring Validation Exception", dve);
153             if (ve==null) ve = new DeferredValidationException();
154             ve.addSubException(dve);
155         }
156
157         //give any child validators a chance to validate it as well
158
Iterator it = validators.iterator();
159         while (it.hasNext()) {
160             FormValidator fv = (FormValidator) it.next();
161             if (localLogger.isDebugEnabled()) localLogger.debug("Validating child:"+fv);
162             try {fv.validate(element, map, deferExceptions);}
163             catch (DeferredValidationException dve) {
164                 if (localLogger.isDebugEnabled()) localLogger.debug("Deferring Child Validation Exception:", dve);
165                 if (ve==null) ve = new DeferredValidationException();
166                 ve.addSubException(dve);
167             }
168         }
169
170         //now, if we have generated a ValidationExceptions,
171
//rethrow it
172
if (ve!=null) throw ve;
173     }
174
175     /**
176      * Validate an entire FormMap. This is the method developers should
177      * override to provide specific validation for the entire form, as
178      * opposed to validating a specific element within the form.
179      *
180      * To indicate a form is invalid, through a ValidationException,
181      * which will interrupt the validation process immediately. If you
182      * want to indicate an error, but would still like validation to
183      * continue (so that you can identify multiple errors in one
184      * validation pass) throw a DeferredValidationException instead.
185      *
186      * @param map the map to be validated
187      * @param deferExceptions do we want to deferValidation exceptions
188      * and attempt to validate all elements so that we can process
189      * all the exceptions at once
190      * @throws ValidationException if the element is not valid
191      */

192     public void validateForm(FormMap map, boolean deferExceptions) throws ValidationException {
193         //override this if you want to validate an entire form
194
}
195
196     /**
197      * Validate a FormElement. This is the method developers should
198      * override to provide specific validation based on the value
199      * and (potentially) information contained in the FormElement.
200      *
201      * To indicate an element is invalid, through a ValidationException,
202      * which will interrupt the validation process immediately. If you
203      * want to indicate an error, but would still like validation to
204      * continue (so that you can identify multiple errors in one
205      * validation pass) throw a DeferredValidationException instead.
206      *
207      * @param val the actual value to be validated
208      * @param element the form element that contains the val
209      * to validate elements by comparing them with other elements)
210      * @param deferExceptions do we want to deferValidation exceptions
211      * and attempt to validate all elements so that we can process
212      * all the exceptions at once
213      * @throws ValidationException if the element is not valid
214      */

215     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
216         //override one of these two methods to validate a specific element
217
}
218
219     /**
220      * Validate a FormElement. This is the method developers should
221      * override to provide specific validation based on the value
222      * and (potentially) information contained in the FormElement and
223      * FormMap structures.
224      *
225      * To indicate an element is invalid, through a ValidationException,
226      * which will interrupt the validation process immediately. If you
227      * want to indicate an error, but would still like validation to
228      * continue (so that you can identify multiple errors in one
229      * validation pass) throw a DeferredValidationException instead.
230      *
231      * @param val the actual value to be validated
232      * @param element the form element that contains the val
233      * @param map the map to which the element belongs (sometimes necessary
234      * to validate elements by comparing them with other elements)
235      * @param deferExceptions do we want to deferValidation exceptions
236      * and attempt to validate all elements so that we can process
237      * all the exceptions at once
238      * @throws ValidationException if the element is not valid
239      */

240     public void validateFormElement(Object JavaDoc val, FormElement element, FormMap map, boolean deferExceptions) throws ValidationException {
241         //override one of these two methods to validate a specific element
242
}
243
244
245 }
246
Popular Tags