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.  *      https://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}
  21.  * and a pluggable validator instead of using the default message
  22.  * stored in the {@code ValidatorAction} (aka pluggable validator).
  23.  * Instances of this class are configured with a <msg> 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} 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.      * Constructs a new instance.
  51.      */
  52.     public Msg() {
  53.         // empty
  54.     }

  55.     /**
  56.      * Creates and returns a copy of this object.
  57.      * @return A copy of the Msg.
  58.      */
  59.     @Override
  60.     public Object clone() {
  61.         try {
  62.             return super.clone();

  63.         } catch (final CloneNotSupportedException e) {
  64.             throw new UnsupportedOperationException(e.toString(), e);
  65.         }
  66.     }

  67.     /**
  68.      * Returns the resource bundle name.
  69.      * @return The bundle name.
  70.      * @since 1.1
  71.      */
  72.     public String getBundle() {
  73.         return bundle;
  74.     }

  75.     /**
  76.      * Gets the key/value.
  77.      * @return The message key/value.
  78.      */
  79.     public String getKey() {
  80.         return key;
  81.     }

  82.     /**
  83.      * Gets the name of the dependency.
  84.      * @return The dependency name.
  85.      */
  86.     public String getName() {
  87.         return name;
  88.     }

  89.     /**
  90.      * Tests whether or not the key is a resource key or literal value.
  91.      * @return {@code true} if key is a resource key.
  92.      * @since 1.1.4
  93.      */
  94.     public boolean isResource() {
  95.         return resource;
  96.     }

  97.     /**
  98.      * Sets the resource bundle name.
  99.      * @param bundle The new bundle name.
  100.      * @since 1.1
  101.      */
  102.     public void setBundle(final String bundle) {
  103.         this.bundle = bundle;
  104.     }

  105.     /**
  106.      * Sets the key/value.
  107.      * @param key The message key/value.
  108.      */
  109.     public void setKey(final String key) {
  110.         this.key = key;
  111.     }

  112.     /**
  113.      * Sets the name of the dependency.
  114.      * @param name The dependency name.
  115.      */
  116.     public void setName(final String name) {
  117.         this.name = name;
  118.     }

  119.     /**
  120.      * Sets whether or not the key is a resource.
  121.      * @param resource If true indicates the key is a resource.
  122.      * @since 1.1.4
  123.      */
  124.     public void setResource(final boolean resource) {
  125.         this.resource = resource;
  126.     }

  127.     /**
  128.      * Returns a string representation of the object.
  129.      * @return Msg String representation.
  130.      */
  131.     @Override
  132.     public String toString() {
  133.         final StringBuilder results = new StringBuilder();

  134.         results.append("Msg: name=");
  135.         results.append(name);
  136.         results.append("  key=");
  137.         results.append(key);
  138.         results.append("  resource=");
  139.         results.append(resource);
  140.         results.append("  bundle=");
  141.         results.append(bundle);
  142.         results.append("\n");

  143.         return results.toString();
  144.     }

  145. }