View Javadoc

    1   /*
    2    * $Id: FormSetFactory.java 366933 2006-01-07 22:51:01Z niallp $
    3    * $Rev: 366933 $
    4    * $Date: 2006-01-07 22:51:01 +0000 (Sat, 07 Jan 2006) $
    5    *
    6    * ====================================================================
    7    * Copyright 2005-2006 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 transient Log log = LogFactory.getLog(FormSetFactory.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       * @throws Exception If an error occurs creating the FormSet.
    45       */
    46      public Object createObject(Attributes attributes) throws Exception {
    47
    48          ValidatorResources resources = (ValidatorResources)digester.peek(0);
    49
    50          String language = attributes.getValue("language");
    51          String country  = attributes.getValue("country");
    52          String variant  = attributes.getValue("variant");
    53
    54          return createFormSet(resources, language, country, variant);
    55
    56      }
    57
    58      /***
    59       * <p>Create or retrieve a <code>FormSet</code> based on the language, country
    60       *    and variant.</p>
    61       *
    62       * @param resources The validator resources.
    63       * @param language The locale's language.
    64       * @param country The locale's country.
    65       * @param variant The locale's language variant.
    66       * @return The FormSet for a locale.
    67       * @since Validator 1.2
    68       */
    69      private FormSet createFormSet(ValidatorResources resources,
    70                                    String language,
    71                                    String country,
    72                                    String variant) throws Exception {
    73
    74          // Retrieve existing FormSet for the language/country/variant
    75          FormSet formSet = resources.getFormSet(language, country, variant);
    76          if (formSet != null) {
    77              if (getLog().isDebugEnabled()) {
    78                  getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
    79              }
    80              return formSet;
    81          }
    82
    83          // Create a new FormSet for the language/country/variant
    84          formSet = new FormSet();
    85          formSet.setLanguage(language);
    86          formSet.setCountry(country);
    87          formSet.setVariant(variant);
    88
    89          // Add the FormSet to the validator resources
    90          resources.addFormSet(formSet);
    91
    92          if (getLog().isDebugEnabled()) {
    93              getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
    94          }
    95
    96          return formSet;
    97
    98      }
    99
    100     /***
    101      * Accessor method for Log instance.
    102      *
    103      * The Log instance variable is transient and
    104      * accessing it through this method ensures it
    105      * is re-initialized when this instance is
    106      * de-serialized.
    107      *
    108      * @return The Log instance.
    109      */
    110     private Log getLog() {
    111         if (log == null) {
    112             log =  LogFactory.getLog(FormSetFactory.class);
    113         }
    114         return log;
    115     }
    116
    117 }