Msg.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.  * An alternative message can be associated with a <code>Field</code>
  21.  * and a pluggable validator instead of using the default message
  22.  * stored in the <code>ValidatorAction</code> (aka pluggable validator).
  23.  * Instances of this class are configured with a &lt;msg&gt; xml element.
  24.  */
  25. //TODO mutable non-private fields
  26. public class Msg implements Cloneable, Serializable {

  27.     private static final long serialVersionUID = 5690015734364127124L;

  28.     /**
  29.      * The resource bundle name that this Msg's <code>key</code> should be
  30.      * resolved in (optional).
  31.      * @since 1.1
  32.      */
  33.     protected String bundle;

  34.     /**
  35.      * The key or value of the argument.
  36.      */
  37.     protected String key;

  38.     /**
  39.      * The name dependency that this argument goes with (optional).
  40.      */
  41.     protected String name;

  42.     /**
  43.      * Whether or not the key is a message resource (optional).  Defaults to
  44.      * true.  If it is 'true', the value will try to be resolved as a message
  45.      * resource.
  46.      * @since 1.1.4
  47.      */
  48.     protected boolean resource = true;

  49.     /**
  50.      * Creates and returns a copy of this object.
  51.      * @return A copy of the Msg.
  52.      */
  53.     @Override
  54.     public Object clone() {
  55.         try {
  56.             return super.clone();

  57.         } catch (final CloneNotSupportedException e) {
  58.             throw new UnsupportedOperationException(e.toString(), e);
  59.         }
  60.     }

  61.     /**
  62.      * Returns the resource bundle name.
  63.      * @return The bundle name.
  64.      * @since 1.1
  65.      */
  66.     public String getBundle() {
  67.         return this.bundle;
  68.     }

  69.     /**
  70.      * Gets the key/value.
  71.      * @return The message key/value.
  72.      */
  73.     public String getKey() {
  74.         return key;
  75.     }

  76.     /**
  77.      * Gets the name of the dependency.
  78.      * @return The dependency name.
  79.      */
  80.     public String getName() {
  81.         return name;
  82.     }

  83.     /**
  84.      * Tests whether or not the key is a resource key or literal value.
  85.      * @return {@code true} if key is a resource key.
  86.      * @since 1.1.4
  87.      */
  88.     public boolean isResource() {
  89.         return this.resource;
  90.     }

  91.     /**
  92.      * Sets the resource bundle name.
  93.      * @param bundle The new bundle name.
  94.      * @since 1.1
  95.      */
  96.     public void setBundle(final String bundle) {
  97.         this.bundle = bundle;
  98.     }

  99.     /**
  100.      * Sets the key/value.
  101.      * @param key The message key/value.
  102.      */
  103.     public void setKey(final String key) {
  104.         this.key = key;
  105.     }

  106.     /**
  107.      * Sets the name of the dependency.
  108.      * @param name The dependency name.
  109.      */
  110.     public void setName(final String name) {
  111.         this.name = name;
  112.     }

  113.     /**
  114.      * Sets whether or not the key is a resource.
  115.      * @param resource If true indicates the key is a resource.
  116.      * @since 1.1.4
  117.      */
  118.     public void setResource(final boolean resource) {
  119.         this.resource = resource;
  120.     }

  121.     /**
  122.      * Returns a string representation of the object.
  123.      * @return Msg String representation.
  124.      */
  125.     @Override
  126.     public String toString() {
  127.         final StringBuilder results = new StringBuilder();

  128.         results.append("Msg: name=");
  129.         results.append(name);
  130.         results.append("  key=");
  131.         results.append(key);
  132.         results.append("  resource=");
  133.         results.append(resource);
  134.         results.append("  bundle=");
  135.         results.append(bundle);
  136.         results.append("\n");

  137.         return results.toString();
  138.     }

  139. }