Var.java

  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.  *      http://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. import java.io.Serializable;

  19. /**
  20.  * A variable that can be associated with a <code>Field</code> for
  21.  * passing in information to a pluggable validator.  Instances of this class are
  22.  * configured with a &lt;var&gt; xml element.
  23.  */
  24. public class Var implements Cloneable, Serializable {

  25.     private static final long serialVersionUID = -684185211548420224L;

  26.     /**
  27.      * Int Constant for JavaScript type.  This can be used
  28.      * when auto-generating JavaScript.
  29.      */
  30.     public static final String JSTYPE_INT = "int";

  31.     /**
  32.      * String Constant for JavaScript type.  This can be used
  33.      * when auto-generating JavaScript.
  34.      */
  35.     public static final String JSTYPE_STRING = "string";

  36.     /**
  37.      * Regular Expression Constant for JavaScript type.  This can be used
  38.      * when auto-generating JavaScript.
  39.      */
  40.     public static final String JSTYPE_REGEXP = "regexp";

  41.     /**
  42.      * The name of the variable.
  43.      */
  44.     private String name;

  45.     /**
  46.      * The key or value the variable.
  47.      */
  48.     private String value;

  49.     /**
  50.      * The optional JavaScript type of the variable.
  51.      */
  52.     private String jsType;

  53.     /**
  54.      * Whether the variable is a resource [false]
  55.      */
  56.     private boolean resource;

  57.     /**
  58.      * The bundle for a variable (when resource = 'true').
  59.      */
  60.     private String bundle;

  61.     /**
  62.      * Default Constructor.
  63.      */
  64.     public Var() {
  65.     }

  66.     /**
  67.      * Constructs a variable with a specified name, value
  68.      * and JavaScript type.
  69.      * @param name Variable name.
  70.      * @param value Variable value.
  71.      * @param jsType Variable JavaScript type.
  72.      */
  73.     public Var(final String name, final String value, final String jsType) {
  74.         this.name = name;
  75.         this.value = value;
  76.         this.jsType = jsType;
  77.     }

  78.     /**
  79.      * Creates and returns a copy of this object.
  80.      * @return A copy of the variable.
  81.      */
  82.     @Override
  83.     public Object clone() {
  84.         try {
  85.             return super.clone();

  86.         } catch (final CloneNotSupportedException e) {
  87.             throw new UnsupportedOperationException(e.toString(), e);
  88.         }
  89.     }

  90.     /**
  91.      * Returns the resource bundle name.
  92.      * @return The bundle name.
  93.      * @since 1.2.0
  94.      */
  95.     public String getBundle() {
  96.         return this.bundle;
  97.     }

  98.     /**
  99.      * Gets the JavaScript type of the variable.
  100.      * @return The JavaScript type of the variable.
  101.      */
  102.     public String getJsType() {
  103.         return this.jsType;
  104.     }

  105.     /**
  106.      * Gets the name of the variable.
  107.      * @return The name of the variable.
  108.      */
  109.     public String getName() {
  110.         return this.name;
  111.     }

  112.     /**
  113.      * Gets the value of the variable.
  114.      * @return The value of the variable.
  115.      */
  116.     public String getValue() {
  117.         return this.value;
  118.     }

  119.     /**
  120.      * Tests whether or not the value is a resource key or literal value.
  121.      * @return {@code true} if value is a resource key.
  122.      * @since 1.2.0
  123.      */
  124.     public boolean isResource() {
  125.         return this.resource;
  126.     }

  127.     /**
  128.      * Sets the resource bundle name.
  129.      * @param bundle The new bundle name.
  130.      * @since 1.2.0
  131.      */
  132.     public void setBundle(final String bundle) {
  133.         this.bundle = bundle;
  134.     }

  135.     /**
  136.      * Sets the JavaScript type of the variable.
  137.      * @param jsType The JavaScript type of the variable.
  138.      */
  139.     public void setJsType(final String jsType) {
  140.         this.jsType = jsType;
  141.     }

  142.     /**
  143.      * Sets the name of the variable.
  144.      * @param name The name of the variable.
  145.      */
  146.     public void setName(final String name) {
  147.         this.name = name;
  148.     }

  149.     /**
  150.      * Sets whether or not the value is a resource.
  151.      * @param resource If true indicates the value is a resource.
  152.      * @since 1.2.0
  153.      */
  154.     public void setResource(final boolean resource) {
  155.         this.resource = resource;
  156.     }

  157.     /**
  158.      * Sets the value of the variable.
  159.      * @param value The value of the variable.
  160.      */
  161.     public void setValue(final String value) {
  162.         this.value = value;
  163.     }

  164.     /**
  165.      * Returns a string representation of the object.
  166.      * @return A string representation of the variable.
  167.      */
  168.     @Override
  169.     public String toString() {
  170.         final StringBuilder results = new StringBuilder();

  171.         results.append("Var: name=");
  172.         results.append(name);
  173.         results.append("  value=");
  174.         results.append(value);
  175.         results.append("  resource=");
  176.         results.append(resource);
  177.         if (resource) {
  178.             results.append("  bundle=");
  179.             results.append(bundle);
  180.         }
  181.         results.append("  jsType=");
  182.         results.append(jsType);
  183.         results.append("\n");

  184.         return results.toString();
  185.     }

  186. }