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 org.xml.sax.Attributes;
20 import org.apache.commons.digester.AbstractObjectCreationFactory;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 /***
25 * Factory class used by Digester to create FormSet's.
26 *
27 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
28 * @since Validator 1.2
29 */
30 public class FormSetFactory extends AbstractObjectCreationFactory {
31
32 /*** Logging */
33 private transient Log log = LogFactory.getLog(FormSetFactory.class);
34
35 /***
36 * <p>Create or retrieve a <code>FormSet</code> for the specified
37 * attributes.</p>
38 *
39 * @param attributes The sax attributes for the formset element.
40 * @return The FormSet for a locale.
41 * @throws Exception If an error occurs creating the FormSet.
42 */
43 public Object createObject(Attributes attributes) throws Exception {
44
45 ValidatorResources resources = (ValidatorResources)digester.peek(0);
46
47 String language = attributes.getValue("language");
48 String country = attributes.getValue("country");
49 String variant = attributes.getValue("variant");
50
51 return createFormSet(resources, language, country, variant);
52
53 }
54
55 /***
56 * <p>Create or retrieve a <code>FormSet</code> based on the language, country
57 * and variant.</p>
58 *
59 * @param resources The validator resources.
60 * @param language The locale's language.
61 * @param country The locale's country.
62 * @param variant The locale's language variant.
63 * @return The FormSet for a locale.
64 * @since Validator 1.2
65 */
66 private FormSet createFormSet(ValidatorResources resources,
67 String language,
68 String country,
69 String variant) throws Exception {
70
71
72 FormSet formSet = resources.getFormSet(language, country, variant);
73 if (formSet != null) {
74 if (getLog().isDebugEnabled()) {
75 getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
76 }
77 return formSet;
78 }
79
80
81 formSet = new FormSet();
82 formSet.setLanguage(language);
83 formSet.setCountry(country);
84 formSet.setVariant(variant);
85
86
87 resources.addFormSet(formSet);
88
89 if (getLog().isDebugEnabled()) {
90 getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
91 }
92
93 return formSet;
94
95 }
96
97 /***
98 * Accessor method for Log instance.
99 *
100 * The Log instance variable is transient and
101 * accessing it through this method ensures it
102 * is re-initialized when this instance is
103 * de-serialized.
104 *
105 * @return The Log instance.
106 */
107 private Log getLog() {
108 if (log == null) {
109 log = LogFactory.getLog(FormSetFactory.class);
110 }
111 return log;
112 }
113
114 }