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 * @version $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Do, 05 Jan 2012) $
037 */
038public class Arg implements Cloneable, Serializable {
039
040    private static final long serialVersionUID = -8922606779669839294L;
041
042    /**
043     * The resource bundle name that this Arg's <code>key</code> should be
044     * resolved in (optional).
045     * @since Validator 1.1
046     */
047    protected String bundle = null;
048
049    /**
050     * The key or value of the argument.
051     */
052    protected String key = null;
053
054    /**
055     * The name dependency that this argument goes with (optional).
056     */
057    protected String name = null;
058
059    /**
060     * This argument's position in the message. Set postion=0 to
061     * make a replacement in this string: "some msg {0}".
062     * @since Validator 1.1
063     */
064    protected int position = -1;
065
066    /**
067     * Whether or not the key is a message resource (optional).  Defaults to
068     * true.  If it is 'true', the value will try to be resolved as a message
069     * resource.
070     */
071    protected boolean resource = true;
072
073    /**
074     * Creates and returns a copy of this object.
075     * @return A copy of this object.
076     */
077    public Object clone() {
078        try {
079            return super.clone();
080
081        } catch(CloneNotSupportedException e) {
082            throw new RuntimeException(e.toString());
083        }
084    }
085
086    /**
087     * Returns the resource bundle name.
088     * @return the bundle name.
089     * @since Validator 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</code> 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 Validator 1.1
131     */
132    public void setBundle(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(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(String name) {
149        this.name = name;
150    }
151
152    /**
153     * Set this argument's replacement position.
154     * @param position set this argument's replacement position.
155     */
156    public void setPosition(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(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    public String toString() {
173        StringBuffer results = new StringBuffer();
174
175        results.append("Arg: name=");
176        results.append(name);
177        results.append("  key=");
178        results.append(key);
179        results.append("  position=");
180        results.append(position);
181        results.append("  bundle=");
182        results.append(bundle);
183        results.append("  resource=");
184        results.append(resource);
185        results.append("\n");
186
187        return results.toString();
188    }
189
190}