    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 java.io.Serializable;
    020
    021/**
    022 * <p>
    023 * A default argument or an argument for a
    024 * specific validator definition (ex: required)
    025 * can be stored to pass into a message as parameters.  This can be used in a
    026 * pluggable validator for constructing locale
    027 * sensitive messages by using <code>java.text.MessageFormat</code>
    028 * or an equivalent class.  The resource field can be
    029 * used to determine if the value stored in the argument
    030 * is a value to be retrieved from a locale sensitive
    031 * message retrieval system like <code>java.util.PropertyResourceBundle</code>.
    032 * The resource field defaults to 'true'.
    033 * </p>
    034 * <p>Instances of this class are configured with an &lt;arg&gt; xml element.</p>
    035 */
    036//TODO mutable non-private fields
    037public class Arg implements Cloneable, Serializable {
    038
    039    private static final long serialVersionUID = -8922606779669839294L;
    040
    041    /**
    042     * The resource bundle name that this Arg's <code>key</code> should be
    043     * resolved in (optional).
    044     * @since 1.1
    045     */
    046    protected String bundle;
    047
    048    /**
    049     * The key or value of the argument.
    050     */
    051    protected String key;
    052
    053    /**
    054     * The name dependency that this argument goes with (optional).
    055     */
    056    protected String name;
    057
    058    /**
    059     * This argument's position in the message. Set postion=0 to
    060     * make a replacement in this string: "some msg {0}".
    061     * @since 1.1
    062     */
    063    protected int position = -1;
    064
    065    /**
    066     * Whether or not the key is a message resource (optional).  Defaults to
    067     * true.  If it is 'true', the value will try to be resolved as a message
    068     * resource.
    069     */
    070    protected boolean resource = true;
    071
    072    /**
    073     * Creates and returns a copy of this object.
    074     * @return A copy of this object.
    075     */
    076    @Override
    077    public Object clone() {
    078        try {
    079            return super.clone();
    080        } catch (final CloneNotSupportedException e) {
    081            throw new UnsupportedOperationException(e.toString(), e);
    082        }
    083    }
    084
    085    /**
    086     * Returns the resource bundle name.
    087     * @return the bundle name.
    088     * @since 1.1
    089     */
    090    public String getBundle() {
    091        return this.bundle;
    092    }
    093
    094    /**
    095     * Gets the key/value.
    096     * @return the key value.
    097     */
    098    public String getKey() {
    099        return this.key;
    100    }
    101
    102    /**
    103     * Gets the name of the dependency.
    104     * @return the name of the dependency.
    105     */
    106    public String getName() {
    107        return this.name;
    108    }
    109
    110    /**
    111     * Argument's replacement position.
    112     * @return This argument's replacement position.
    113     */
    114    public int getPosition() {
    115        return this.position;
    116    }
    117
    118    /**
    119     * Tests whether or not the key is a resource key or literal value.
    120     * @return {@code true} if key is a resource key.
    121     */
    122    public boolean isResource() {
    123        return this.resource;
    124    }
    125
    126    /**
    127     * Sets the resource bundle name.
    128     * @param bundle The new bundle name.
    129     * @since 1.1
    130     */
    131    public void setBundle(final String bundle) {
    132        this.bundle = bundle;
    133    }
    134
    135    /**
    136     * Sets the key/value.
    137     * @param key They to access the argument.
    138     */
    139    public void setKey(final String key) {
    140        this.key = key;
    141    }
    142
    143    /**
    144     * Sets the name of the dependency.
    145     * @param name the name of the dependency.
    146     */
    147    public void setName(final String name) {
    148        this.name = name;
    149    }
    150
    151    /**
    152     * Sets this argument's replacement position.
    153     * @param position set this argument's replacement position.
    154     */
    155    public void setPosition(final int position) {
    156        this.position = position;
    157    }
    158
    159    /**
    160     * Sets whether or not the key is a resource.
    161     * @param resource If true indicates the key is a resource.
    162     */
    163    public void setResource(final boolean resource) {
    164        this.resource = resource;
    165    }
    166
    167    /**
    168     * Returns a string representation of the object.
    169     * @return a string representation of the object.
    170     */
    171    @Override
    172    public String toString() {
    173        // @formatter:off
    174        return new StringBuilder()
    175            .append("Arg: name=")
    176            .append(name)
    177            .append("  key=")
    178            .append(key)
    179            .append("  position=")
    180            .append(position)
    181            .append("  bundle=")
    182            .append(bundle)
    183            .append("  resource=")
    184            .append(resource)
    185            .append("\n")
    186            .toString();
    187        // @formatter:on
    188    }
    189
    190}