FormSetFactory.java

  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. import org.apache.commons.digester.AbstractObjectCreationFactory;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.xml.sax.Attributes;

  22. /**
  23.  * Factory class used by Digester to create FormSet's.
  24.  *
  25.  * @since 1.2
  26.  */
  27. public class FormSetFactory extends AbstractObjectCreationFactory {

  28.     /** Logging */
  29.     private transient Log log = LogFactory.getLog(FormSetFactory.class);

  30.     /**
  31.      * <p>Create or retrieve a <code>FormSet</code> based on the language, country
  32.      *    and variant.</p>
  33.      *
  34.      * @param resources The validator resources.
  35.      * @param language The locale's language.
  36.      * @param country The locale's country.
  37.      * @param variant The locale's language variant.
  38.      * @return The FormSet for a locale.
  39.      * @since 1.2
  40.      */
  41.     private FormSet createFormSet(final ValidatorResources resources,
  42.                                   final String language,
  43.                                   final String country,
  44.                                   final String variant) {

  45.         // Retrieve existing FormSet for the language/country/variant
  46.         FormSet formSet = resources.getFormSet(language, country, variant);
  47.         if (formSet != null) {
  48.             if (getLog().isDebugEnabled()) {
  49.                 getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
  50.             }
  51.             return formSet;
  52.         }

  53.         // Create a new FormSet for the language/country/variant
  54.         formSet = new FormSet();
  55.         formSet.setLanguage(language);
  56.         formSet.setCountry(country);
  57.         formSet.setVariant(variant);

  58.         // Add the FormSet to the validator resources
  59.         resources.addFormSet(formSet);

  60.         if (getLog().isDebugEnabled()) {
  61.             getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
  62.         }

  63.         return formSet;

  64.     }

  65.     /**
  66.      * <p>Create or retrieve a <code>FormSet</code> for the specified
  67.      *    attributes.</p>
  68.      *
  69.      * @param attributes The sax attributes for the formset element.
  70.      * @return The FormSet for a locale.
  71.      * @throws Exception If an error occurs creating the FormSet.
  72.      */
  73.     @Override
  74.     public Object createObject(final Attributes attributes) throws Exception {

  75.         final ValidatorResources resources = (ValidatorResources) digester.peek(0);

  76.         final String language = attributes.getValue("language");
  77.         final String country = attributes.getValue("country");
  78.         final String variant = attributes.getValue("variant");

  79.         return createFormSet(resources, language, country, variant);

  80.     }

  81.     /**
  82.      * Accessor method for Log instance.
  83.      *
  84.      * The Log instance variable is transient and
  85.      * accessing it through this method ensures it
  86.      * is re-initialized when this instance is
  87.      * de-serialized.
  88.      *
  89.      * @return The Log instance.
  90.      */
  91.     private Log getLog() {
  92.         if (log == null) {
  93.             log = LogFactory.getLog(FormSetFactory.class);
  94.         }
  95.         return log;
  96.     }

  97. }