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 *
026 * @version $Revision: 1649191 $
027 */
028public class Var implements Cloneable, Serializable {
029
030    private static final long serialVersionUID = -684185211548420224L;
031
032    /**
033     * Int Constant for JavaScript type.  This can be used
034     * when auto-generating JavaScript.
035     */
036    public static final String JSTYPE_INT = "int";
037
038    /**
039     * String Constant for JavaScript type.  This can be used
040     * when auto-generating JavaScript.
041     */
042    public static final String JSTYPE_STRING = "string";
043
044    /**
045     * Regular Expression Constant for JavaScript type.  This can be used
046     * when auto-generating JavaScript.
047     */
048    public static final String JSTYPE_REGEXP = "regexp";
049
050    /**
051     * The name of the variable.
052     */
053    private String name = null;
054
055    /**
056     * The key or value the variable.
057     */
058    private String value = null;
059
060    /**
061     * The optional JavaScript type of the variable.
062     */
063    private String jsType = null;
064
065    /**
066     * Whether the variable is a resource [false]
067     */
068    private boolean resource = false;
069
070    /**
071     * The bundle for a variable (when resource = 'true').
072     */
073    private String bundle = null;
074
075    /**
076     * Default Constructor.
077     */
078    public Var() {
079        super();
080    }
081
082    /**
083     * Constructs a variable with a specified name, value
084     * and Javascript type.
085     * @param name Variable name.
086     * @param value Variable value.
087     * @param jsType Variable Javascript type.
088     */
089    public Var(String name, String value, String jsType) {
090        this.name = name;
091        this.value = value;
092        this.jsType = jsType;
093    }
094
095    /**
096     * Gets the name of the variable.
097     * @return The name of the variable.
098     */
099    public String getName() {
100        return this.name;
101    }
102
103    /**
104     * Sets the name of the variable.
105     * @param name The name of the variable.
106     */
107    public void setName(String name) {
108        this.name = name;
109    }
110
111    /**
112     * Gets the value of the variable.
113     * @return The value of the variable.
114     */
115    public String getValue() {
116        return this.value;
117    }
118
119    /**
120     * Sets the value of the variable.
121     * @param value The value of the variable.
122     */
123    public void setValue(String value) {
124        this.value = value;
125    }
126
127    /**
128     * Tests whether or not the value is a resource key or literal value.
129     * @return <code>true</code> if value is a resource key.
130     * @since Validator 1.2.0
131     */
132    public boolean isResource() {
133        return this.resource;
134    }
135
136    /**
137     * Sets whether or not the value is a resource.
138     * @param resource If true indicates the value is a resource.
139     * @since Validator 1.2.0
140     */
141    public void setResource(boolean resource) {
142        this.resource = resource;
143    }
144
145    /**
146     * Returns the resource bundle name.
147     * @return The bundle name.
148     * @since Validator 1.2.0
149     */
150    public String getBundle() {
151        return this.bundle;
152    }
153
154    /**
155     * Sets the resource bundle name.
156     * @param bundle The new bundle name.
157     * @since Validator 1.2.0
158     */
159    public void setBundle(String bundle) {
160        this.bundle = bundle;
161    }
162
163    /**
164     * Gets the JavaScript type of the variable.
165     * @return The Javascript type of the variable.
166     */
167    public String getJsType() {
168        return this.jsType;
169    }
170
171    /**
172     * Sets the JavaScript type of the variable.
173     * @param jsType The Javascript type of the variable.
174     */
175    public void setJsType(String jsType) {
176        this.jsType = jsType;
177    }
178
179    /**
180     * Creates and returns a copy of this object.
181     * @return A copy of the variable.
182     */
183    public Object clone() {
184        try {
185            return super.clone();
186
187        } catch(CloneNotSupportedException e) {
188            throw new RuntimeException(e.toString());
189        }
190    }
191
192    /**
193     * Returns a string representation of the object.
194     * @return A string representation of the variable.
195     */
196    public String toString() {
197        StringBuffer results = new StringBuffer();
198
199        results.append("Var: name=");
200        results.append(name);
201        results.append("  value=");
202        results.append(value);
203        results.append("  resource=");
204        results.append(resource);
205        if (resource) {
206            results.append("  bundle=");
207            results.append(bundle);
208        }
209        results.append("  jsType=");
210        results.append(jsType);
211        results.append("\n");
212
213        return results.toString();
214    }
215
216}