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.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          // Retrieve existing FormSet for the language/country/variant
    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          // Create a new FormSet for the language/country/variant
    81          formSet = new FormSet();
    82          formSet.setLanguage(language);
    83          formSet.setCountry(country);
    84          formSet.setVariant(variant);
    85
    86          // Add the FormSet to the validator resources
    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 }