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          } catch (final CloneNotSupportedException e) {
81              throw new UnsupportedOperationException(e.toString(), e);
82          }
83      }
84  
85      /**
86       * Returns the resource bundle name.
87       * @return the bundle name.
88       * @since 1.1
89       */
90      public String getBundle() {
91          return this.bundle;
92      }
93  
94      /**
95       * Gets the key/value.
96       * @return the key value.
97       */
98      public String getKey() {
99          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 }