View Javadoc
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    *      http://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       * <p>Create or retrieve a <code>FormSet</code> based on the language, country
36       *    and variant.</p>
37       *
38       * @param resources The validator resources.
39       * @param language The locale's language.
40       * @param country The locale's country.
41       * @param variant The locale's language variant.
42       * @return The FormSet for a locale.
43       * @since 1.2
44       */
45      private FormSet createFormSet(final ValidatorResources resources,
46                                    final String language,
47                                    final String country,
48                                    final String variant) {
49  
50          // Retrieve existing FormSet for the language/country/variant
51          FormSet formSet = resources.getFormSet(language, country, variant);
52          if (formSet != null) {
53              if (getLog().isDebugEnabled()) {
54                  getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
55              }
56              return formSet;
57          }
58  
59          // Create a new FormSet for the language/country/variant
60          formSet = new FormSet();
61          formSet.setLanguage(language);
62          formSet.setCountry(country);
63          formSet.setVariant(variant);
64  
65          // Add the FormSet to the validator resources
66          resources.addFormSet(formSet);
67  
68          if (getLog().isDebugEnabled()) {
69              getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
70          }
71  
72          return formSet;
73  
74      }
75  
76      /**
77       * <p>Create or retrieve a <code>FormSet</code> for the specified
78       *    attributes.</p>
79       *
80       * @param attributes The sax attributes for the formset element.
81       * @return The FormSet for a locale.
82       * @throws Exception If an error occurs creating the FormSet.
83       */
84      @Override
85      public Object createObject(final Attributes attributes) throws Exception {
86  
87          final ValidatorResources resources = (ValidatorResources) digester.peek(0);
88  
89          final String language = attributes.getValue("language");
90          final String country = attributes.getValue("country");
91          final String variant = attributes.getValue("variant");
92  
93          return createFormSet(resources, language, country, variant);
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 }