001: /*
002: * $Id: DynaValidatorForm.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.validator;
022:
023: import org.apache.commons.beanutils.DynaBean;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.commons.validator.Validator;
027: import org.apache.commons.validator.ValidatorException;
028: import org.apache.commons.validator.ValidatorResults;
029: import org.apache.struts.action.ActionErrors;
030: import org.apache.struts.action.ActionMapping;
031: import org.apache.struts.action.DynaActionForm;
032:
033: import javax.servlet.ServletContext;
034: import javax.servlet.http.HttpServletRequest;
035:
036: import java.io.Serializable;
037:
038: import java.util.Map;
039:
040: /**
041: * <p>This class extends <strong>DynaActionForm</strong> and provides basic
042: * field validation based on an XML file. The key passed into the validator
043: * is the action element's 'name' attribute from the struts-config.xml which
044: * should match the form element's name attribute in the validation.xml.</p>
045: *
046: * <ul>
047: *
048: * <li>See <code>ValidatorPlugin</code> definition in struts-config.xml for
049: * validation rules.</li>
050: *
051: * </ul>
052: *
053: * @version $Rev: 471754 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
054: * $
055: * @see org.apache.struts.action.ActionForm
056: * @since Struts 1.1
057: */
058: public class DynaValidatorForm extends DynaActionForm implements
059: DynaBean, Serializable {
060: /**
061: * Commons Logging instance.
062: */
063: private static Log log = LogFactory.getLog(DynaValidatorForm.class);
064:
065: /**
066: * The results returned from the validation performed by the
067: * <code>Validator</code>.
068: */
069: protected ValidatorResults validatorResults = null;
070:
071: /**
072: * Used to indicate the current page of a multi-page form.
073: */
074: protected int page = 0;
075:
076: /**
077: * Gets page.
078: *
079: * @return page number.
080: */
081: public int getPage() {
082: return page;
083: }
084:
085: /**
086: * Sets page.
087: *
088: * @param page page number
089: */
090: public void setPage(int page) {
091: this .page = page;
092: }
093:
094: /**
095: * Validate the properties that have been set from this HTTP request, and
096: * return an <code>ActionErrors</code> object that encapsulates any
097: * validation errors that have been found. If no errors are found, return
098: * <code>null</code> or an <code>ActionErrors</code> object with no
099: * recorded error messages.
100: *
101: * @param mapping The mapping used to select this instance.
102: * @param request The servlet request we are processing.
103: * @return <code>ActionErrors</code> object that encapsulates any
104: * validation errors.
105: */
106: public ActionErrors validate(ActionMapping mapping,
107: HttpServletRequest request) {
108: this .setPageFromDynaProperty();
109:
110: ServletContext application = getServlet().getServletContext();
111: ActionErrors errors = new ActionErrors();
112:
113: String validationKey = getValidationKey(mapping, request);
114:
115: Validator validator = Resources.initValidator(validationKey,
116: this , application, request, errors, page);
117:
118: try {
119: validatorResults = validator.validate();
120: } catch (ValidatorException e) {
121: log.error(e.getMessage(), e);
122: }
123:
124: return errors;
125: }
126:
127: /**
128: * Returns the Validation key.
129: *
130: * @param mapping The mapping used to select this instance
131: * @param request The servlet request we are processing
132: * @return validation key - the form element's name in this case
133: */
134: public String getValidationKey(ActionMapping mapping,
135: HttpServletRequest request) {
136: return mapping.getAttribute();
137: }
138:
139: /**
140: * Sets this.page to the value of the Dyna property "page" if it's
141: * defined. This is used to setup the page variable before validation
142: * starts.
143: *
144: * @since Struts 1.2
145: */
146: protected void setPageFromDynaProperty() {
147: Map props = this .getMap();
148:
149: if (props.containsKey("page")) {
150: Integer p = null;
151:
152: try {
153: p = (Integer) props.get("page");
154: } catch (ClassCastException e) {
155: log
156: .error(
157: "Dyna 'page' property must be of type java.lang.Integer.",
158: e);
159: throw e;
160: }
161:
162: if (p == null) {
163: throw new NullPointerException(
164: "Dyna 'page' property must not be null. "
165: + " Either provide an initial value or set 'convertNull' to false. ");
166: }
167:
168: this .page = p.intValue();
169: }
170: }
171:
172: /**
173: * Reset all properties to their default values.
174: *
175: * @param mapping The mapping used to select this instance
176: * @param request The servlet request we are processing
177: */
178: public void reset(ActionMapping mapping, HttpServletRequest request) {
179: super .reset(mapping, request);
180: page = 0;
181: validatorResults = null;
182: }
183:
184: /**
185: * Get results of the validation performed by the <code>Validator</code>.
186: *
187: * @return validator results as ValidatorResults object
188: */
189: public ValidatorResults getValidatorResults() {
190: return validatorResults;
191: }
192:
193: /**
194: * Set results of the validation performed by the <code>Validator</code>.
195: *
196: * @param validatorResults Set results of the validation performed
197: */
198: public void setValidatorResults(ValidatorResults validatorResults) {
199: this .validatorResults = validatorResults;
200: }
201:
202: /**
203: * Returns a <code>Map</code> of values returned from any validation that
204: * returns a value other than <code>null</code> or <code>Boolean</code>
205: * with the key the full property path of the field.
206: *
207: * @return Returns a <code>Map</code> of values, otherwise returns null if
208: * no results.
209: */
210: public Map getResultValueMap() {
211: return ((validatorResults != null) ? validatorResults
212: .getResultValueMap() : null);
213: }
214: }
|