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: */
017: package org.apache.commons.validator;
018:
019: import org.xml.sax.Attributes;
020: import org.apache.commons.digester.AbstractObjectCreationFactory;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * Factory class used by Digester to create FormSet's.
026: *
027: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
028: * @since Validator 1.2
029: */
030: public 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
046: .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, String country, String variant)
069: throws Exception {
070:
071: // Retrieve existing FormSet for the language/country/variant
072: FormSet formSet = resources.getFormSet(language, country,
073: variant);
074: if (formSet != null) {
075: if (getLog().isDebugEnabled()) {
076: getLog().debug(
077: "FormSet[" + formSet.displayKey()
078: + "] found - merging.");
079: }
080: return formSet;
081: }
082:
083: // Create a new FormSet for the language/country/variant
084: formSet = new FormSet();
085: formSet.setLanguage(language);
086: formSet.setCountry(country);
087: formSet.setVariant(variant);
088:
089: // Add the FormSet to the validator resources
090: resources.addFormSet(formSet);
091:
092: if (getLog().isDebugEnabled()) {
093: getLog().debug(
094: "FormSet[" + formSet.displayKey() + "] created.");
095: }
096:
097: return formSet;
098:
099: }
100:
101: /**
102: * Accessor method for Log instance.
103: *
104: * The Log instance variable is transient and
105: * accessing it through this method ensures it
106: * is re-initialized when this instance is
107: * de-serialized.
108: *
109: * @return The Log instance.
110: */
111: private Log getLog() {
112: if (log == null) {
113: log = LogFactory.getLog(FormSetFactory.class);
114: }
115: return log;
116: }
117:
118: }
|