1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.commons.validator;
22
23 import org.xml.sax.Attributes;
24 import org.apache.commons.digester.AbstractObjectCreationFactory;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /***
29 * Factory class used by Digester to create FormSet's.
30 *
31 * @since Validator 1.2
32 */
33 public class FormSetFactory extends AbstractObjectCreationFactory {
34
35 /*** Logging */
36 private static final Log log = LogFactory.getLog(ValidatorResources.class);
37
38 /***
39 * <p>Create or retrieve a <code>FormSet</code> for the specified
40 * attributes.</p>
41 *
42 * @param attributes The sax attributes for the formset element.
43 * @return The FormSet for a locale.
44 */
45 public Object createObject(Attributes attributes) throws Exception {
46
47 ValidatorResources resources = (ValidatorResources)digester.peek(0);
48
49 String language = attributes.getValue("language");
50 String country = attributes.getValue("country");
51 String variant = attributes.getValue("variant");
52
53 return createFormSet(resources, language, country, variant);
54
55 }
56
57 /***
58 * <p>Create or retrieve a <code>FormSet</code> based on the language, country
59 * and variant.</p>
60 *
61 * @param resources The validator resources.
62 * @param language The locale's language.
63 * @param country The locale's country.
64 * @param variant The locale's language variant.
65 * @return The FormSet for a locale.
66 * @since Validator 1.2
67 */
68 private FormSet createFormSet(ValidatorResources resources,
69 String language,
70 String country,
71 String variant) throws Exception {
72
73
74 FormSet formSet = resources.getFormSet(language, country, variant);
75 if (formSet != null) {
76 if (log.isDebugEnabled()) {
77 log.debug("FormSet[" + formSet.displayKey() + "] found - merging.");
78 }
79 return formSet;
80 }
81
82
83 formSet = new FormSet();
84 formSet.setLanguage(language);
85 formSet.setCountry(country);
86 formSet.setVariant(variant);
87
88
89 resources.addFormSet(formSet);
90
91 if (log.isDebugEnabled()) {
92 log.debug("FormSet[" + formSet.displayKey() + "] created.");
93 }
94
95 return formSet;
96
97 }
98
99 }