001: /*
002: * $Id: ValidatorForm.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.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.commons.validator.Validator;
026: import org.apache.commons.validator.ValidatorException;
027: import org.apache.commons.validator.ValidatorResults;
028: import org.apache.struts.action.ActionErrors;
029: import org.apache.struts.action.ActionForm;
030: import org.apache.struts.action.ActionMapping;
031:
032: import javax.servlet.ServletContext;
033: import javax.servlet.http.HttpServletRequest;
034:
035: import java.io.Serializable;
036:
037: import java.util.Map;
038:
039: /**
040: * <p>This class extends <strong>ActionForm</strong> and provides basic field
041: * validation based on an XML file. The key passed into the validator is the
042: * action element's 'name' attribute from the struts-config.xml which should
043: * match the form element's name attribute in the validation.xml.</p>
044: *
045: * <ul>
046: *
047: * <li>See <code>ValidatorPlugin</code> definition in struts-config.xml for
048: * validation rules.</li>
049: *
050: * </ul>
051: *
052: * @version $Rev: 471754 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
053: * $
054: * @see org.apache.struts.action.ActionForm
055: * @since Struts 1.1
056: */
057: public class ValidatorForm extends ActionForm implements Serializable {
058: /**
059: * Commons Logging instance.
060: */
061: private static Log log = LogFactory.getLog(ValidatorForm.class);
062:
063: /**
064: * The results returned from the validation performed by the
065: * <code>Validator</code>.
066: */
067: protected ValidatorResults validatorResults = null;
068:
069: /**
070: * Used to indicate the current page of a multi-page form.
071: */
072: protected int page = 0;
073:
074: /**
075: * Gets page.
076: *
077: * @return page number
078: */
079: public int getPage() {
080: return page;
081: }
082:
083: /**
084: * Sets page.
085: *
086: * @param page page number
087: */
088: public void setPage(int page) {
089: this .page = page;
090: }
091:
092: /**
093: * Validate the properties that have been set from this HTTP request, and
094: * return an <code>ActionErrors</code> object that encapsulates any
095: * validation errors that have been found. If no errors are found, return
096: * <code>null</code> or an <code>ActionErrors</code> object with no
097: * recorded error messages.
098: *
099: * @param mapping The mapping used to select this instance
100: * @param request The servlet request we are processing
101: * @return <code>ActionErrors</code> object that encapsulates any
102: * validation errors
103: */
104: public ActionErrors validate(ActionMapping mapping,
105: HttpServletRequest request) {
106: ServletContext application = getServlet().getServletContext();
107: ActionErrors errors = new ActionErrors();
108:
109: String validationKey = getValidationKey(mapping, request);
110:
111: Validator validator = Resources.initValidator(validationKey,
112: this , application, request, errors, page);
113:
114: try {
115: validatorResults = validator.validate();
116: } catch (ValidatorException e) {
117: log.error(e.getMessage(), e);
118: }
119:
120: return errors;
121: }
122:
123: /**
124: * Returns the Validation key.
125: *
126: * @param mapping The mapping used to select this instance
127: * @param request The servlet request we are processing
128: * @return validation key - the form element's name in this case
129: */
130: public String getValidationKey(ActionMapping mapping,
131: HttpServletRequest request) {
132: return mapping.getAttribute();
133: }
134:
135: /**
136: * Reset all properties to their default values.
137: *
138: * @param mapping The mapping used to select this instance
139: * @param request The servlet request we are processing
140: */
141: public void reset(ActionMapping mapping, HttpServletRequest request) {
142: super .reset(mapping, request);
143: page = 0;
144: validatorResults = null;
145: }
146:
147: /**
148: * Get results of the validation performed by the <code>Validator</code>.
149: *
150: * @return results of the validation
151: */
152: public ValidatorResults getValidatorResults() {
153: return validatorResults;
154: }
155:
156: /**
157: * Set results of the validation performed by the <code>Validator</code>.
158: *
159: * @param validatorResults results of validation
160: */
161: public void setValidatorResults(ValidatorResults validatorResults) {
162: this .validatorResults = validatorResults;
163: }
164:
165: /**
166: * Returns a <code>Map</code> of values returned from any validation that
167: * returns a value other than <code>null</code> or <code>Boolean</code>
168: * with the key the full property path of the field.
169: *
170: * @return <code>Map</code> of non-null values
171: */
172: public Map getResultValueMap() {
173: return ((validatorResults != null) ? validatorResults
174: .getResultValueMap() : null);
175: }
176: }
|