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