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     */
017    package org.apache.commons.validator;
018    
019    import 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: 493905 $ $Date: 2007-01-08 03:11:38 +0100 (Mo, 08. Jan 2007) $
027     */
028    public class Var implements Cloneable, Serializable {
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 = null;
052    
053        /**
054         * The key or value the variable.
055         */
056        private String value = null;
057    
058        /**
059         * The optional JavaScript type of the variable.
060         */
061        private String jsType = null;
062    
063        /**
064         * Whether the variable is a resource [false]
065         */
066        private boolean resource = false;
067    
068        /**
069         * The bundle for a variable (when resource = 'true').
070         */
071        private String bundle = null;
072    
073        /**
074         * Default Constructor.
075         */
076        public Var() {
077            super();
078        }
079    
080        /**
081         * Constructs a variable with a specified name, value
082         * and Javascript type.
083         * @param name Variable name.
084         * @param value Variable value.
085         * @param jsType Variable Javascript type.
086         */
087        public Var(String name, String value, String jsType) {
088            this.name = name;
089            this.value = value;
090            this.jsType = jsType;
091        }
092    
093        /**
094         * Gets the name of the variable.
095         * @return The name of the variable.
096         */
097        public String getName() {
098            return this.name;
099        }
100    
101        /**
102         * Sets the name of the variable.
103         * @param name The name of the variable.
104         */
105        public void setName(String name) {
106            this.name = name;
107        }
108    
109        /**
110         * Gets the value of the variable.
111         * @return The value of the variable.
112         */
113        public String getValue() {
114            return this.value;
115        }
116    
117        /**
118         * Sets the value of the variable.
119         * @param value The value of the variable.
120         */
121        public void setValue(String value) {
122            this.value = value;
123        }
124    
125        /**
126         * Tests whether or not the value is a resource key or literal value.
127         * @return <code>true</code> if value is a resource key.
128         * @since Validator 1.2.0
129         */
130        public boolean isResource() {
131            return this.resource;
132        }
133    
134        /**
135         * Sets whether or not the value is a resource.
136         * @param resource If true indicates the value is a resource.
137         * @since Validator 1.2.0
138         */
139        public void setResource(boolean resource) {
140            this.resource = resource;
141        }
142    
143        /**
144         * Returns the resource bundle name.
145         * @return The bundle name.
146         * @since Validator 1.2.0
147         */
148        public String getBundle() {
149            return this.bundle;
150        }
151    
152        /**
153         * Sets the resource bundle name.
154         * @param bundle The new bundle name.
155         * @since Validator 1.2.0
156         */
157        public void setBundle(String bundle) {
158            this.bundle = bundle;
159        }
160    
161        /**
162         * Gets the JavaScript type of the variable.
163         * @return The Javascript type of the variable.
164         */
165        public String getJsType() {
166            return this.jsType;
167        }
168    
169        /**
170         * Sets the JavaScript type of the variable.
171         * @param jsType The Javascript type of the variable.
172         */
173        public void setJsType(String jsType) {
174            this.jsType = jsType;
175        }
176    
177        /**
178         * Creates and returns a copy of this object.
179         * @return A copy of the variable.
180         */
181        public Object clone() {
182            try {
183                return super.clone();
184    
185            } catch(CloneNotSupportedException e) {
186                throw new RuntimeException(e.toString());
187            }
188        }
189    
190        /**
191         * Returns a string representation of the object.
192         * @return A string representation of the variable.
193         */
194        public String toString() {
195            StringBuffer results = new StringBuffer();
196    
197            results.append("Var: name=");
198            results.append(name);
199            results.append("  value=");
200            results.append(value);
201            results.append("  resource=");
202            results.append(resource);
203            if (resource) {
204                results.append("  bundle=");
205                results.append(bundle);
206            }
207            results.append("  jsType=");
208            results.append(jsType);
209            results.append("\n");
210    
211            return results.toString();
212        }
213    
214    }