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 transient Log log = LogFactory.getLog(FormSetFactory.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 * @throws Exception If an error occurs creating the FormSet.
45 */
46 public Object createObject(Attributes attributes) throws Exception {
47
48 ValidatorResources resources = (ValidatorResources)digester.peek(0);
49
50 String language = attributes.getValue("language");
51 String country = attributes.getValue("country");
52 String variant = attributes.getValue("variant");
53
54 return createFormSet(resources, language, country, variant);
55
56 }
57
58 /***
59 * <p>Create or retrieve a <code>FormSet</code> based on the language, country
60 * and variant.</p>
61 *
62 * @param resources The validator resources.
63 * @param language The locale's language.
64 * @param country The locale's country.
65 * @param variant The locale's language variant.
66 * @return The FormSet for a locale.
67 * @since Validator 1.2
68 */
69 private FormSet createFormSet(ValidatorResources resources,
70 String language,
71 String country,
72 String variant) throws Exception {
73
74
75 FormSet formSet = resources.getFormSet(language, country, variant);
76 if (formSet != null) {
77 if (getLog().isDebugEnabled()) {
78 getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
79 }
80 return formSet;
81 }
82
83
84 formSet = new FormSet();
85 formSet.setLanguage(language);
86 formSet.setCountry(country);
87 formSet.setVariant(variant);
88
89
90 resources.addFormSet(formSet);
91
92 if (getLog().isDebugEnabled()) {
93 getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
94 }
95
96 return formSet;
97
98 }
99
100 /***
101 * Accessor method for Log instance.
102 *
103 * The Log instance variable is transient and
104 * accessing it through this method ensures it
105 * is re-initialized when this instance is
106 * de-serialized.
107 *
108 * @return The Log instance.
109 */
110 private Log getLog() {
111 if (log == null) {
112 log = LogFactory.getLog(FormSetFactory.class);
113 }
114 return log;
115 }
116
117 }