    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 *      https://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.apache.commons.digester.AbstractObjectCreationFactory;
    020import org.apache.commons.logging.Log;
    021import org.apache.commons.logging.LogFactory;
    022import org.xml.sax.Attributes;
    023
    024/**
    025 * Factory class used by Digester to create FormSet's.
    026 *
    027 * @since 1.2
    028 */
    029public class FormSetFactory extends AbstractObjectCreationFactory {
    030
    031    /** Logging */
    032    private transient Log log = LogFactory.getLog(FormSetFactory.class);
    033
    034    /**
    035     * Constructs a new instance.
    036     */
    037    public FormSetFactory() {
    038        // empty
    039    }
    040
    041    /**
    042     * Create or retrieve a {@code FormSet} based on the language, country
    043     *    and variant.
    044     *
    045     * @param resources The validator resources.
    046     * @param language The locale's language.
    047     * @param country The locale's country.
    048     * @param variant The locale's language variant.
    049     * @return The FormSet for a locale.
    050     * @since 1.2
    051     */
    052    private FormSet createFormSet(final ValidatorResources resources,
    053                                  final String language,
    054                                  final String country,
    055                                  final String variant) {
    056
    057        // Retrieve existing FormSet for the language/country/variant
    058        FormSet formSet = resources.getFormSet(language, country, variant);
    059        if (formSet != null) {
    060            if (getLog().isDebugEnabled()) {
    061                getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
    062            }
    063            return formSet;
    064        }
    065
    066        // Create a new FormSet for the language/country/variant
    067        formSet = new FormSet();
    068        formSet.setLanguage(language);
    069        formSet.setCountry(country);
    070        formSet.setVariant(variant);
    071
    072        // Add the FormSet to the validator resources
    073        resources.addFormSet(formSet);
    074
    075        if (getLog().isDebugEnabled()) {
    076            getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
    077        }
    078
    079        return formSet;
    080
    081    }
    082
    083    /**
    084     * Create or retrieve a {@code FormSet} for the specified
    085     *    attributes.
    086     *
    087     * @param attributes The sax attributes for the formset element.
    088     * @return The FormSet for a locale.
    089     * @throws Exception If an error occurs creating the FormSet.
    090     */
    091    @Override
    092    public Object createObject(final Attributes attributes) throws Exception {
    093
    094        final ValidatorResources resources = (ValidatorResources) digester.peek(0);
    095
    096        final String language = attributes.getValue("language");
    097        final String country = attributes.getValue("country");
    098        final String variant = attributes.getValue("variant");
    099
    100        return createFormSet(resources, language, country, variant);
    101
    102    }
    103
    104    /**
    105     * Accessor method for Log instance.
    106     *
    107     * The Log instance variable is transient and
    108     * accessing it through this method ensures it
    109     * is re-initialized when this instance is
    110     * de-serialized.
    111     *
    112     * @return The Log instance.
    113     */
    114    private Log getLog() {
    115        if (log == null) {
    116            log = LogFactory.getLog(FormSetFactory.class);
    117        }
    118        return log;
    119    }
    120
    121}