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
081        } catch (final CloneNotSupportedException e) {
082            throw new UnsupportedOperationException(e.toString(), e);
083        }
084    }
085
086    /**
087     * Returns the resource bundle name.
088     * @return the bundle name.
089     * @since 1.1
090     */
091    public String getBundle() {
092        return this.bundle;
093    }
094
095    /**
096     * Gets the key/value.
097     * @return the key value.
098     */
099    public String getKey() {
100        return this.key;
101    }
102
103    /**
104     * Gets the name of the dependency.
105     * @return the name of the dependency.
106     */
107    public String getName() {
108        return this.name;
109    }
110
111    /**
112     * Argument's replacement position.
113     * @return This argument's replacement position.
114     */
115    public int getPosition() {
116        return this.position;
117    }
118
119    /**
120     * Tests whether or not the key is a resource key or literal value.
121     * @return {@code true} if key is a resource key.
122     */
123    public boolean isResource() {
124        return this.resource;
125    }
126
127    /**
128     * Sets the resource bundle name.
129     * @param bundle The new bundle name.
130     * @since 1.1
131     */
132    public void setBundle(final String bundle) {
133        this.bundle = bundle;
134    }
135
136    /**
137     * Sets the key/value.
138     * @param key They to access the argument.
139     */
140    public void setKey(final String key) {
141        this.key = key;
142    }
143
144    /**
145     * Sets the name of the dependency.
146     * @param name the name of the dependency.
147     */
148    public void setName(final String name) {
149        this.name = name;
150    }
151
152    /**
153     * Sets this argument's replacement position.
154     * @param position set this argument's replacement position.
155     */
156    public void setPosition(final int position) {
157        this.position = position;
158    }
159
160    /**
161     * Sets whether or not the key is a resource.
162     * @param resource If true indicates the key is a resource.
163     */
164    public void setResource(final boolean resource) {
165        this.resource = resource;
166    }
167
168    /**
169     * Returns a string representation of the object.
170     * @return a string representation of the object.
171     */
172    @Override
173    public String toString() {
174        final StringBuilder results = new StringBuilder();
175
176        results.append("Arg: name=");
177        results.append(name);
178        results.append("  key=");
179        results.append(key);
180        results.append("  position=");
181        results.append(position);
182        results.append("  bundle=");
183        results.append(bundle);
184        results.append("  resource=");
185        results.append(resource);
186        results.append("\n");
187
188        return results.toString();
189    }
190
191}