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: 493905 $ $Date: 2007-01-08 03:11:38 +0100 (Mo, 08 Jan 2007) $
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    public Object createObject(Attributes attributes) throws Exception {
044
045        ValidatorResources resources = (ValidatorResources)digester.peek(0);
046
047        String language = attributes.getValue("language");
048        String country  = attributes.getValue("country");
049        String variant  = attributes.getValue("variant");
050
051        return createFormSet(resources, language, country, variant);
052
053    }
054
055    /**
056     * <p>Create or retrieve a <code>FormSet</code> based on the language, country
057     *    and variant.</p>
058     *
059     * @param resources The validator resources.
060     * @param language The locale's language.
061     * @param country The locale's country.
062     * @param variant The locale's language variant.
063     * @return The FormSet for a locale.
064     * @since Validator 1.2
065     */
066    private FormSet createFormSet(ValidatorResources resources,
067                                  String language,
068                                  String country,
069                                  String variant) throws Exception {
070
071        // Retrieve existing FormSet for the language/country/variant
072        FormSet formSet = resources.getFormSet(language, country, variant);
073        if (formSet != null) {
074            if (getLog().isDebugEnabled()) {
075                getLog().debug("FormSet[" + formSet.displayKey() + "] found - merging.");
076            }
077            return formSet;
078        }
079
080        // Create a new FormSet for the language/country/variant
081        formSet = new FormSet();
082        formSet.setLanguage(language);
083        formSet.setCountry(country);
084        formSet.setVariant(variant);
085
086        // Add the FormSet to the validator resources
087        resources.addFormSet(formSet);
088
089        if (getLog().isDebugEnabled()) {
090            getLog().debug("FormSet[" + formSet.displayKey() + "] created.");
091        }
092
093        return formSet;
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}