~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/share/org/apache/struts/validator/DynaValidatorForm.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/validator/DynaValidatorForm.java,v 1.16 2004/04/02 14:30:57 germuska Exp $
 
3
 * $Revision: 1.16 $
 
4
 * $Date: 2004/04/02 14:30:57 $
 
5
 *
 
6
 * Copyright 2000-2004 The Apache Software Foundation.
 
7
 * 
 
8
 * Licensed under the Apache License, Version 2.0 (the "License");
 
9
 * you may not use this file except in compliance with the License.
 
10
 * You may obtain a copy of the License at
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
14
 * Unless required by applicable law or agreed to in writing, software
 
15
 * distributed under the License is distributed on an "AS IS" BASIS,
 
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
17
 * See the License for the specific language governing permissions and
 
18
 * limitations under the License.
 
19
 */
 
20
 
 
21
package org.apache.struts.validator;
 
22
 
 
23
import java.io.Serializable;
 
24
import java.util.Map;
 
25
 
 
26
import javax.servlet.ServletContext;
 
27
import javax.servlet.http.HttpServletRequest;
 
28
 
 
29
import org.apache.commons.beanutils.DynaBean;
 
30
import org.apache.commons.logging.Log;
 
31
import org.apache.commons.logging.LogFactory;
 
32
import org.apache.commons.validator.Validator;
 
33
import org.apache.commons.validator.ValidatorException;
 
34
import org.apache.commons.validator.ValidatorResults;
 
35
import org.apache.struts.action.ActionErrors;
 
36
import org.apache.struts.action.ActionMapping;
 
37
import org.apache.struts.action.DynaActionForm;
 
38
 
 
39
/**
 
40
 * <p>This class extends <strong>DynaActionForm</strong> and provides
 
41
 * basic field validation based on an XML file.  The key passed into the
 
42
 * validator is the action element's 'name' attribute from the
 
43
 * struts-config.xml which should match the form element's name attribute
 
44
 * in the validation.xml.</p>
 
45
 *
 
46
 * <ul><li>See <code>ValidatorPlugin</code> definition in struts-config.xml
 
47
 * for validation rules.</li></ul>
 
48
 *
 
49
 * @version $Revision: 1.16 $ $Date: 2004/04/02 14:30:57 $
 
50
 * @since Struts 1.1
 
51
 * @see org.apache.struts.action.ActionForm
 
52
 */
 
53
public class DynaValidatorForm extends DynaActionForm implements DynaBean, Serializable {
 
54
 
 
55
    /**
 
56
     * Commons Logging instance.
 
57
     */
 
58
    private static Log log = LogFactory.getLog(DynaValidatorForm.class);
 
59
 
 
60
    /**
 
61
     * The results returned from the validation performed
 
62
     * by the <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
     * @return page number.
 
74
     */
 
75
    public int getPage() {
 
76
        return page;
 
77
    }
 
78
 
 
79
    /**
 
80
     * Sets page.
 
81
     * @param page page number
 
82
     */
 
83
    public void setPage(int page) {
 
84
        this.page = page;
 
85
    }
 
86
 
 
87
    /**
 
88
     * Validate the properties that have been set from this HTTP request,
 
89
     * and return an <code>ActionErrors</code> object that encapsulates any
 
90
     * validation errors that have been found.  If no errors are found, return
 
91
     * <code>null</code> or an <code>ActionErrors</code> object with no
 
92
     * recorded error messages.
 
93
     *
 
94
     * @param mapping The mapping used to select this instance.
 
95
     * @param request The servlet request we are processing.
 
96
     * @return <code>ActionErrors</code> object that encapsulates any validation errors.
 
97
     */
 
98
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
 
99
        this.setPageFromDynaProperty();
 
100
 
 
101
        ServletContext application = getServlet().getServletContext();
 
102
        ActionErrors errors = new ActionErrors();
 
103
 
 
104
        String validationKey = getValidationKey(mapping, request);
 
105
 
 
106
        Validator validator = Resources.initValidator(validationKey,
 
107
                             this,
 
108
                             application, request,
 
109
                             errors, page);
 
110
 
 
111
        try {
 
112
            validatorResults = validator.validate();
 
113
        } catch (ValidatorException e) {
 
114
            log.error(e.getMessage(), e);
 
115
        }
 
116
 
 
117
        return errors;
 
118
    }
 
119
 
 
120
    /**
 
121
     * Returns the Validation key.
 
122
     *
 
123
     * @param mapping The mapping used to select this instance
 
124
     * @param request The servlet request we are processing
 
125
     * @return validation key - the form element's name in this case
 
126
     */
 
127
    public String getValidationKey(ActionMapping mapping,
 
128
                                   HttpServletRequest request) {
 
129
 
 
130
        return mapping.getAttribute();
 
131
    }
 
132
 
 
133
    /**
 
134
     * Sets this.page to the value of the Dyna property "page" if it's defined.  This is
 
135
     * used to setup the page variable before validation starts.
 
136
     * @since Struts 1.2
 
137
     */
 
138
    protected void setPageFromDynaProperty() {
 
139
        Map props = this.getMap();
 
140
        if (props.containsKey("page")) {
 
141
            try {
 
142
                this.page = ((Integer) props.get("page")).intValue();
 
143
 
 
144
            } catch (ClassCastException e) {
 
145
                log.error("Dyna 'page' property must be of type java.lang.Integer.", e);
 
146
                throw e;
 
147
            }
 
148
        }
 
149
    }
 
150
 
 
151
    /**
 
152
     * Reset all properties to their default values.
 
153
     *
 
154
     * @param mapping The mapping used to select this instance
 
155
     * @param request The servlet request we are processing
 
156
     */
 
157
    public void reset(ActionMapping mapping, HttpServletRequest request) {
 
158
        super.reset(mapping, request);
 
159
        page = 0;
 
160
        validatorResults = null;
 
161
    }
 
162
 
 
163
    /**
 
164
     * Get results of the validation performed by the
 
165
     * <code>Validator</code>.
 
166
     * @return validator results as ValidatorResults object
 
167
     */
 
168
    public ValidatorResults getValidatorResults() {
 
169
        return validatorResults;
 
170
    }
 
171
 
 
172
    /**
 
173
     * Set results of the validation performed by the
 
174
     * <code>Validator</code>.
 
175
     * @param validatorResults  Set results of the validation performed
 
176
     */
 
177
    public void setValidatorResults(ValidatorResults validatorResults) {
 
178
        this.validatorResults = validatorResults;
 
179
    }
 
180
 
 
181
    /**
 
182
     * Returns a <code>Map</code> of values returned
 
183
     * from any validation that returns a value other than
 
184
     * <code>null</code> or <code>Boolean</code> with the
 
185
     * key the full property path of the field.
 
186
     * @return Returns a <code>Map</code> of values, otherwise returns null if no results.
 
187
     */
 
188
    public Map getResultValueMap() {
 
189
        return (validatorResults != null ? validatorResults.getResultValueMap() : null);
 
190
    }
 
191
 
 
192
}
 
193