1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.collections.FastHashMap;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class Form implements Serializable {
41
42 private static final long serialVersionUID = 6445211789563796371L;
43
44
45 protected String name;
46
47
48
49
50
51
52 protected List<Field> lFields = new ArrayList<>();
53
54
55
56
57
58
59 @Deprecated
60 protected FastHashMap hFields = new FastHashMap();
61
62
63
64
65
66
67 protected String inherit;
68
69
70
71
72
73 private boolean processed;
74
75
76
77
78
79
80 public void addField(final Field f) {
81 this.lFields.add(f);
82 getFieldMap().put(f.getKey(), f);
83 }
84
85
86
87
88
89
90
91
92 public boolean containsField(final String fieldName) {
93 return getFieldMap().containsKey(fieldName);
94 }
95
96
97
98
99
100
101
102 public String getExtends() {
103 return inherit;
104 }
105
106
107
108
109
110
111
112
113
114 public Field getField(final String fieldName) {
115 return getFieldMap().get(fieldName);
116 }
117
118
119
120
121
122
123
124 @SuppressWarnings("unchecked")
125 protected Map<String, Field> getFieldMap() {
126 return hFields;
127 }
128
129
130
131
132
133
134
135 public List<Field> getFields() {
136 return Collections.unmodifiableList(lFields);
137 }
138
139
140
141
142
143
144 public String getName() {
145 return name;
146 }
147
148
149
150
151
152
153
154 public boolean isExtending() {
155 return inherit != null;
156 }
157
158
159
160
161
162
163
164
165 public boolean isProcessed() {
166 return processed;
167 }
168
169
170
171
172
173
174
175
176
177 protected void merge(final Form depends) {
178
179 final List<Field> templFields = new ArrayList<>();
180 @SuppressWarnings("unchecked")
181 final
182 Map<String, Field> temphFields = new FastHashMap();
183 for (final Field defaultField : depends.getFields()) {
184 if (defaultField != null) {
185 final String fieldKey = defaultField.getKey();
186 if (!this.containsField(fieldKey)) {
187 templFields.add(defaultField);
188 temphFields.put(fieldKey, defaultField);
189 }
190 else {
191 final Field old = getField(fieldKey);
192 getFieldMap().remove(fieldKey);
193 lFields.remove(old);
194 templFields.add(old);
195 temphFields.put(fieldKey, old);
196 }
197 }
198 }
199 lFields.addAll(0, templFields);
200 getFieldMap().putAll(temphFields);
201 }
202
203
204
205
206
207
208
209
210
211 protected void process(final Map<String, String> globalConstants, final Map<String, String> constants, final Map<String, Form> forms) {
212 if (isProcessed()) {
213 return;
214 }
215
216 int n = 0;
217 if (isExtending()) {
218 final Form parent = forms.get(inherit);
219 if (parent != null) {
220 if (!parent.isProcessed()) {
221
222 parent.process(constants, globalConstants, forms);
223 }
224 for (final Field f : parent.getFields()) {
225
226 if (getFieldMap().get(f.getKey()) == null) {
227 lFields.add(n, f);
228 getFieldMap().put(f.getKey(), f);
229 n++;
230 }
231 }
232 }
233 }
234 hFields.setFast(true);
235
236 for (final Iterator<Field> i = lFields.listIterator(n); i.hasNext(); ) {
237 final Field f = i.next();
238 f.process(globalConstants, constants);
239 }
240
241 processed = true;
242 }
243
244
245
246
247
248
249
250 public void setExtends(final String inherit) {
251 this.inherit = inherit;
252 }
253
254
255
256
257
258
259 public void setName(final String name) {
260 this.name = name;
261 }
262
263
264
265
266
267
268 @Override
269 public String toString() {
270 final StringBuilder results = new StringBuilder();
271
272 results.append("Form: ");
273 results.append(name);
274 results.append("\n");
275
276 for (final Field lField : lFields) {
277 results.append("\tField: \n");
278 results.append(lField);
279 results.append("\n");
280 }
281
282 return results.toString();
283 }
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298 ValidatorResults validate(final Map<String, Object> params, final Map<String, ValidatorAction> actions, final int page)
299 throws ValidatorException {
300 return validate(params, actions, page, null);
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317 ValidatorResults validate(final Map<String, Object> params, final Map<String, ValidatorAction> actions, final int page, final String fieldName)
318 throws ValidatorException {
319 final ValidatorResults results = new ValidatorResults();
320 params.put(Validator.VALIDATOR_RESULTS_PARAM, results);
321
322
323 if (fieldName != null) {
324 final Field field = getFieldMap().get(fieldName);
325
326 if (field == null) {
327 throw new ValidatorException("Unknown field " + fieldName + " in form " + getName());
328 }
329 params.put(Validator.FIELD_PARAM, field);
330
331 if (field.getPage() <= page) {
332 results.merge(field.validate(params, actions));
333 }
334 } else {
335 for (final Field field : this.lFields) {
336
337 params.put(Validator.FIELD_PARAM, field);
338
339 if (field.getPage() <= page) {
340 results.merge(field.validate(params, actions));
341 }
342 }
343 }
344
345 return results;
346 }
347 }