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    *      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  
19  import java.io.Serializable;
20  
21  /**
22   * An alternative message can be associated with a {@code Field}
23   * and a pluggable validator instead of using the default message
24   * stored in the {@code ValidatorAction} (aka pluggable validator).
25   * Instances of this class are configured with a <msg> 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} 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       * Constructs a new instance.
59       */
60      public Msg() {
61          // empty
62      }
63  
64      /**
65       * Creates and returns a copy of this object.
66       * @return A copy of the Msg.
67       */
68      @Override
69      public Object clone() {
70          try {
71              return super.clone();
72  
73          } catch (final CloneNotSupportedException e) {
74              throw new UnsupportedOperationException(e.toString(), e);
75          }
76      }
77  
78      /**
79       * Returns the resource bundle name.
80       * @return The bundle name.
81       * @since 1.1
82       */
83      public String getBundle() {
84          return bundle;
85      }
86  
87      /**
88       * Gets the key/value.
89       * @return The message key/value.
90       */
91      public String getKey() {
92          return key;
93      }
94  
95      /**
96       * Gets the name of the dependency.
97       * @return The dependency name.
98       */
99      public String getName() {
100         return name;
101     }
102 
103     /**
104      * Tests whether or not the key is a resource key or literal value.
105      * @return {@code true} if key is a resource key.
106      * @since 1.1.4
107      */
108     public boolean isResource() {
109         return resource;
110     }
111 
112     /**
113      * Sets the resource bundle name.
114      * @param bundle The new bundle name.
115      * @since 1.1
116      */
117     public void setBundle(final String bundle) {
118         this.bundle = bundle;
119     }
120 
121     /**
122      * Sets the key/value.
123      * @param key The message key/value.
124      */
125     public void setKey(final String key) {
126         this.key = key;
127     }
128 
129     /**
130      * Sets the name of the dependency.
131      * @param name The dependency name.
132      */
133     public void setName(final String name) {
134         this.name = name;
135     }
136 
137     /**
138      * Sets whether or not the key is a resource.
139      * @param resource If true indicates the key is a resource.
140      * @since 1.1.4
141      */
142     public void setResource(final boolean resource) {
143         this.resource = resource;
144     }
145 
146     /**
147      * Returns a string representation of the object.
148      * @return Msg String representation.
149      */
150     @Override
151     public String toString() {
152         final StringBuilder results = new StringBuilder();
153 
154         results.append("Msg: name=");
155         results.append(name);
156         results.append("  key=");
157         results.append(key);
158         results.append("  resource=");
159         results.append(resource);
160         results.append("  bundle=");
161         results.append(bundle);
162         results.append("\n");
163 
164         return results.toString();
165     }
166 
167 }