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.IOException;
20 import java.io.InputStream;
21 import java.io.Serializable;
22 import java.net.URL;
23 import java.util.Collections;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import org.apache.commons.collections.FastHashMap;
28 import org.apache.commons.digester.Digester;
29 import org.apache.commons.digester.Rule;
30 import org.apache.commons.digester.xmlrules.DigesterLoader;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.SAXException;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class ValidatorResources implements Serializable {
54
55 private static final long serialVersionUID = -8203745881446239554L;
56
57
58 private static final String VALIDATOR_RULES = "digester-rules.xml";
59
60
61
62
63
64
65 private static final String[] REGISTRATIONS = {
66 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN",
67 "/org/apache/commons/validator/resources/validator_1_0.dtd",
68 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN",
69 "/org/apache/commons/validator/resources/validator_1_0_1.dtd",
70 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN",
71 "/org/apache/commons/validator/resources/validator_1_1.dtd",
72 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN",
73 "/org/apache/commons/validator/resources/validator_1_1_3.dtd",
74 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.2.0//EN",
75 "/org/apache/commons/validator/resources/validator_1_2_0.dtd",
76 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN",
77 "/org/apache/commons/validator/resources/validator_1_3_0.dtd",
78 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.4.0//EN",
79 "/org/apache/commons/validator/resources/validator_1_4_0.dtd"
80 };
81
82
83
84
85 protected static Locale defaultLocale = Locale.getDefault();
86
87 private static final String ARGS_PATTERN
88 = "form-validation/formset/form/field/arg";
89
90 private transient Log log = LogFactory.getLog(ValidatorResources.class);
91
92
93
94
95
96
97 @Deprecated
98 protected FastHashMap hFormSets = new FastHashMap();
99
100
101
102
103
104
105 @Deprecated
106 protected FastHashMap hConstants = new FastHashMap();
107
108
109
110
111
112
113 @Deprecated
114 protected FastHashMap hActions = new FastHashMap();
115
116
117
118
119
120 protected FormSet defaultFormSet;
121
122
123
124
125 public ValidatorResources() {
126 }
127
128
129
130
131
132
133
134
135
136
137 public ValidatorResources(final InputStream in) throws IOException, SAXException {
138 this(new InputStream[]{in});
139 }
140
141
142
143
144
145
146
147
148
149
150
151 public ValidatorResources(final InputStream[] streams)
152 throws IOException, SAXException {
153
154 final Digester digester = initDigester();
155 for (int i = 0; i < streams.length; i++) {
156 if (streams[i] == null) {
157 throw new IllegalArgumentException("Stream[" + i + "] is null");
158 }
159 digester.push(this);
160 digester.parse(streams[i]);
161 }
162
163 process();
164 }
165
166
167
168
169
170
171
172
173
174 public ValidatorResources(final String uri) throws IOException, SAXException {
175 this(new String[] { uri });
176 }
177
178
179
180
181
182
183
184
185
186
187 public ValidatorResources(final String... uris)
188 throws IOException, SAXException {
189
190 final Digester digester = initDigester();
191 for (final String element : uris) {
192 digester.push(this);
193 digester.parse(element);
194 }
195
196 process();
197 }
198
199
200
201
202
203
204
205
206
207
208 public ValidatorResources(final URL url)
209 throws IOException, SAXException {
210 this(new URL[]{url});
211 }
212
213
214
215
216
217
218
219
220
221
222 public ValidatorResources(final URL[] urls)
223 throws IOException, SAXException {
224
225 final Digester digester = initDigester();
226 for (final URL url : urls) {
227 digester.push(this);
228 digester.parse(url);
229 }
230
231 process();
232 }
233
234
235
236
237
238
239 public void addConstant(final String name, final String value) {
240 if (getLog().isDebugEnabled()) {
241 getLog().debug("Adding Global Constant: " + name + "," + value);
242 }
243
244 hConstants.put(name, value);
245 }
246
247
248
249
250
251
252
253
254 public void addFormSet(final FormSet fs) {
255 final String key = buildKey(fs);
256 if (key.isEmpty()) {
257 if (getLog().isWarnEnabled() && defaultFormSet != null) {
258
259 getLog().warn("Overriding default FormSet definition.");
260 }
261 defaultFormSet = fs;
262 } else {
263 final FormSet formset = getFormSets().get(key);
264 if (formset == null) {
265 if (getLog().isDebugEnabled()) {
266 getLog().debug("Adding FormSet '" + fs + "'.");
267 }
268 } else if (getLog().isWarnEnabled()) {
269
270 getLog().warn("Overriding FormSet definition. Duplicate for locale: " + key);
271 }
272 getFormSets().put(key, fs);
273 }
274 }
275
276
277
278
279
280
281
282 private void addOldArgRules(final Digester digester) {
283
284 final Rule rule = new Rule() {
285 @Override
286 public void begin(final String namespace, final String name, final Attributes attributes) {
287
288 final Arg arg = new Arg();
289 arg.setKey(attributes.getValue("key"));
290 arg.setName(attributes.getValue("name"));
291 if ("false".equalsIgnoreCase(attributes.getValue("resource"))) {
292 arg.setResource(false);
293 }
294 try {
295 final int length = "arg".length();
296 arg.setPosition(Integer.parseInt(name.substring(length)));
297 } catch (final Exception ex) {
298 getLog().error("Error parsing Arg position: " + name + " " + arg + " " + ex);
299 }
300
301
302 ((Field) getDigester().peek(0)).addArg(arg);
303 }
304 };
305
306
307 digester.addRule(ARGS_PATTERN + "0", rule);
308 digester.addRule(ARGS_PATTERN + "1", rule);
309 digester.addRule(ARGS_PATTERN + "2", rule);
310 digester.addRule(ARGS_PATTERN + "3", rule);
311
312 }
313
314
315
316
317
318
319
320
321 public void addValidatorAction(final ValidatorAction va) {
322 va.init();
323
324 getActions().put(va.getName(), va);
325
326 if (getLog().isDebugEnabled()) {
327 getLog().debug("Add ValidatorAction: " + va.getName() + "," + va.getClassname());
328 }
329 }
330
331
332
333
334
335
336
337 protected String buildKey(final FormSet fs) {
338 return
339 buildLocale(fs.getLanguage(), fs.getCountry(), fs.getVariant());
340 }
341
342
343
344
345 private String buildLocale(final String lang, final String country, final String variant) {
346 final StringBuilder key = new StringBuilder().append(lang != null && !lang.isEmpty() ? lang : "");
347 key.append(country != null && !country.isEmpty() ? "_" + country : "");
348 key.append(variant != null && !variant.isEmpty() ? "_" + variant : "");
349 return key.toString();
350 }
351
352
353
354
355
356
357 @SuppressWarnings("unchecked")
358 protected Map<String, ValidatorAction> getActions() {
359 return hActions;
360 }
361
362
363
364
365
366
367 @SuppressWarnings("unchecked")
368 protected Map<String, String> getConstants() {
369 return hConstants;
370 }
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387 public Form getForm(final Locale locale, final String formKey) {
388 return this.getForm(locale.getLanguage(), locale.getCountry(), locale
389 .getVariant(), formKey);
390 }
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409 public Form getForm(final String language, final String country, final String variant, final String formKey) {
410
411 Form form = null;
412
413
414 String key = buildLocale(language, country, variant);
415 if (!key.isEmpty()) {
416 final FormSet formSet = getFormSets().get(key);
417 if (formSet != null) {
418 form = formSet.getForm(formKey);
419 }
420 }
421 final String localeKey = key;
422
423
424 if (form == null) {
425 key = buildLocale(language, country, null);
426 if (!key.isEmpty()) {
427 final FormSet formSet = getFormSets().get(key);
428 if (formSet != null) {
429 form = formSet.getForm(formKey);
430 }
431 }
432 }
433
434
435 if (form == null) {
436 key = buildLocale(language, null, null);
437 if (!key.isEmpty()) {
438 final FormSet formSet = getFormSets().get(key);
439 if (formSet != null) {
440 form = formSet.getForm(formKey);
441 }
442 }
443 }
444
445
446 if (form == null) {
447 form = defaultFormSet.getForm(formKey);
448 key = "default";
449 }
450
451 if (form == null) {
452 if (getLog().isWarnEnabled()) {
453 getLog().warn("Form '" + formKey + "' not found for locale '" + localeKey + "'");
454 }
455 } else if (getLog().isDebugEnabled()) {
456 getLog().debug("Form '" + formKey + "' found in formset '" + key + "' for locale '" + localeKey + "'");
457 }
458
459 return form;
460
461 }
462
463
464
465
466
467
468
469
470
471
472 FormSet getFormSet(final String language, final String country, final String variant) {
473 final String key = buildLocale(language, country, variant);
474 if (key.isEmpty()) {
475 return defaultFormSet;
476 }
477 return getFormSets().get(key);
478 }
479
480
481
482
483
484
485 @SuppressWarnings("unchecked")
486 protected Map<String, FormSet> getFormSets() {
487 return hFormSets;
488 }
489
490
491
492
493
494
495
496
497
498
499
500 private Log getLog() {
501 if (log == null) {
502 log = LogFactory.getLog(ValidatorResources.class);
503 }
504 return log;
505 }
506
507
508
509
510
511
512
513
514
515
516
517 private FormSet getParent(final FormSet fs) {
518
519 FormSet parent = null;
520 if (fs.getType() == FormSet.LANGUAGE_FORMSET) {
521 parent = defaultFormSet;
522 } else if (fs.getType() == FormSet.COUNTRY_FORMSET) {
523 parent = getFormSets().get(buildLocale(fs.getLanguage(), null, null));
524 if (parent == null) {
525 parent = defaultFormSet;
526 }
527 } else if (fs.getType() == FormSet.VARIANT_FORMSET) {
528 parent = getFormSets().get(buildLocale(fs.getLanguage(), fs.getCountry(), null));
529 if (parent == null) {
530 parent = getFormSets().get(buildLocale(fs.getLanguage(), null, null));
531 if (parent == null) {
532 parent = defaultFormSet;
533 }
534 }
535 }
536 return parent;
537 }
538
539
540
541
542
543
544 public ValidatorAction getValidatorAction(final String key) {
545 return getActions().get(key);
546 }
547
548
549
550
551
552 public Map<String, ValidatorAction> getValidatorActions() {
553 return Collections.unmodifiableMap(getActions());
554 }
555
556
557
558
559 private Digester initDigester() {
560 URL rulesUrl = this.getClass().getResource(VALIDATOR_RULES);
561 if (rulesUrl == null) {
562
563 rulesUrl = ValidatorResources.class.getResource(VALIDATOR_RULES);
564 }
565 if (getLog().isDebugEnabled()) {
566 getLog().debug("Loading rules from '" + rulesUrl + "'");
567 }
568 final Digester digester = DigesterLoader.createDigester(rulesUrl);
569 digester.setNamespaceAware(true);
570 digester.setValidating(true);
571 digester.setUseContextClassLoader(true);
572
573
574 addOldArgRules(digester);
575
576
577 for (int i = 0; i < REGISTRATIONS.length; i += 2) {
578 final URL url = this.getClass().getResource(REGISTRATIONS[i + 1]);
579 if (url != null) {
580 digester.register(REGISTRATIONS[i], url.toString());
581 }
582 }
583 return digester;
584 }
585
586
587
588
589
590
591
592
593
594 public void process() {
595 hFormSets.setFast(true);
596 hConstants.setFast(true);
597 hActions.setFast(true);
598
599 processForms();
600 }
601
602
603
604
605
606
607 private void processForms() {
608 if (defaultFormSet == null) {
609
610 defaultFormSet = new FormSet();
611 }
612 defaultFormSet.process(getConstants());
613
614 for (final String key : getFormSets().keySet()) {
615 final FormSet fs = getFormSets().get(key);
616 fs.merge(getParent(fs));
617 }
618
619
620 for (final FormSet fs : getFormSets().values()) {
621 if (!fs.isProcessed()) {
622 fs.process(getConstants());
623 }
624 }
625 }
626
627 }