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: 1739356 $
037 */
038//TODO mutable non-private fields
039public class Arg implements Cloneable, Serializable {
040
041    private static final long serialVersionUID = -8922606779669839294L;
042
043    /**
044     * The resource bundle name that this Arg's <code>key</code> should be
045     * resolved in (optional).
046     * @since Validator 1.1
047     */
048    protected String bundle = null;
049
050    /**
051     * The key or value of the argument.
052     */
053    protected String key = null;
054
055    /**
056     * The name dependency that this argument goes with (optional).
057     */
058    protected String name = null;
059
060    /**
061     * This argument's position in the message. Set postion=0 to
062     * make a replacement in this string: "some msg {0}".
063     * @since Validator 1.1
064     */
065    protected int position = -1;
066
067    /**
068     * Whether or not the key is a message resource (optional).  Defaults to
069     * true.  If it is 'true', the value will try to be resolved as a message
070     * resource.
071     */
072    protected boolean resource = true;
073
074    /**
075     * Creates and returns a copy of this object.
076     * @return A copy of this object.
077     */
078    @Override
079    public Object clone() {
080        try {
081            return super.clone();
082
083        } catch(CloneNotSupportedException e) {
084            throw new RuntimeException(e.toString());
085        }
086    }
087
088    /**
089     * Returns the resource bundle name.
090     * @return the bundle name.
091     * @since Validator 1.1
092     */
093    public String getBundle() {
094        return this.bundle;
095    }
096
097    /**
098     * Gets the key/value.
099     * @return the key value.
100     */
101    public String getKey() {
102        return this.key;
103    }
104
105    /**
106     * Gets the name of the dependency.
107     * @return the name of the dependency.
108     */
109    public String getName() {
110        return this.name;
111    }
112
113    /**
114     * Argument's replacement position.
115     * @return This argument's replacement position.
116     */
117    public int getPosition() {
118        return this.position;
119    }
120
121    /**
122     * Tests whether or not the key is a resource key or literal value.
123     * @return <code>true</code> if key is a resource key.
124     */
125    public boolean isResource() {
126        return this.resource;
127    }
128
129    /**
130     * Sets the resource bundle name.
131     * @param bundle The new bundle name.
132     * @since Validator 1.1
133     */
134    public void setBundle(String bundle) {
135        this.bundle = bundle;
136    }
137
138    /**
139     * Sets the key/value.
140     * @param key They to access the argument.
141     */
142    public void setKey(String key) {
143        this.key = key;
144    }
145
146    /**
147     * Sets the name of the dependency.
148     * @param name the name of the dependency.
149     */
150    public void setName(String name) {
151        this.name = name;
152    }
153
154    /**
155     * Set this argument's replacement position.
156     * @param position set this argument's replacement position.
157     */
158    public void setPosition(int position) {
159        this.position = position;
160    }
161
162    /**
163     * Sets whether or not the key is a resource.
164     * @param resource If true indicates the key is a resource.
165     */
166    public void setResource(boolean resource) {
167        this.resource = resource;
168    }
169
170    /**
171     * Returns a string representation of the object.
172     * @return a string representation of the object.
173     */
174    @Override
175    public String toString() {
176        StringBuilder results = new StringBuilder();
177
178        results.append("Arg: name=");
179        results.append(name);
180        results.append("  key=");
181        results.append(key);
182        results.append("  position=");
183        results.append(position);
184        results.append("  bundle=");
185        results.append(bundle);
186        results.append("  resource=");
187        results.append(resource);
188        results.append("\n");
189
190        return results.toString();
191    }
192
193}