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    *      https://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  import java.text.MessageFormat;
21  
22  /**
23   * <p>
24   * A default argument or an argument for a
25   * specific validator definition (ex: required)
26   * can be stored to pass into a message as parameters.  This can be used in a
27   * pluggable validator for constructing locale
28   * sensitive messages by using {@link MessageFormat}
29   * or an equivalent class.  The resource field can be
30   * used to determine if the value stored in the argument
31   * is a value to be retrieved from a locale sensitive
32   * message retrieval system like {@code java.util.PropertyResourceBundle}.
33   * The resource field defaults to 'true'.
34   * </p>
35   * <p>Instances of this class are configured with an &lt;arg&gt; xml element.</p>
36   */
37  //TODO mutable non-private fields
38  public class Arg implements Cloneable, Serializable {
39  
40      private static final long serialVersionUID = -8922606779669839294L;
41  
42      /**
43       * The resource bundle name that this Arg's {@code key} should be
44       * resolved in (optional).
45       *
46       * @since 1.1
47       */
48      protected String bundle;
49  
50      /**
51       * The key or value of the argument.
52       */
53      protected String key;
54  
55      /**
56       * The name dependency that this argument goes with (optional).
57       */
58      protected String name;
59  
60      /**
61       * This argument's position in the message. Set position=0 to
62       * make a replacement in this string: "some msg {0}".
63       *
64       * @since 1.1
65       */
66      protected int position = -1;
67  
68      /**
69       * Whether or not the key is a message resource (optional).  Defaults to
70       * true.  If it is 'true', the value will try to be resolved as a message
71       * resource.
72       */
73      protected boolean resource = true;
74  
75      /**
76       * Constructs a new instance.
77       */
78      public Arg() {
79          // empty
80      }
81  
82      /**
83       * Creates and returns a copy of this object.
84       *
85       * @return A copy of this object.
86       */
87      @Override
88      public Object clone() {
89          try {
90              return super.clone();
91          } catch (final CloneNotSupportedException e) {
92              throw new UnsupportedOperationException(e.toString(), e);
93          }
94      }
95  
96      /**
97       * Gets the resource bundle name.
98       *
99       * @return the bundle name.
100      * @since 1.1
101      */
102     public String getBundle() {
103         return bundle;
104     }
105 
106     /**
107      * Gets the key/value.
108      *
109      * @return the key value.
110      */
111     public String getKey() {
112         return key;
113     }
114 
115     /**
116      * Gets the name of the dependency.
117      *
118      * @return the name of the dependency.
119      */
120     public String getName() {
121         return name;
122     }
123 
124     /**
125      * Gets the replacement position.
126      *
127      * @return This replacement position.
128      */
129     public int getPosition() {
130         return position;
131     }
132 
133     /**
134      * Tests whether or not the key is a resource key or literal value.
135      *
136      * @return {@code true} if key is a resource key.
137      */
138     public boolean isResource() {
139         return resource;
140     }
141 
142     /**
143      * Sets the resource bundle name.
144      *
145      * @param bundle The new bundle name.
146      * @since 1.1
147      */
148     public void setBundle(final String bundle) {
149         this.bundle = bundle;
150     }
151 
152     /**
153      * Sets the key/value.
154      *
155      * @param key They to access the argument.
156      */
157     public void setKey(final String key) {
158         this.key = key;
159     }
160 
161     /**
162      * Sets the name of the dependency.
163      *
164      * @param name the name of the dependency.
165      */
166     public void setName(final String name) {
167         this.name = name;
168     }
169 
170     /**
171      * Sets this argument's replacement position.
172      *
173      * @param position set this argument's replacement position.
174      */
175     public void setPosition(final int position) {
176         this.position = position;
177     }
178 
179     /**
180      * Sets whether or not the key is a resource.
181      *
182      * @param resource If true indicates the key is a resource.
183      */
184     public void setResource(final boolean resource) {
185         this.resource = resource;
186     }
187 
188     /**
189      * Returns a string representation of the object.
190      *
191      * @return a string representation of the object.
192      */
193     @Override
194     public String toString() {
195         // @formatter:off
196         return new StringBuilder()
197             .append("Arg: name=")
198             .append(name)
199             .append("  key=")
200             .append(key)
201             .append("  position=")
202             .append(position)
203             .append("  bundle=")
204             .append(bundle)
205             .append("  resource=")
206             .append(resource)
207             .append("\n")
208             .toString();
209         // @formatter:on
210     }
211 
212 }