KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > Form


1 /*
2  * $Id: Form.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package org.apache.commons.validator;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.commons.collections.FastHashMap; // DEPRECATED
32

33 /**
34  * <p>
35  * This contains a set of validation rules for a form/JavaBean. The information
36  * is contained in a list of <code>Field</code> objects. Instances of this
37  * class are configured with a &lt;form&gt; xml element.
38  * </p>
39  * <p>
40  * The use of FastHashMap is deprecated and will be replaced in a future
41  * release.
42  * </p>
43  */

44 public class Form implements Serializable JavaDoc {
45
46     /**
47      * The name/key the set of validation rules is stored under.
48      */

49     protected String JavaDoc name = null;
50
51     /**
52      * List of <code>Field</code>s. Used to maintain
53      * the order they were added in although individual
54      * <code>Field</code>s can be retrieved using
55      * <code>Map</code> of <code>Field</code>s.
56      */

57     protected List JavaDoc lFields = new ArrayList JavaDoc();
58
59     /**
60      * Map of <code>Field</code>s keyed on their property value.
61      * @deprecated Subclasses should use getFieldMap() instead.
62      */

63     protected FastHashMap hFields = new FastHashMap();
64
65     /**
66      * The name/key of the form which this form extends from.
67      * @since Validator 1.2.0
68      */

69     protected String JavaDoc inherit = null;
70
71     /**
72      * Whether or not the this <code>Form</code> was processed
73      * for replacing variables in strings with their values.
74      */

75     private boolean processed = false;
76
77     /**
78      * Gets the name/key of the set of validation rules.
79      */

80     public String JavaDoc getName() {
81         return name;
82     }
83
84     /**
85      * Sets the name/key of the set of validation rules.
86      */

87     public void setName(String JavaDoc name) {
88         this.name = name;
89     }
90
91     /**
92      * Add a <code>Field</code> to the <code>Form</code>.
93      */

94     public void addField(Field f) {
95         this.lFields.add(f);
96         this.hFields.put(f.getKey(), f);
97     }
98
99     /**
100      * A <code>List</code> of <code>Field</code>s is returned as an
101      * unmodifiable <code>List</code>.
102      */

103     public List JavaDoc getFields() {
104         return Collections.unmodifiableList(lFields);
105     }
106
107     /**
108      * Returns the Field with the given name or null if this Form has no such
109      * field.
110      * @since Validator 1.1
111      */

112     public Field getField(String JavaDoc fieldName) {
113         return (Field) this.hFields.get(fieldName);
114     }
115
116     /**
117      * Returns true if this Form contains a Field with the given name.
118      * @since Validator 1.1
119      */

120     public boolean containsField(String JavaDoc fieldName) {
121         return this.hFields.containsKey(fieldName);
122     }
123
124     /**
125      * Processes all of the <code>Form</code>'s <code>Field</code>s.
126      * @since Validator 1.2.0
127      */

128     protected void process(Map JavaDoc globalConstants, Map JavaDoc constants, Map JavaDoc forms) {
129         if (isProcessed()) {
130             return;
131         }
132
133         int n = 0; //we want the fields from its parent first
134
if (isExtending()) {
135             Form parent = (Form) forms.get(inherit);
136             if (parent != null) {
137                 if (!parent.isProcessed()) {
138                     //we want to go all the way up the tree
139
parent.process(constants, globalConstants, forms);
140                 }
141                 for (Iterator JavaDoc i = parent.getFields().iterator(); i.hasNext();) {
142                     Field f = (Field) i.next();
143                     //we want to be able to override any fields we like
144
if (hFields.get(f.getKey()) == null) {
145                         lFields.add(n, f);
146                         hFields.put(f.getKey(), f);
147                         n++;
148                     }
149                 }
150             }
151         }
152         hFields.setFast(true);
153         //no need to reprocess parent's fields, we iterate from 'n'
154
for (Iterator JavaDoc i = lFields.listIterator(n); i.hasNext();) {
155             Field f = (Field) i.next();
156             f.process(globalConstants, constants);
157         }
158
159         processed = true;
160     }
161
162     /**
163      * Returns a string representation of the object.
164      */

165     public String JavaDoc toString() {
166         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
167
168         results.append("Form: ");
169         results.append(name);
170         results.append("\n");
171
172         for (Iterator JavaDoc i = lFields.iterator(); i.hasNext();) {
173             results.append("\tField: \n");
174             results.append(i.next());
175             results.append("\n");
176         }
177
178         return results.toString();
179     }
180
181     /**
182      * Validate all Fields in this Form on the given page and below.
183      * @param params A Map of parameter class names to parameter values to pass
184      * into validation methods.
185      * @param actions A Map of validator names to ValidatorAction objects.
186      * @param page Fields on pages higher than this will not be validated.
187      * @return A ValidatorResults object containing all validation messages.
188      * @throws ValidatorException
189      */

190     ValidatorResults validate(Map JavaDoc params, Map JavaDoc actions, int page)
191         throws ValidatorException {
192
193         ValidatorResults results = new ValidatorResults();
194         params.put(Validator.VALIDATOR_RESULTS_PARAM, results);
195
196         Iterator JavaDoc fields = this.lFields.iterator();
197         while (fields.hasNext()) {
198             Field field = (Field) fields.next();
199
200             params.put(Validator.FIELD_PARAM, field);
201
202             if (field.getPage() <= page) {
203                 results.merge(field.validate(params, actions));
204             }
205         }
206
207         return results;
208     }
209
210     /**
211      * Whether or not the this <code>Form</code> was processed
212      * for replacing variables in strings with their values.
213      * @since Validator 1.2.0
214      */

215     public boolean isProcessed() {
216         return processed;
217     }
218
219     /**
220      * Gets the name/key of the parent set of validation rules.
221      * @since Validator 1.2.0
222      */

223     public String JavaDoc getExtends() {
224         return inherit;
225     }
226
227     /**
228      * Sets the name/key of the parent set of validation rules.
229      * @since Validator 1.2.0
230      */

231     public void setExtends(String JavaDoc inherit) {
232         this.inherit = inherit;
233     }
234
235     /**
236      * Get extends flag.
237      * @since Validator 1.2.0
238      */

239     public boolean isExtending() {
240         return inherit != null;
241     }
242
243     /**
244      * Returns a Map of String field keys to Field objects.
245      * @since Validator 1.2.0
246      */

247     protected Map JavaDoc getFieldMap() {
248         return hFields;
249     }
250 }
Popular Tags