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       * @since 1.1
46       */
47      protected String bundle;
48  
49      /**
50       * The key or value of the argument.
51       */
52      protected String key;
53  
54      /**
55       * The name dependency that this argument goes with (optional).
56       */
57      protected String name;
58  
59      /**
60       * This argument's position in the message. Set position=0 to
61       * make a replacement in this string: "some msg {0}".
62       * @since 1.1
63       */
64      protected int position = -1;
65  
66      /**
67       * Whether or not the key is a message resource (optional).  Defaults to
68       * true.  If it is 'true', the value will try to be resolved as a message
69       * resource.
70       */
71      protected boolean resource = true;
72  
73      /**
74       * Constructs a new instance.
75       */
76      public Arg() {
77          // empty
78      }
79  
80      /**
81       * Creates and returns a copy of this object.
82       * @return A copy of this object.
83       */
84      @Override
85      public Object clone() {
86          try {
87              return super.clone();
88          } catch (final CloneNotSupportedException e) {
89              throw new UnsupportedOperationException(e.toString(), e);
90          }
91      }
92  
93      /**
94       * Gets the resource bundle name.
95       *
96       * @return the bundle name.
97       * @since 1.1
98       */
99      public String getBundle() {
100         return bundle;
101     }
102 
103     /**
104      * Gets the key/value.
105      *
106      * @return the key value.
107      */
108     public String getKey() {
109         return key;
110     }
111 
112     /**
113      * Gets the name of the dependency.
114      *
115      * @return the name of the dependency.
116      */
117     public String getName() {
118         return name;
119     }
120 
121     /**
122      * Gets the replacement position.
123      *
124      * @return This replacement position.
125      */
126     public int getPosition() {
127         return position;
128     }
129 
130     /**
131      * Tests whether or not the key is a resource key or literal value.
132      *
133      * @return {@code true} if key is a resource key.
134      */
135     public boolean isResource() {
136         return resource;
137     }
138 
139     /**
140      * Sets the resource bundle name.
141      *
142      * @param bundle The new bundle name.
143      * @since 1.1
144      */
145     public void setBundle(final String bundle) {
146         this.bundle = bundle;
147     }
148 
149     /**
150      * Sets the key/value.
151      *
152      * @param key They to access the argument.
153      */
154     public void setKey(final String key) {
155         this.key = key;
156     }
157 
158     /**
159      * Sets the name of the dependency.
160      *
161      * @param name the name of the dependency.
162      */
163     public void setName(final String name) {
164         this.name = name;
165     }
166 
167     /**
168      * Sets this argument's replacement position.
169      *
170      * @param position set this argument's replacement position.
171      */
172     public void setPosition(final int position) {
173         this.position = position;
174     }
175 
176     /**
177      * Sets whether or not the key is a resource.
178      *
179      * @param resource If true indicates the key is a resource.
180      */
181     public void setResource(final boolean resource) {
182         this.resource = resource;
183     }
184 
185     /**
186      * Returns a string representation of the object.
187      *
188      * @return a string representation of the object.
189      */
190     @Override
191     public String toString() {
192         // @formatter:off
193         return new StringBuilder()
194             .append("Arg: name=")
195             .append(name)
196             .append("  key=")
197             .append(key)
198             .append("  position=")
199             .append(position)
200             .append("  bundle=")
201             .append(bundle)
202             .append("  resource=")
203             .append(resource)
204             .append("\n")
205             .toString();
206         // @formatter:on
207     }
208 
209 }