KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DynaValidatorForm.java 164862 2005-04-26 19:08:32Z niallp $
3  *
4  * Copyright 2000-2005 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.beanutils.DynaBean;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.commons.validator.Validator;
31 import org.apache.commons.validator.ValidatorException;
32 import org.apache.commons.validator.ValidatorResults;
33 import org.apache.struts.action.ActionErrors;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.action.DynaActionForm;
36
37 /**
38  * <p>This class extends <strong>DynaActionForm</strong> and provides
39  * basic field validation based on an XML file. The key passed into the
40  * validator is the action element's 'name' attribute from the
41  * struts-config.xml which should match the form element's name attribute
42  * in the validation.xml.</p>
43  *
44  * <ul><li>See <code>ValidatorPlugin</code> definition in struts-config.xml
45  * for validation rules.</li></ul>
46  *
47  * @version $Rev: 164862 $ $Date: 2005-04-26 20:08:32 +0100 (Tue, 26 Apr 2005) $
48  * @since Struts 1.1
49  * @see org.apache.struts.action.ActionForm
50  */

51 public class DynaValidatorForm extends DynaActionForm implements DynaBean, Serializable JavaDoc {
52
53     /**
54      * Commons Logging instance.
55      */

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

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

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

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

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

96     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
97         this.setPageFromDynaProperty();
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      * Sets this.page to the value of the Dyna property "page" if it's defined. This is
133      * used to setup the page variable before validation starts.
134      * @since Struts 1.2
135      */

136     protected void setPageFromDynaProperty() {
137         Map JavaDoc props = this.getMap();
138         if (props.containsKey("page")) {
139             Integer JavaDoc p = null;
140             try {
141                 p = (Integer JavaDoc)props.get("page");
142             } catch (ClassCastException JavaDoc e) {
143                 log.error("Dyna 'page' property must be of type java.lang.Integer.", e);
144                 throw e;
145             }
146             if (p == null) {
147                 throw new NullPointerException JavaDoc("Dyna 'page' property must not be null. " +
148                     " Either provide an initial value or set 'convertNull' to false. ");
149             }
150             this.page = p.intValue();
151         }
152     }
153
154     /**
155      * Reset all properties to their default values.
156      *
157      * @param mapping The mapping used to select this instance
158      * @param request The servlet request we are processing
159      */

160     public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
161         super.reset(mapping, request);
162         page = 0;
163         validatorResults = null;
164     }
165
166     /**
167      * Get results of the validation performed by the
168      * <code>Validator</code>.
169      * @return validator results as ValidatorResults object
170      */

171     public ValidatorResults getValidatorResults() {
172         return validatorResults;
173     }
174
175     /**
176      * Set results of the validation performed by the
177      * <code>Validator</code>.
178      * @param validatorResults Set results of the validation performed
179      */

180     public void setValidatorResults(ValidatorResults validatorResults) {
181         this.validatorResults = validatorResults;
182     }
183
184     /**
185      * Returns a <code>Map</code> of values returned
186      * from any validation that returns a value other than
187      * <code>null</code> or <code>Boolean</code> with the
188      * key the full property path of the field.
189      * @return Returns a <code>Map</code> of values, otherwise returns null if no results.
190      */

191     public Map JavaDoc getResultValueMap() {
192         return (validatorResults != null ? validatorResults.getResultValueMap() : null);
193     }
194
195 }
196
197
Popular Tags