View Javadoc

1   /*
2    * $Id: ValidatorForm.java 421119 2006-07-12 04:49:11Z wsmoak $
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  package org.apache.struts.validator;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.commons.validator.Validator;
23  import org.apache.commons.validator.ValidatorException;
24  import org.apache.commons.validator.ValidatorResults;
25  import org.apache.struts.action.ActionErrors;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionMapping;
28  
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.HttpServletRequest;
31  
32  import java.io.Serializable;
33  
34  import java.util.Map;
35  
36  /***
37   * <p>This class extends <strong>ActionForm</strong> and provides basic field
38   * validation based on an XML file.  The key passed into the validator is the
39   * action element's 'name' attribute from the struts-config.xml which should
40   * match the form element's name attribute in the validation.xml.</p>
41   *
42   * <ul>
43   *
44   * <li>See <code>ValidatorPlugin</code> definition in struts-config.xml for
45   * validation rules.</li>
46   *
47   * </ul>
48   *
49   * @version $Rev: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
50   *          $
51   * @see org.apache.struts.action.ActionForm
52   * @since Struts 1.1
53   */
54  public class ValidatorForm extends ActionForm implements Serializable {
55      /***
56       * Commons Logging instance.
57       */
58      private static Log log = LogFactory.getLog(ValidatorForm.class);
59  
60      /***
61       * The results returned from the validation performed by the
62       * <code>Validator</code>.
63       */
64      protected ValidatorResults validatorResults = null;
65  
66      /***
67       * Used to indicate the current page of a multi-page form.
68       */
69      protected int page = 0;
70  
71      /***
72       * Gets page.
73       *
74       * @return page number
75       */
76      public int getPage() {
77          return page;
78      }
79  
80      /***
81       * Sets page.
82       *
83       * @param page page number
84       */
85      public void setPage(int page) {
86          this.page = page;
87      }
88  
89      /***
90       * Validate the properties that have been set from this HTTP request, and
91       * return an <code>ActionErrors</code> object that encapsulates any
92       * validation errors that have been found.  If no errors are found, return
93       * <code>null</code> or an <code>ActionErrors</code> object with no
94       * recorded error messages.
95       *
96       * @param mapping The mapping used to select this instance
97       * @param request The servlet request we are processing
98       * @return <code>ActionErrors</code> object that encapsulates any
99       *         validation errors
100      */
101     public ActionErrors validate(ActionMapping mapping,
102         HttpServletRequest request) {
103         ServletContext application = getServlet().getServletContext();
104         ActionErrors errors = new ActionErrors();
105 
106         String validationKey = getValidationKey(mapping, request);
107 
108         Validator validator =
109             Resources.initValidator(validationKey, this, application, request,
110                 errors, page);
111 
112         try {
113             validatorResults = validator.validate();
114         } catch (ValidatorException e) {
115             log.error(e.getMessage(), e);
116         }
117 
118         return errors;
119     }
120 
121     /***
122      * Returns the Validation key.
123      *
124      * @param mapping The mapping used to select this instance
125      * @param request The servlet request we are processing
126      * @return validation key - the form element's name in this case
127      */
128     public String getValidationKey(ActionMapping mapping,
129         HttpServletRequest request) {
130         return mapping.getAttribute();
131     }
132 
133     /***
134      * Reset all properties to their default values.
135      *
136      * @param mapping The mapping used to select this instance
137      * @param request The servlet request we are processing
138      */
139     public void reset(ActionMapping mapping, HttpServletRequest request) {
140         super.reset(mapping, request);
141         page = 0;
142         validatorResults = null;
143     }
144 
145     /***
146      * Get results of the validation performed by the <code>Validator</code>.
147      *
148      * @return results of the validation
149      */
150     public ValidatorResults getValidatorResults() {
151         return validatorResults;
152     }
153 
154     /***
155      * Set results of the validation performed by the <code>Validator</code>.
156      *
157      * @param validatorResults results of validation
158      */
159     public void setValidatorResults(ValidatorResults validatorResults) {
160         this.validatorResults = validatorResults;
161     }
162 
163     /***
164      * Returns a <code>Map</code> of values returned from any validation that
165      * returns a value other than <code>null</code> or <code>Boolean</code>
166      * with the key the full property path of the field.
167      *
168      * @return <code>Map</code> of non-null values
169      */
170     public Map getResultValueMap() {
171         return ((validatorResults != null)
172         ? validatorResults.getResultValueMap() : null);
173     }
174 }