View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator;
18  
19  import java.io.Serializable;
20  
21  /**
22   * <p>
23   * A default argument or an argument for a
24   * specific validator definition (ex: required)
25   * can be stored to pass into a message as parameters.  This can be used in a
26   * pluggable validator for constructing locale
27   * sensitive messages by using <code>java.text.MessageFormat</code>
28   * or an equivalent class.  The resource field can be
29   * used to determine if the value stored in the argument
30   * is a value to be retrieved from a locale sensitive
31   * message retrieval system like <code>java.util.PropertyResourceBundle</code>.
32   * The resource field defaults to 'true'.
33   * </p>
34   * <p>Instances of this class are configured with an &lt;arg&gt; xml element.</p>
35   */
36  //TODO mutable non-private fields
37  public class Arg implements Cloneable, Serializable {
38  
39      private static final long serialVersionUID = -8922606779669839294L;
40  
41      /**
42       * The resource bundle name that this Arg's <code>key</code> should be
43       * resolved in (optional).
44       * @since 1.1
45       */
46      protected String bundle;
47  
48      /**
49       * The key or value of the argument.
50       */
51      protected String key;
52  
53      /**
54       * The name dependency that this argument goes with (optional).
55       */
56      protected String name;
57  
58      /**
59       * This argument's position in the message. Set postion=0 to
60       * make a replacement in this string: "some msg {0}".
61       * @since 1.1
62       */
63      protected int position = -1;
64  
65      /**
66       * Whether or not the key is a message resource (optional).  Defaults to
67       * true.  If it is 'true', the value will try to be resolved as a message
68       * resource.
69       */
70      protected boolean resource = true;
71  
72      /**
73       * Creates and returns a copy of this object.
74       * @return A copy of this object.
75       */
76      @Override
77      public Object clone() {
78          try {
79              return super.clone();
80  
81          } catch (final CloneNotSupportedException e) {
82              throw new UnsupportedOperationException(e.toString(), e);
83          }
84      }
85  
86      /**
87       * Returns the resource bundle name.
88       * @return the bundle name.
89       * @since 1.1
90       */
91      public String getBundle() {
92          return this.bundle;
93      }
94  
95      /**
96       * Gets the key/value.
97       * @return the key value.
98       */
99      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 }