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
21 /**
22 * A variable that can be associated with a {@code Field} for
23 * passing in information to a pluggable validator. Instances of this class are
24 * configured with a <var> 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 * Constructs a new instance.
75 */
76 public Var() {
77 }
78
79 /**
80 * Constructs a variable with a specified name, value
81 * and JavaScript type.
82 *
83 * @param name Variable name.
84 * @param value Variable value.
85 * @param jsType Variable JavaScript type.
86 */
87 public Var(final String name, final String value, final String jsType) {
88 this.name = name;
89 this.value = value;
90 this.jsType = jsType;
91 }
92
93 /**
94 * Creates and returns a copy of this object.
95 *
96 * @return A copy of the variable.
97 */
98 @Override
99 public Object clone() {
100 try {
101 return super.clone();
102
103 } catch (final CloneNotSupportedException e) {
104 throw new UnsupportedOperationException(e.toString(), e);
105 }
106 }
107
108 /**
109 * Returns the resource bundle name.
110 *
111 * @return The bundle name.
112 * @since 1.2.0
113 */
114 public String getBundle() {
115 return bundle;
116 }
117
118 /**
119 * Gets the JavaScript type of the variable.
120 *
121 * @return The JavaScript type of the variable.
122 */
123 public String getJsType() {
124 return jsType;
125 }
126
127 /**
128 * Gets the name of the variable.
129 *
130 * @return The name of the variable.
131 */
132 public String getName() {
133 return name;
134 }
135
136 /**
137 * Gets the value of the variable.
138 *
139 * @return The value of the variable.
140 */
141 public String getValue() {
142 return value;
143 }
144
145 /**
146 * Tests whether or not the value is a resource key or literal value.
147 *
148 * @return {@code true} if value is a resource key.
149 * @since 1.2.0
150 */
151 public boolean isResource() {
152 return resource;
153 }
154
155 /**
156 * Sets the resource bundle name.
157 *
158 * @param bundle The new bundle name.
159 * @since 1.2.0
160 */
161 public void setBundle(final String bundle) {
162 this.bundle = bundle;
163 }
164
165 /**
166 * Sets the JavaScript type of the variable.
167 *
168 * @param jsType The JavaScript type of the variable.
169 */
170 public void setJsType(final String jsType) {
171 this.jsType = jsType;
172 }
173
174 /**
175 * Sets the name of the variable.
176 *
177 * @param name The name of the variable.
178 */
179 public void setName(final String name) {
180 this.name = name;
181 }
182
183 /**
184 * Sets whether or not the value is a resource.
185 *
186 * @param resource If true indicates the value is a resource.
187 * @since 1.2.0
188 */
189 public void setResource(final boolean resource) {
190 this.resource = resource;
191 }
192
193 /**
194 * Sets the value of the variable.
195 *
196 * @param value The value of the variable.
197 */
198 public void setValue(final String value) {
199 this.value = value;
200 }
201
202 /**
203 * Returns a string representation of the object.
204 *
205 * @return A string representation of the variable.
206 */
207 @Override
208 public String toString() {
209 final StringBuilder results = new StringBuilder();
210
211 results.append("Var: name=");
212 results.append(name);
213 results.append(" value=");
214 results.append(value);
215 results.append(" resource=");
216 results.append(resource);
217 if (resource) {
218 results.append(" bundle=");
219 results.append(bundle);
220 }
221 results.append(" jsType=");
222 results.append(jsType);
223 results.append("\n");
224
225 return results.toString();
226 }
227
228 }