org.apache.commons.validator.FormSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-validator Show documentation
Show all versions of commons-validator Show documentation
Apache Commons Validator provides the building blocks for both client side validation and server side data validation.
It may be used standalone or with a framework like Struts.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Holds a set of Form
s stored associated with a Locale
* based on the country, language, and variant specified. Instances of this
* class are configured with a <formset> xml element.
*
* @version $Revision: 1739361 $
*/
public class FormSet implements Serializable {
private static final long serialVersionUID = -8936513232763306055L;
/** Logging */
private transient Log log = LogFactory.getLog(FormSet.class);
/**
* Whether or not the this FormSet
was processed for replacing
* variables in strings with their values.
*/
private boolean processed = false;
/** Language component of Locale
(required). */
private String language = null;
/** Country component of Locale
(optional). */
private String country = null;
/** Variant component of Locale
(optional). */
private String variant = null;
/**
* A Map
of Form
s using the name field of the
* Form
as the key.
*/
private final Map forms = new HashMap();
/**
* A Map
of Constant
s using the name field of the
* Constant
as the key.
*/
private final Map constants = new HashMap();
/**
* This is the type of FormSet
s where no locale is specified.
*/
protected final static int GLOBAL_FORMSET = 1;
/**
* This is the type of FormSet
s where only language locale is
* specified.
*/
protected final static int LANGUAGE_FORMSET = 2;
/**
* This is the type of FormSet
s where only language and country
* locale are specified.
*/
protected final static int COUNTRY_FORMSET = 3;
/**
* This is the type of FormSet
s where full locale has been set.
*/
protected final static int VARIANT_FORMSET = 4;
/**
* Flag indicating if this formSet has been merged with its parent (higher
* rank in Locale hierarchy).
*/
private boolean merged;
/**
* Has this formSet been merged?
*
* @return true if it has been merged
* @since Validator 1.2.0
*/
protected boolean isMerged() {
return merged;
}
/**
* Returns the type of FormSet
:GLOBAL_FORMSET
,
* LANGUAGE_FORMSET
,COUNTRY_FORMSET
or VARIANT_FORMSET
* .
*
* @return The type value
* @since Validator 1.2.0
* @throws NullPointerException if there is inconsistency in the locale
* definition (not sure about this)
*/
protected int getType() {
if (getVariant() != null) {
if (getLanguage() == null || getCountry() == null) {
throw new NullPointerException(
"When variant is specified, country and language must be specified.");
}
return VARIANT_FORMSET;
}
else if (getCountry() != null) {
if (getLanguage() == null) {
throw new NullPointerException(
"When country is specified, language must be specified.");
}
return COUNTRY_FORMSET;
}
else if (getLanguage() != null) {
return LANGUAGE_FORMSET;
}
else {
return GLOBAL_FORMSET;
}
}
/**
* Merges the given FormSet
into this one. If any of depends
* s Forms
are not in this FormSet
then, include
* them, else merge both Forms
. Theoretically we should only
* merge a "parent" formSet.
*
* @param depends FormSet to be merged
* @since Validator 1.2.0
*/
protected void merge(FormSet depends) {
if (depends != null) {
Map pForms = getForms();
Map dForms = depends.getForms();
for (Iterator> it = dForms.entrySet().iterator(); it.hasNext(); ) {
Entry entry = it.next();
String key = entry.getKey();
Form pForm = pForms.get(key);
if (pForm != null) {//merge, but principal 'rules', don't overwrite
// anything
pForm.merge(entry.getValue());
}
else {//just add
addForm(entry.getValue());
}
}
}
merged = true;
}
/**
* Whether or not the this FormSet
was processed for replacing
* variables in strings with their values.
*
* @return The processed value
*/
public boolean isProcessed() {
return processed;
}
/**
* Gets the equivalent of the language component of Locale
.
*
* @return The language value
*/
public String getLanguage() {
return language;
}
/**
* Sets the equivalent of the language component of Locale
.
*
* @param language The new language value
*/
public void setLanguage(String language) {
this.language = language;
}
/**
* Gets the equivalent of the country component of Locale
.
*
* @return The country value
*/
public String getCountry() {
return country;
}
/**
* Sets the equivalent of the country component of Locale
.
*
* @param country The new country value
*/
public void setCountry(String country) {
this.country = country;
}
/**
* Gets the equivalent of the variant component of Locale
.
*
* @return The variant value
*/
public String getVariant() {
return variant;
}
/**
* Sets the equivalent of the variant component of Locale
.
*
* @param variant The new variant value
*/
public void setVariant(String variant) {
this.variant = variant;
}
/**
* Add a Constant
to the locale level.
*
* @param name The constant name
* @param value The constant value
*/
public void addConstant(String name, String value) {
if (constants.containsKey(name)) {
getLog().error("Constant '" + name + "' already exists in FormSet["
+ this.displayKey() + "] - ignoring.");
} else {
constants.put(name, value);
}
}
/**
* Add a Form
to the FormSet
.
*
* @param f The form
*/
public void addForm(Form f) {
String formName = f.getName();
if (forms.containsKey(formName)) {
getLog().error("Form '" + formName + "' already exists in FormSet["
+ this.displayKey() + "] - ignoring.");
} else {
forms.put(f.getName(), f);
}
}
/**
* Retrieve a Form
based on the form name.
*
* @param formName The form name
* @return The form
*/
public Form getForm(String formName) {
return this.forms.get(formName);
}
/**
* A Map
of Form
s is returned as an unmodifiable
* Map
with the key based on the form name.
*
* @return The forms map
*/
public Map getForms() {
return Collections.unmodifiableMap(forms);
}
/**
* Processes all of the Form
s.
*
* @param globalConstants Global constants
*/
synchronized void process(Map globalConstants) {
for (Iterator