View Javadoc

1   /*
2    * $Id: FormSetFactory.java 293498 2005-10-04 02:59:43Z niallp $
3    * $Rev: 293498 $
4    * $Date: 2005-10-04 03:59:43 +0100 (Tue, 04 Oct 2005) $
5    *
6    * ====================================================================
7    * Copyright 2005 The Apache Software Foundation
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *     http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
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          // Retrieve existing FormSet for the language/country/variant
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          // Create a new FormSet for the language/country/variant
83          formSet = new FormSet();
84          formSet.setLanguage(language);
85          formSet.setCountry(country);
86          formSet.setVariant(variant);
87  
88          // Add the FormSet to the validator resources
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  }