    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.validator;
    018
    019import org.xml.sax.Attributes;
    020import org.apache.commons.digester.AbstractObjectCreationFactory;
    021import org.apache.commons.logging.Log;
    022import org.apache.commons.logging.LogFactory;
    023
    024/**
    025 * Factory class used by Digester to create FormSet's.
    026 *
    027 * @version $Revision$
    028 * @since Validator 1.2
    029 */
    030public class FormSetFactory extends AbstractObjectCreationFactory {
    031
    032    /** Logging */
    033    private transient Log log = LogFactory.getLog(FormSetFactory.class);
    034
    035    /**
    036     * <p>Create or retrieve a <code>FormSet</code> for the specified
    037     *    attributes.</p>
    038     *
    039     * @param attributes The sax attributes for the formset element.
    040     * @return The FormSet for a locale.
    041     * @throws Exception If an error occurs creating the FormSet.
    042     */
    043    @Override
    044    public Object createObject(Attributes attributes) throws Exception {
    045
    046        ValidatorResources resources = (ValidatorResources)digester.peek(0);
    047
    048        String language = attributes.getValue("language");
    049        String country  = attributes.getValue("country");
    050        String variant  = attributes.getValue("variant");
    051
    052        return createFormSet(resources, language, country, variant);
    053
    054    }
    055
    056    /**
    057     * <p>Create or retrieve a <code>FormSet</code> based on the language, country
    058     *    and variant.</p>
    059     *
    060     * @param resources The validator resources.
    061     * @param language The locale's language.
    062     * @param country The locale's country.
    063     * @param variant The locale's language variant.
    064     * @return The FormSet for a locale.
    065     * @since Validator 1.2
    066     */
    067    private FormSet createFormSet(ValidatorResources resources,
    068                                  String language,
    069                                  String country,
    070                                  String variant) throws Exception {
    071
    072        // Retrieve existing FormSet for the language/country/variant
    073        FormSet formSet = resources.getFormSet(language, country, variant);
    074        if (formSet != null) {
    075            if (getLog().isDebugEnabled()) {
    076                getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
    077            }
    078            return formSet;
    079        }
    080
    081        // Create a new FormSet for the language/country/variant
    082        formSet = new FormSet();
    083        formSet.setLanguage(language);
    084        formSet.setCountry(country);
    085        formSet.setVariant(variant);
    086
    087        // Add the FormSet to the validator resources
    088        resources.addFormSet(formSet);
    089
    090        if (getLog().isDebugEnabled()) {
    091            getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
    092        }
    093
    094        return formSet;
    095
    096    }
    097
    098    /**
    099     * Accessor method for Log instance.
    100     *
    101     * The Log instance variable is transient and
    102     * accessing it through this method ensures it
    103     * is re-initialized when this instance is
    104     * de-serialized.
    105     *
    106     * @return The Log instance.
    107     */
    108    private Log getLog() {
    109        if (log == null) {
    110            log =  LogFactory.getLog(FormSetFactory.class);
    111        }
    112        return log;
    113    }
    114
    115}