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.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Map.Entry;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28
29
30
31
32
33 public class FormSet implements Serializable {
34
35 private static final long serialVersionUID = -8936513232763306055L;
36
37
38
39
40 protected final static int GLOBAL_FORMSET = 1;
41
42
43
44
45
46 protected final static int LANGUAGE_FORMSET = 2;
47
48
49
50
51
52 protected final static int COUNTRY_FORMSET = 3;
53
54
55
56
57 protected final static int VARIANT_FORMSET = 4;
58
59
60 private transient Log log = LogFactory.getLog(FormSet.class);
61
62
63
64
65
66 private boolean processed;
67
68
69 private String language;
70
71
72 private String country;
73
74
75 private String variant;
76
77
78
79
80
81 private final Map<String, Form> forms = new HashMap<>();
82
83
84
85
86
87 private final Map<String, String> constants = new HashMap<>();
88
89
90
91
92
93 private boolean merged;
94
95
96
97
98
99
100
101 public void addConstant(final String name, final String value) {
102 if (constants.containsKey(name)) {
103 getLog().error("Constant '" + name + "' already exists in FormSet[" + this.displayKey() + "] - ignoring.");
104 } else {
105 constants.put(name, value);
106 }
107 }
108
109
110
111
112
113
114 public void addForm(final Form f) {
115
116 final String formName = f.getName();
117 if (forms.containsKey(formName)) {
118 getLog().error("Form '" + formName + "' already exists in FormSet[" + this.displayKey() + "] - ignoring.");
119
120 } else {
121 forms.put(f.getName(), f);
122 }
123
124 }
125
126
127
128
129
130
131 public String displayKey() {
132 final StringBuilder results = new StringBuilder();
133 if (language != null && !language.isEmpty()) {
134 results.append("language=");
135 results.append(language);
136 }
137 if (country != null && !country.isEmpty()) {
138 if (results.length() > 0) {
139 results.append(", ");
140 }
141 results.append("country=");
142 results.append(country);
143 }
144 if (variant != null && !variant.isEmpty()) {
145 if (results.length() > 0) {
146 results.append(", ");
147 }
148 results.append("variant=");
149 results.append(variant);
150 }
151 if (results.length() == 0) {
152 results.append("default");
153 }
154
155 return results.toString();
156 }
157
158
159
160
161
162
163 public String getCountry() {
164 return country;
165 }
166
167
168
169
170
171
172
173 public Form getForm(final String formName) {
174 return this.forms.get(formName);
175 }
176
177
178
179
180
181
182
183 public Map<String, Form> getForms() {
184 return Collections.unmodifiableMap(forms);
185 }
186
187
188
189
190
191
192 public String getLanguage() {
193 return language;
194 }
195
196
197
198
199
200
201
202
203
204
205
206 private Log getLog() {
207 if (log == null) {
208 log = LogFactory.getLog(FormSet.class);
209 }
210 return log;
211 }
212
213
214
215
216
217
218
219
220
221
222
223 protected int getType() {
224 if (getVariant() != null) {
225 if (getLanguage() == null || getCountry() == null) {
226 throw new NullPointerException("When variant is specified, country and language must be specified.");
227 }
228 return VARIANT_FORMSET;
229 }
230 if (getCountry() != null) {
231 if (getLanguage() == null) {
232 throw new NullPointerException("When country is specified, language must be specified.");
233 }
234 return COUNTRY_FORMSET;
235 }
236 if (getLanguage() != null) {
237 return LANGUAGE_FORMSET;
238 }
239 return GLOBAL_FORMSET;
240 }
241
242
243
244
245
246
247 public String getVariant() {
248 return variant;
249 }
250
251
252
253
254
255
256
257 protected boolean isMerged() {
258 return merged;
259 }
260
261
262
263
264
265
266
267 public boolean isProcessed() {
268 return processed;
269 }
270
271
272
273
274
275
276
277
278
279
280 protected void merge(final FormSet depends) {
281 if (depends != null) {
282 final Map<String, Form> pForms = getForms();
283 final Map<String, Form> dForms = depends.getForms();
284 for (final Entry<String, Form> entry : dForms.entrySet()) {
285 final String key = entry.getKey();
286 final Form pForm = pForms.get(key);
287 if (pForm != null) {
288
289 pForm.merge(entry.getValue());
290 } else {
291 addForm(entry.getValue());
292 }
293 }
294 }
295 merged = true;
296 }
297
298
299
300
301
302
303 synchronized void process(final Map<String, String> globalConstants) {
304 for (final Form f : forms.values()) {
305 f.process(globalConstants, constants, forms);
306 }
307
308 processed = true;
309 }
310
311
312
313
314
315
316 public void setCountry(final String country) {
317 this.country = country;
318 }
319
320
321
322
323
324
325 public void setLanguage(final String language) {
326 this.language = language;
327 }
328
329
330
331
332
333
334 public void setVariant(final String variant) {
335 this.variant = variant;
336 }
337
338
339
340
341
342
343 @Override
344 public String toString() {
345 final StringBuilder results = new StringBuilder();
346
347 results.append("FormSet: language=");
348 results.append(language);
349 results.append(" country=");
350 results.append(country);
351 results.append(" variant=");
352 results.append(variant);
353 results.append("\n");
354
355 for (final Object name : getForms().values()) {
356 results.append(" ");
357 results.append(name);
358 results.append("\n");
359 }
360
361 return results.toString();
362 }
363 }