1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.validator;
19
20 import org.apache.commons.beanutils.DynaBean;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.commons.validator.Validator;
24 import org.apache.commons.validator.ValidatorException;
25 import org.apache.commons.validator.ValidatorResults;
26 import org.apache.struts.action.ActionErrors;
27 import org.apache.struts.action.ActionMapping;
28 import org.apache.struts.action.DynaActionForm;
29
30 import javax.servlet.ServletContext;
31 import javax.servlet.http.HttpServletRequest;
32
33 import java.io.Serializable;
34
35 import java.util.Map;
36
37 /***
38 * <p>This class extends <strong>DynaActionForm</strong> and provides basic
39 * field validation based on an XML file. The key passed into the validator
40 * is the action element's 'name' attribute from the struts-config.xml which
41 * should match the form element's name attribute in the validation.xml.</p>
42 *
43 * <ul>
44 *
45 * <li>See <code>ValidatorPlugin</code> definition in struts-config.xml for
46 * validation rules.</li>
47 *
48 * </ul>
49 *
50 * @version $Rev: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
51 * $
52 * @see org.apache.struts.action.ActionForm
53 * @since Struts 1.1
54 */
55 public class DynaValidatorForm extends DynaActionForm implements DynaBean,
56 Serializable {
57 /***
58 * Commons Logging instance.
59 */
60 private static Log log = LogFactory.getLog(DynaValidatorForm.class);
61
62 /***
63 * The results returned from the validation performed by the
64 * <code>Validator</code>.
65 */
66 protected ValidatorResults validatorResults = null;
67
68 /***
69 * Used to indicate the current page of a multi-page form.
70 */
71 protected int page = 0;
72
73 /***
74 * Gets page.
75 *
76 * @return page number.
77 */
78 public int getPage() {
79 return page;
80 }
81
82 /***
83 * Sets page.
84 *
85 * @param page page number
86 */
87 public void setPage(int page) {
88 this.page = page;
89 }
90
91 /***
92 * Validate the properties that have been set from this HTTP request, and
93 * return an <code>ActionErrors</code> object that encapsulates any
94 * validation errors that have been found. If no errors are found, return
95 * <code>null</code> or an <code>ActionErrors</code> object with no
96 * recorded error messages.
97 *
98 * @param mapping The mapping used to select this instance.
99 * @param request The servlet request we are processing.
100 * @return <code>ActionErrors</code> object that encapsulates any
101 * validation errors.
102 */
103 public ActionErrors validate(ActionMapping mapping,
104 HttpServletRequest request) {
105 this.setPageFromDynaProperty();
106
107 ServletContext application = getServlet().getServletContext();
108 ActionErrors errors = new ActionErrors();
109
110 String validationKey = getValidationKey(mapping, request);
111
112 Validator validator =
113 Resources.initValidator(validationKey, this, application, request,
114 errors, page);
115
116 try {
117 validatorResults = validator.validate();
118 } catch (ValidatorException e) {
119 log.error(e.getMessage(), e);
120 }
121
122 return errors;
123 }
124
125 /***
126 * Returns the Validation key.
127 *
128 * @param mapping The mapping used to select this instance
129 * @param request The servlet request we are processing
130 * @return validation key - the form element's name in this case
131 */
132 public String getValidationKey(ActionMapping mapping,
133 HttpServletRequest request) {
134 return mapping.getAttribute();
135 }
136
137 /***
138 * Sets this.page to the value of the Dyna property "page" if it's
139 * defined. This is used to setup the page variable before validation
140 * starts.
141 *
142 * @since Struts 1.2
143 */
144 protected void setPageFromDynaProperty() {
145 Map props = this.getMap();
146
147 if (props.containsKey("page")) {
148 Integer p = null;
149
150 try {
151 p = (Integer) props.get("page");
152 } catch (ClassCastException e) {
153 log.error("Dyna 'page' property must be of type java.lang.Integer.",
154 e);
155 throw e;
156 }
157
158 if (p == null) {
159 throw new NullPointerException(
160 "Dyna 'page' property must not be null. "
161 + " Either provide an initial value or set 'convertNull' to false. ");
162 }
163
164 this.page = p.intValue();
165 }
166 }
167
168 /***
169 * Reset all properties to their default values.
170 *
171 * @param mapping The mapping used to select this instance
172 * @param request The servlet request we are processing
173 */
174 public void reset(ActionMapping mapping, HttpServletRequest request) {
175 super.reset(mapping, request);
176 page = 0;
177 validatorResults = null;
178 }
179
180 /***
181 * Get results of the validation performed by the <code>Validator</code>.
182 *
183 * @return validator results as ValidatorResults object
184 */
185 public ValidatorResults getValidatorResults() {
186 return validatorResults;
187 }
188
189 /***
190 * Set results of the validation performed by the <code>Validator</code>.
191 *
192 * @param validatorResults Set results of the validation performed
193 */
194 public void setValidatorResults(ValidatorResults validatorResults) {
195 this.validatorResults = validatorResults;
196 }
197
198 /***
199 * Returns a <code>Map</code> of values returned from any validation that
200 * returns a value other than <code>null</code> or <code>Boolean</code>
201 * with the key the full property path of the field.
202 *
203 * @return Returns a <code>Map</code> of values, otherwise returns null if
204 * no results.
205 */
206 public Map getResultValueMap() {
207 return ((validatorResults != null)
208 ? validatorResults.getResultValueMap() : null);
209 }
210 }