KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > validator > ValidatorForm


1 /*
2  * $Id: ValidatorForm.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2000-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.validator;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.commons.validator.Validator;
30 import org.apache.commons.validator.ValidatorException;
31 import org.apache.commons.validator.ValidatorResults;
32 import org.apache.struts.action.ActionErrors;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionMapping;
35
36 /**
37  * <p>This class extends <strong>ActionForm</strong> and provides
38  * basic field validation based on an XML file. The key passed into the
39  * validator is the action element's 'name' attribute from the
40  * struts-config.xml which should match the form element's name attribute
41  * in the validation.xml.</p>
42  *
43  * <ul><li>See <code>ValidatorPlugin</code> definition in struts-config.xml
44  * for validation rules.</li></ul>
45  *
46  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
47  * @see org.apache.struts.action.ActionForm
48  * @since Struts 1.1
49  */

50 public class ValidatorForm extends ActionForm implements Serializable JavaDoc {
51
52     /**
53      * Commons Logging instance.
54      */

55     private static Log log = LogFactory.getLog(ValidatorForm.class);
56
57     /**
58      * The results returned from the validation performed
59      * by the <code>Validator</code>.
60      */

61     protected ValidatorResults validatorResults = null;
62
63     /**
64      * Used to indicate the current page of a multi-page form.
65      */

66     protected int page = 0;
67
68     /**
69      * Gets page.
70      * @return page number
71      */

72     public int getPage() {
73         return page;
74     }
75
76     /**
77      * Sets page.
78      * @param page page number
79      */

80     public void setPage(int page) {
81         this.page = page;
82     }
83
84     /**
85      * Validate the properties that have been set from this HTTP request,
86      * and return an <code>ActionErrors</code> object that encapsulates any
87      * validation errors that have been found. If no errors are found, return
88      * <code>null</code> or an <code>ActionErrors</code> object with no
89      * recorded error messages.
90      *
91      * @param mapping The mapping used to select this instance
92      * @param request The servlet request we are processing
93      * @return <code>ActionErrors</code> object that encapsulates any validation errors
94
95      */

96     public ActionErrors validate(ActionMapping mapping,
97                                  HttpServletRequest JavaDoc request) {
98
99         ServletContext JavaDoc application = getServlet().getServletContext();
100         ActionErrors errors = new ActionErrors();
101
102         String JavaDoc validationKey = getValidationKey(mapping, request);
103
104         Validator validator = Resources.initValidator(validationKey,
105                              this,
106                              application, request,
107                              errors, page);
108
109         try {
110             validatorResults = validator.validate();
111         } catch (ValidatorException e) {
112             log.error(e.getMessage(), e);
113         }
114
115         return errors;
116     }
117
118     /**
119      * Returns the Validation key.
120      *
121      * @param mapping The mapping used to select this instance
122      * @param request The servlet request we are processing
123      * @return validation key - the form element's name in this case
124      */

125     public String JavaDoc getValidationKey(ActionMapping mapping,
126                                    HttpServletRequest JavaDoc request) {
127
128         return mapping.getAttribute();
129     }
130
131     /**
132      * Reset all properties to their default values.
133      *
134      * @param mapping The mapping used to select this instance
135      * @param request The servlet request we are processing
136      */

137     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
138         super.reset(mapping, request);
139         page = 0;
140         validatorResults = null;
141     }
142
143     /**
144      * Get results of the validation performed by the
145      * <code>Validator</code>.
146      * @return results of the validation
147      */

148     public ValidatorResults getValidatorResults() {
149         return validatorResults;
150     }
151
152     /**
153      * Set results of the validation performed by the
154      * <code>Validator</code>.
155      * @param validatorResults results of validation
156      */

157     public void setValidatorResults(ValidatorResults validatorResults) {
158         this.validatorResults = validatorResults;
159     }
160
161     /**
162      * Returns a <code>Map</code> of values returned
163      * from any validation that returns a value other than
164      * <code>null</code> or <code>Boolean</code> with the
165      * key the full property path of the field.
166      * @return <code>Map</code> of non-null values
167      */

168     public Map JavaDoc getResultValueMap() {
169         return (validatorResults != null ? validatorResults.getResultValueMap() : null);
170     }
171 }
172
Popular Tags