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.  *      https://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.      * Constructs a new instance.
  32.      */
  33.     public FormSetFactory() {
  34.         // empty
  35.     }

  36.     /**
  37.      * <p>Create or retrieve a {@code FormSet} based on the language, country
  38.      *    and variant.</p>
  39.      *
  40.      * @param resources The validator resources.
  41.      * @param language The locale's language.
  42.      * @param country The locale's country.
  43.      * @param variant The locale's language variant.
  44.      * @return The FormSet for a locale.
  45.      * @since 1.2
  46.      */
  47.     private FormSet createFormSet(final ValidatorResources resources,
  48.                                   final String language,
  49.                                   final String country,
  50.                                   final String variant) {

  51.         // Retrieve existing FormSet for the language/country/variant
  52.         FormSet formSet = resources.getFormSet(language, country, variant);
  53.         if (formSet != null) {
  54.             if (getLog().isDebugEnabled()) {
  55.                 getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
  56.             }
  57.             return formSet;
  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.         // Add the FormSet to the validator resources
  65.         resources.addFormSet(formSet);

  66.         if (getLog().isDebugEnabled()) {
  67.             getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
  68.         }

  69.         return formSet;

  70.     }

  71.     /**
  72.      * <p>Create or retrieve a {@code FormSet} for the specified
  73.      *    attributes.</p>
  74.      *
  75.      * @param attributes The sax attributes for the formset element.
  76.      * @return The FormSet for a locale.
  77.      * @throws Exception If an error occurs creating the FormSet.
  78.      */
  79.     @Override
  80.     public Object createObject(final Attributes attributes) throws Exception {

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

  82.         final String language = attributes.getValue("language");
  83.         final String country = attributes.getValue("country");
  84.         final String variant = attributes.getValue("variant");

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

  86.     }

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

  103. }