Author: niallp Date: Mon Oct 3 19:59:43 2005 New Revision: 293498 URL: http://svn.apache.org/viewcvs?rev=293498&view=rev Log: Fix for Bug 36899 - Merge multiple validation.xml, reported by Wolfgang Gehner.
I've changed the digester rules to use a new FormSetFactory class which looks up the FormSet in the ValidatorResources and if the FormSet already exists it returns that one, otherwise it creates a new FormSet and adds it to the ValidatorResources. Additionally I also added checks in the Form's addConstant() and addForm() methods to log an error if the Constant or Form already exists and ignore the duplicate. Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSetFactory.java (with props) Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSet.java jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSet.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSet.java?rev=293498&r1=293497&r2=293498&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSet.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSet.java Mon Oct 3 19:59:43 2005 @@ -25,6 +25,8 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; /** * Holds a set of <code>Form</code>s stored associated with a <code>Locale</code> @@ -33,6 +35,9 @@ */ public class FormSet implements Serializable { + /** Logging */ + private static final Log log = LogFactory.getLog(FormSet.class); + /** * Whether or not the this <code>FormSet</code> was processed for replacing * variables in strings with their values. @@ -230,7 +235,15 @@ * @param value The constant value */ public void addConstant(String name, String value) { - this.constants.put(name, value); + + if (constants.containsKey(name)) { + log.error("Constant '" + name + "' already exists in FormSet[" + + this.displayKey() + "] - ignoring."); + + } else { + constants.put(name, value); + } + } /** @@ -239,7 +252,16 @@ * @param f The form */ public void addForm(Form f) { - forms.put(f.getName(), f); + + String formName = f.getName(); + if (forms.containsKey(formName)) { + log.error("Form '" + formName + "' already exists in FormSet[" + + this.displayKey() + "] - ignoring."); + + } else { + forms.put(f.getName(), f); + } + } /** @@ -274,6 +296,38 @@ } processed = true; + } + + /** + * Returns a string representation of the object's key. + * + * @return A string representation of the key + */ + public String displayKey() { + StringBuffer results = new StringBuffer(); + if (language != null && language.length() > 0) { + results.append("language="); + results.append(language); + } + if (country != null && country.length() > 0) { + if (results.length() > 0) { + results.append(", "); + } + results.append("country="); + results.append(country); + } + if (variant != null && variant.length() > 0) { + if (results.length() > 0) { + results.append(", "); + } + results.append("variant="); + results.append(variant ); + } + if (results.length() == 0) { + results.append("default"); + } + + return results.toString(); } /** Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSetFactory.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSetFactory.java?rev=293498&view=auto ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSetFactory.java (added) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSetFactory.java Mon Oct 3 19:59:43 2005 @@ -0,0 +1,99 @@ +/* + * $Id$ + * $Rev$ + * $Date$ + * + * ==================================================================== + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.validator; + +import org.xml.sax.Attributes; +import org.apache.commons.digester.AbstractObjectCreationFactory; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Factory class used by Digester to create FormSet's. + * + * @since Validator 1.2 + */ +public class FormSetFactory extends AbstractObjectCreationFactory { + + /** Logging */ + private static final Log log = LogFactory.getLog(ValidatorResources.class); + + /** + * <p>Create or retrieve a <code>FormSet</code> for the specified + * attributes.</p> + * + * @param attributes The sax attributes for the formset element. + * @return The FormSet for a locale. + */ + public Object createObject(Attributes attributes) throws Exception { + + ValidatorResources resources = (ValidatorResources)digester.peek(0); + + String language = attributes.getValue("language"); + String country = attributes.getValue("country"); + String variant = attributes.getValue("variant"); + + return createFormSet(resources, language, country, variant); + + } + + /** + * <p>Create or retrieve a <code>FormSet</code> based on the language, country + * and variant.</p> + * + * @param resources The validator resources. + * @param language The locale's language. + * @param country The locale's country. + * @param variant The locale's language variant. + * @return The FormSet for a locale. + * @since Validator 1.2 + */ + private FormSet createFormSet(ValidatorResources resources, + String language, + String country, + String variant) throws Exception { + + // Retrieve existing FormSet for the language/country/variant + FormSet formSet = resources.getFormSet(language, country, variant); + if (formSet != null) { + if (log.isDebugEnabled()) { + log.debug("FormSet[" + formSet.displayKey() + "] found - merging."); + } + return formSet; + } + + // Create a new FormSet for the language/country/variant + formSet = new FormSet(); + formSet.setLanguage(language); + formSet.setCountry(country); + formSet.setVariant(variant); + + // Add the FormSet to the validator resources + resources.addFormSet(formSet); + + if (log.isDebugEnabled()) { + log.debug("FormSet[" + formSet.displayKey() + "] created."); + } + + return formSet; + + } + +} Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSetFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/FormSetFactory.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java?rev=293498&r1=293497&r2=293498&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/ValidatorResources.java Mon Oct 3 19:59:43 2005 @@ -495,6 +495,26 @@ } /** + * <p>Gets a <code>FormSet</code> based on the language, country + * and variant.</p> + * @param language The locale's language. + * @param country The locale's country. + * @param variant The locale's language variant. + * @return The FormSet for a locale. + * @since Validator 1.2 + */ + FormSet getFormSet(String language, String country, String variant) { + + String key = buildLocale(language, country, variant); + + if (key.length() == 0) { + return defaultFormSet; + } + + return (FormSet)hFormSets.get(key); + } + + /** * Returns a Map of String locale keys to Lists of their FormSets. * @return Map of Form sets * @since Validator 1.2.0 Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml?rev=293498&r1=293497&r2=293498&view=diff ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/digester-rules.xml Mon Oct 3 19:59:43 2005 @@ -32,9 +32,8 @@ <pattern value="form-validation/formset"> - <object-create-rule classname="org.apache.commons.validator.FormSet" /> - <set-properties-rule/> - <set-next-rule methodname="addFormSet" paramtype="org.apache.commons.validator.FormSet" /> + + <factory-create-rule classname="org.apache.commons.validator.FormSetFactory" /> <pattern value="constant"> <call-method-rule methodname="addConstant" paramcount="2" /> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]