View Javadoc
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  
19  import java.io.Serializable;
20  
21  /**
22   * An alternative message can be associated with a <code>Field</code>
23   * and a pluggable validator instead of using the default message
24   * stored in the <code>ValidatorAction</code> (aka pluggable validator).
25   * Instances of this class are configured with a &lt;msg&gt; xml element.
26   */
27  //TODO mutable non-private fields
28  public class Msg implements Cloneable, Serializable {
29  
30      private static final long serialVersionUID = 5690015734364127124L;
31  
32      /**
33       * The resource bundle name that this Msg's <code>key</code> should be
34       * resolved in (optional).
35       * @since 1.1
36       */
37      protected String bundle;
38  
39      /**
40       * The key or value of the argument.
41       */
42      protected String key;
43  
44      /**
45       * The name dependency that this argument goes with (optional).
46       */
47      protected String name;
48  
49      /**
50       * Whether or not the key is a message resource (optional).  Defaults to
51       * true.  If it is 'true', the value will try to be resolved as a message
52       * resource.
53       * @since 1.1.4
54       */
55      protected boolean resource = true;
56  
57      /**
58       * Creates and returns a copy of this object.
59       * @return A copy of the Msg.
60       */
61      @Override
62      public Object clone() {
63          try {
64              return super.clone();
65  
66          } catch (final CloneNotSupportedException e) {
67              throw new UnsupportedOperationException(e.toString(), e);
68          }
69      }
70  
71      /**
72       * Returns the resource bundle name.
73       * @return The bundle name.
74       * @since 1.1
75       */
76      public String getBundle() {
77          return this.bundle;
78      }
79  
80      /**
81       * Gets the key/value.
82       * @return The message key/value.
83       */
84      public String getKey() {
85          return key;
86      }
87  
88      /**
89       * Gets the name of the dependency.
90       * @return The dependency name.
91       */
92      public String getName() {
93          return name;
94      }
95  
96      /**
97       * Tests whether or not the key is a resource key or literal value.
98       * @return {@code true} if key is a resource key.
99       * @since 1.1.4
100      */
101     public boolean isResource() {
102         return this.resource;
103     }
104 
105     /**
106      * Sets the resource bundle name.
107      * @param bundle The new bundle name.
108      * @since 1.1
109      */
110     public void setBundle(final String bundle) {
111         this.bundle = bundle;
112     }
113 
114     /**
115      * Sets the key/value.
116      * @param key The message key/value.
117      */
118     public void setKey(final String key) {
119         this.key = key;
120     }
121 
122     /**
123      * Sets the name of the dependency.
124      * @param name The dependency name.
125      */
126     public void setName(final String name) {
127         this.name = name;
128     }
129 
130     /**
131      * Sets whether or not the key is a resource.
132      * @param resource If true indicates the key is a resource.
133      * @since 1.1.4
134      */
135     public void setResource(final boolean resource) {
136         this.resource = resource;
137     }
138 
139     /**
140      * Returns a string representation of the object.
141      * @return Msg String representation.
142      */
143     @Override
144     public String toString() {
145         final StringBuilder results = new StringBuilder();
146 
147         results.append("Msg: name=");
148         results.append(name);
149         results.append("  key=");
150         results.append(key);
151         results.append("  resource=");
152         results.append(resource);
153         results.append("  bundle=");
154         results.append(bundle);
155         results.append("\n");
156 
157         return results.toString();
158     }
159 
160 }