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 public Form() {
79
80 }
81
82
83
84
85
86
87 public void addField(final Field f) {
88 lFields.add(f);
89 getFieldMap().put(f.getKey(), f);
90 }
91
92
93
94
95
96
97
98
99 public boolean containsField(final String fieldName) {
100 return getFieldMap().containsKey(fieldName);
101 }
102
103
104
105
106
107
108
109 public String getExtends() {
110 return inherit;
111 }
112
113
114
115
116
117
118
119
120
121 public Field getField(final String fieldName) {
122 return getFieldMap().get(fieldName);
123 }
124
125
126
127
128
129
130
131 @SuppressWarnings("unchecked")
132 protected Map<String, Field> getFieldMap() {
133 return hFields;
134 }
135
136
137
138
139
140
141
142 public List<Field> getFields() {
143 return Collections.unmodifiableList(lFields);
144 }
145
146
147
148
149
150
151 public String getName() {
152 return name;
153 }
154
155
156
157
158
159
160
161 public boolean isExtending() {
162 return inherit != null;
163 }
164
165
166
167
168
169
170
171
172 public boolean isProcessed() {
173 return processed;
174 }
175
176
177
178
179
180
181
182
183
184 protected void merge(final Form depends) {
185 final List<Field> templFields = new ArrayList<>();
186 @SuppressWarnings("unchecked")
187 final Map<String, Field> temphFields = new FastHashMap();
188 for (final Field defaultField : depends.getFields()) {
189 if (defaultField != null) {
190 final String fieldKey = defaultField.getKey();
191 if (!containsField(fieldKey)) {
192 templFields.add(defaultField);
193 temphFields.put(fieldKey, defaultField);
194 } else {
195 final Field old = getField(fieldKey);
196 getFieldMap().remove(fieldKey);
197 lFields.remove(old);
198 templFields.add(old);
199 temphFields.put(fieldKey, old);
200 }
201 }
202 }
203 lFields.addAll(0, templFields);
204 getFieldMap().putAll(temphFields);
205 }
206
207
208
209
210
211
212
213
214
215 protected void process(final Map<String, String> globalConstants, final Map<String, String> constants, final Map<String, Form> forms) {
216 if (isProcessed()) {
217 return;
218 }
219
220 int n = 0;
221 if (isExtending()) {
222 final Form parent = forms.get(inherit);
223 if (parent != null) {
224 if (!parent.isProcessed()) {
225
226 parent.process(constants, globalConstants, forms);
227 }
228 for (final Field f : parent.getFields()) {
229
230 if (getFieldMap().get(f.getKey()) == null) {
231 lFields.add(n, f);
232 getFieldMap().put(f.getKey(), f);
233 n++;
234 }
235 }
236 }
237 }
238 hFields.setFast(true);
239
240 for (final Iterator<Field> i = lFields.listIterator(n); i.hasNext(); ) {
241 final Field f = i.next();
242 f.process(globalConstants, constants);
243 }
244
245 processed = true;
246 }
247
248
249
250
251
252
253
254 public void setExtends(final String inherit) {
255 this.inherit = inherit;
256 }
257
258
259
260
261
262
263 public void setName(final String name) {
264 this.name = name;
265 }
266
267
268
269
270
271
272 @Override
273 public String toString() {
274 final StringBuilder results = new StringBuilder();
275
276 results.append("Form: ");
277 results.append(name);
278 results.append("\n");
279
280 for (final Field lField : lFields) {
281 results.append("\tField: \n");
282 results.append(lField);
283 results.append("\n");
284 }
285
286 return results.toString();
287 }
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 ValidatorResults validate(final Map<String, Object> params, final Map<String, ValidatorAction> actions, final int page)
303 throws ValidatorException {
304 return validate(params, actions, page, null);
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 ValidatorResults validate(final Map<String, Object> params, final Map<String, ValidatorAction> actions, final int page, final String fieldName)
322 throws ValidatorException {
323 final ValidatorResults results = new ValidatorResults();
324 params.put(Validator.VALIDATOR_RESULTS_PARAM, results);
325
326
327 if (fieldName != null) {
328 final Field field = getFieldMap().get(fieldName);
329
330 if (field == null) {
331 throw new ValidatorException("Unknown field " + fieldName + " in form " + getName());
332 }
333 params.put(Validator.FIELD_PARAM, field);
334
335 if (field.getPage() <= page) {
336 results.merge(field.validate(params, actions));
337 }
338 } else {
339 for (final Field field : lFields) {
340
341 params.put(Validator.FIELD_PARAM, field);
342
343 if (field.getPage() <= page) {
344 results.merge(field.validate(params, actions));
345 }
346 }
347 }
348
349 return results;
350 }
351 }