1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.validator;
18
19 import org.apache.commons.digester.AbstractObjectCreationFactory;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.xml.sax.Attributes;
23
24 /**
25 * Factory class used by Digester to create FormSet's.
26 *
27 * @since 1.2
28 */
29 public class FormSetFactory extends AbstractObjectCreationFactory {
30
31 /** Logging */
32 private transient Log log = LogFactory.getLog(FormSetFactory.class);
33
34 /**
35 * Constructs a new instance.
36 */
37 public FormSetFactory() {
38 // empty
39 }
40
41 /**
42 * <p>Create or retrieve a {@code FormSet} based on the language, country
43 * and variant.</p>
44 *
45 * @param resources The validator resources.
46 * @param language The locale's language.
47 * @param country The locale's country.
48 * @param variant The locale's language variant.
49 * @return The FormSet for a locale.
50 * @since 1.2
51 */
52 private FormSet createFormSet(final ValidatorResources resources,
53 final String language,
54 final String country,
55 final String variant) {
56
57 // Retrieve existing FormSet for the language/country/variant
58 FormSet formSet = resources.getFormSet(language, country, variant);
59 if (formSet != null) {
60 if (getLog().isDebugEnabled()) {
61 getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
62 }
63 return formSet;
64 }
65
66 // Create a new FormSet for the language/country/variant
67 formSet = new FormSet();
68 formSet.setLanguage(language);
69 formSet.setCountry(country);
70 formSet.setVariant(variant);
71
72 // Add the FormSet to the validator resources
73 resources.addFormSet(formSet);
74
75 if (getLog().isDebugEnabled()) {
76 getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
77 }
78
79 return formSet;
80
81 }
82
83 /**
84 * <p>Create or retrieve a {@code FormSet} for the specified
85 * attributes.</p>
86 *
87 * @param attributes The sax attributes for the formset element.
88 * @return The FormSet for a locale.
89 * @throws Exception If an error occurs creating the FormSet.
90 */
91 @Override
92 public Object createObject(final Attributes attributes) throws Exception {
93
94 final ValidatorResources resources = (ValidatorResources) digester.peek(0);
95
96 final String language = attributes.getValue("language");
97 final String country = attributes.getValue("country");
98 final String variant = attributes.getValue("variant");
99
100 return createFormSet(resources, language, country, variant);
101
102 }
103
104 /**
105 * Accessor method for Log instance.
106 *
107 * The Log instance variable is transient and
108 * accessing it through this method ensures it
109 * is re-initialized when this instance is
110 * de-serialized.
111 *
112 * @return The Log instance.
113 */
114 private Log getLog() {
115 if (log == null) {
116 log = LogFactory.getLog(FormSetFactory.class);
117 }
118 return log;
119 }
120
121 }