1 /*
2 * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Form.java,v 1.14.2.1 2004/06/22 02:24:38 husted Exp $
3 * $Revision: 1.14.2.1 $
4 * $Date: 2004/06/22 02:24:38 $
5 *
6 * ====================================================================
7 * Copyright 2001-2004 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;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
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 is
36 * contained in a list of <code>Field</code> objects. Instances of this class are
37 * configured with a <form> 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 {
45
46 /***
47 * The name/key the set of validation rules is
48 * stored under.
49 */
50 protected String name = null;
51
52 /***
53 * List of <code>Field</code>s. Used to maintain
54 * the order they were added in although individual
55 * <code>Field</code>s can be retrieved using
56 * <code>Map</code> of <code>Field</code>s.
57 */
58 protected List lFields = new ArrayList();
59
60 /***
61 * Map of <code>Field</code>s keyed on their property value.
62 */
63 protected FastHashMap hFields = new FastHashMap();
64
65 /***
66 * Gets the name/key of the set of validation rules.
67 */
68 public String getName() {
69 return name;
70 }
71
72 /***
73 * Sets the name/key of the set of validation rules.
74 */
75 public void setName(String name) {
76 this.name = name;
77 }
78
79 /***
80 * Add a <code>Field</code> to the <code>Form</code>.
81 */
82 public void addField(Field f) {
83 this.lFields.add(f);
84 this.hFields.put(f.getKey(), f);
85 }
86
87 /***
88 * A <code>List</code> of <code>Field</code>s is returned as an
89 * unmodifiable <code>List</code>.
90 */
91 public List getFields() {
92 return Collections.unmodifiableList(lFields);
93 }
94
95 /***
96 * The <code>Field</code>s are returned as an unmodifiable <code>Map</code>.
97 * @deprecated Use containsField(String) and getField(String) instead.
98 */
99 public Map getFieldMap() {
100 return Collections.unmodifiableMap(hFields);
101 }
102
103 /***
104 * Returns the Field with the given name or null if this Form has no such
105 * field.
106 * @since Validator 1.1
107 */
108 public Field getField(String fieldName) {
109 return (Field) this.hFields.get(fieldName);
110 }
111
112 /***
113 * Returns true if this Form contains a Field with the given name.
114 * @since Validator 1.1
115 */
116 public boolean containsField(String fieldName) {
117 return this.hFields.containsKey(fieldName);
118 }
119
120 /***
121 * Processes all of the <code>Form</code>'s <code>Field</code>s.
122 * @deprecated This method is called by the framework. It will be made protected
123 * in a future release. TODO
124 */
125 public void process(Map globalConstants, Map constants) {
126 hFields.setFast(true);
127
128 for (Iterator i = lFields.iterator(); i.hasNext();) {
129 Field f = (Field) i.next();
130 f.process(globalConstants, constants);
131 }
132 }
133
134 /***
135 * Returns a string representation of the object.
136 */
137 public String toString() {
138 StringBuffer results = new StringBuffer();
139
140 results.append("Form: ");
141 results.append(name);
142 results.append("\n");
143
144 for (Iterator i = lFields.iterator(); i.hasNext();) {
145 results.append("\tField: \n");
146 results.append(i.next());
147 results.append("\n");
148 }
149
150 return results.toString();
151 }
152
153 /***
154 * Validate all Fields in this Form on the given page and below.
155 * @param params A Map of parameter class names to parameter values to pass
156 * into validation methods.
157 * @param actions A Map of validator names to ValidatorAction objects.
158 * @param page Fields on pages higher than this will not be validated.
159 * @return A ValidatorResults object containing all validation messages.
160 * @throws ValidatorException
161 */
162 ValidatorResults validate(Map params, Map actions, int page)
163 throws ValidatorException {
164
165 ValidatorResults results = new ValidatorResults();
166
167 Iterator fields = this.lFields.iterator();
168 while (fields.hasNext()) {
169 Field field = (Field) fields.next();
170
171 params.put(Validator.FIELD_PARAM, field);
172
173 if (field.getPage() <= page) {
174 results.merge(field.validate(params, actions));
175 }
176 }
177
178 return results;
179 }
180
181 }
This page was automatically generated by Maven