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   * A variable that can be associated with a <code>Field</code> for
23   * passing in information to a pluggable validator.  Instances of this class are
24   * configured with a &lt;var&gt; xml element.
25   */
26  public class Var implements Cloneable, Serializable {
27  
28      private static final long serialVersionUID = -684185211548420224L;
29  
30      /**
31       * Int Constant for JavaScript type.  This can be used
32       * when auto-generating JavaScript.
33       */
34      public static final String JSTYPE_INT = "int";
35  
36      /**
37       * String Constant for JavaScript type.  This can be used
38       * when auto-generating JavaScript.
39       */
40      public static final String JSTYPE_STRING = "string";
41  
42      /**
43       * Regular Expression Constant for JavaScript type.  This can be used
44       * when auto-generating JavaScript.
45       */
46      public static final String JSTYPE_REGEXP = "regexp";
47  
48      /**
49       * The name of the variable.
50       */
51      private String name;
52  
53      /**
54       * The key or value the variable.
55       */
56      private String value;
57  
58      /**
59       * The optional JavaScript type of the variable.
60       */
61      private String jsType;
62  
63      /**
64       * Whether the variable is a resource [false]
65       */
66      private boolean resource;
67  
68      /**
69       * The bundle for a variable (when resource = 'true').
70       */
71      private String bundle;
72  
73      /**
74       * Default Constructor.
75       */
76      public Var() {
77      }
78  
79      /**
80       * Constructs a variable with a specified name, value
81       * and JavaScript type.
82       * @param name Variable name.
83       * @param value Variable value.
84       * @param jsType Variable JavaScript type.
85       */
86      public Var(final String name, final String value, final String jsType) {
87          this.name = name;
88          this.value = value;
89          this.jsType = jsType;
90      }
91  
92      /**
93       * Creates and returns a copy of this object.
94       * @return A copy of the variable.
95       */
96      @Override
97      public Object clone() {
98          try {
99              return super.clone();
100 
101         } catch (final CloneNotSupportedException e) {
102             throw new UnsupportedOperationException(e.toString(), e);
103         }
104     }
105 
106     /**
107      * Returns the resource bundle name.
108      * @return The bundle name.
109      * @since 1.2.0
110      */
111     public String getBundle() {
112         return this.bundle;
113     }
114 
115     /**
116      * Gets the JavaScript type of the variable.
117      * @return The JavaScript type of the variable.
118      */
119     public String getJsType() {
120         return this.jsType;
121     }
122 
123     /**
124      * Gets the name of the variable.
125      * @return The name of the variable.
126      */
127     public String getName() {
128         return this.name;
129     }
130 
131     /**
132      * Gets the value of the variable.
133      * @return The value of the variable.
134      */
135     public String getValue() {
136         return this.value;
137     }
138 
139     /**
140      * Tests whether or not the value is a resource key or literal value.
141      * @return {@code true} if value is a resource key.
142      * @since 1.2.0
143      */
144     public boolean isResource() {
145         return this.resource;
146     }
147 
148     /**
149      * Sets the resource bundle name.
150      * @param bundle The new bundle name.
151      * @since 1.2.0
152      */
153     public void setBundle(final String bundle) {
154         this.bundle = bundle;
155     }
156 
157     /**
158      * Sets the JavaScript type of the variable.
159      * @param jsType The JavaScript type of the variable.
160      */
161     public void setJsType(final String jsType) {
162         this.jsType = jsType;
163     }
164 
165     /**
166      * Sets the name of the variable.
167      * @param name The name of the variable.
168      */
169     public void setName(final String name) {
170         this.name = name;
171     }
172 
173     /**
174      * Sets whether or not the value is a resource.
175      * @param resource If true indicates the value is a resource.
176      * @since 1.2.0
177      */
178     public void setResource(final boolean resource) {
179         this.resource = resource;
180     }
181 
182     /**
183      * Sets the value of the variable.
184      * @param value The value of the variable.
185      */
186     public void setValue(final String value) {
187         this.value = value;
188     }
189 
190     /**
191      * Returns a string representation of the object.
192      * @return A string representation of the variable.
193      */
194     @Override
195     public String toString() {
196         final StringBuilder results = new StringBuilder();
197 
198         results.append("Var: name=");
199         results.append(name);
200         results.append("  value=");
201         results.append(value);
202         results.append("  resource=");
203         results.append(resource);
204         if (resource) {
205             results.append("  bundle=");
206             results.append(bundle);
207         }
208         results.append("  jsType=");
209         results.append(jsType);
210         results.append("\n");
211 
212         return results.toString();
213     }
214 
215 }