001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.validator;
018
019import java.io.Serializable;
020
021/**
022 * A variable that can be associated with a <code>Field</code> for
023 * passing in information to a pluggable validator.  Instances of this class are
024 * configured with a &lt;var&gt; xml element.
025 */
026public class Var implements Cloneable, Serializable {
027
028    private static final long serialVersionUID = -684185211548420224L;
029
030    /**
031     * Int Constant for JavaScript type.  This can be used
032     * when auto-generating JavaScript.
033     */
034    public static final String JSTYPE_INT = "int";
035
036    /**
037     * String Constant for JavaScript type.  This can be used
038     * when auto-generating JavaScript.
039     */
040    public static final String JSTYPE_STRING = "string";
041
042    /**
043     * Regular Expression Constant for JavaScript type.  This can be used
044     * when auto-generating JavaScript.
045     */
046    public static final String JSTYPE_REGEXP = "regexp";
047
048    /**
049     * The name of the variable.
050     */
051    private String name;
052
053    /**
054     * The key or value the variable.
055     */
056    private String value;
057
058    /**
059     * The optional JavaScript type of the variable.
060     */
061    private String jsType;
062
063    /**
064     * Whether the variable is a resource [false]
065     */
066    private boolean resource;
067
068    /**
069     * The bundle for a variable (when resource = 'true').
070     */
071    private String bundle;
072
073    /**
074     * Default Constructor.
075     */
076    public Var() {
077    }
078
079    /**
080     * Constructs a variable with a specified name, value
081     * and JavaScript type.
082     * @param name Variable name.
083     * @param value Variable value.
084     * @param jsType Variable JavaScript type.
085     */
086    public Var(final String name, final String value, final String jsType) {
087        this.name = name;
088        this.value = value;
089        this.jsType = jsType;
090    }
091
092    /**
093     * Creates and returns a copy of this object.
094     * @return A copy of the variable.
095     */
096    @Override
097    public Object clone() {
098        try {
099            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}