Source code
Description: source: package: org.apache.commons.validator, class: Var

    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 *      https://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} 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     * Constructs a new instance.
    075     */
    076    public Var() {
    077    }
    078
    079    /**
    080     * Constructs a variable with a specified name, value
    081     * and JavaScript type.
    082     *
    083     * @param name Variable name.
    084     * @param value Variable value.
    085     * @param jsType Variable JavaScript type.
    086     */
    087    public Var(final String name, final String value, final String jsType) {
    088        this.name = name;
    089        this.value = value;
    090        this.jsType = jsType;
    091    }
    092
    093    /**
    094     * Creates and returns a copy of this object.
    095     *
    096     * @return A copy of the variable.
    097     */
    098    @Override
    099    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}