All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.validator.FormSet Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.8.0
Show newest version
/*
 * 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 Forms 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$
 */
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 Forms using the name field of the
     * Form as the key.
     */
    private final Map forms = new HashMap();

    /**
     * A Map of Constants using the name field of the
     * Constant as the key.
     */
    private final Map constants = new HashMap();

    /**
     * This is the type of FormSets where no locale is specified.
     */
    protected final static int GLOBAL_FORMSET = 1;

    /**
     * This is the type of FormSets where only language locale is
     * specified.
     */
    protected final static int LANGUAGE_FORMSET = 2;

    /**
     * This is the type of FormSets where only language and country
     * locale are specified.
     */
    protected final static int COUNTRY_FORMSET = 3;

    /**
     * This is the type of FormSets 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 Forms 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 Forms.
     *
     * @param globalConstants  Global constants
     */
    synchronized void process(Map globalConstants) {
        for (Iterator
i = forms.values().iterator(); i.hasNext(); ) { Form f = i.next(); f.process(globalConstants, constants, forms); } processed = true; } /** * Returns a string representation of the object's key. * * @return A string representation of the key */ public String displayKey() { StringBuilder results = new StringBuilder(); 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(); } /** * Returns a string representation of the object. * * @return A string representation */ @Override public String toString() { StringBuilder results = new StringBuilder(); results.append("FormSet: language="); results.append(language); results.append(" country="); results.append(country); results.append(" variant="); results.append(variant); results.append("\n"); for (Iterator i = getForms().values().iterator(); i.hasNext(); ) { results.append(" "); results.append(i.next()); results.append("\n"); } return results.toString(); } /** * Accessor method for Log instance. * * The Log instance variable is transient and * accessing it through this method ensures it * is re-initialized when this instance is * de-serialized. * * @return The Log instance. */ private Log getLog() { if (log == null) { log = LogFactory.getLog(FormSet.class); } return log; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy