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 *
36 * @since 1.1
37 */
38 protected String bundle;
39
40 /**
41 * The key or value of the argument.
42 */
43 protected String key;
44
45 /**
46 * The name dependency that this argument goes with (optional).
47 */
48 protected String name;
49
50 /**
51 * Whether or not the key is a message resource (optional). Defaults to
52 * true. If it is 'true', the value will try to be resolved as a message
53 * resource.
54 *
55 * @since 1.1.4
56 */
57 protected boolean resource = true;
58
59 /**
60 * Constructs a new instance.
61 */
62 public Msg() {
63 // empty
64 }
65
66 /**
67 * Creates and returns a copy of this object.
68 *
69 * @return A copy of the Msg.
70 */
71 @Override
72 public Object clone() {
73 try {
74 return super.clone();
75
76 } catch (final CloneNotSupportedException e) {
77 throw new UnsupportedOperationException(e.toString(), e);
78 }
79 }
80
81 /**
82 * Returns the resource bundle name.
83 *
84 * @return The bundle name.
85 * @since 1.1
86 */
87 public String getBundle() {
88 return bundle;
89 }
90
91 /**
92 * Gets the key/value.
93 *
94 * @return The message key/value.
95 */
96 public String getKey() {
97 return key;
98 }
99
100 /**
101 * Gets the name of the dependency.
102 *
103 * @return The dependency name.
104 */
105 public String getName() {
106 return name;
107 }
108
109 /**
110 * Tests whether or not the key is a resource key or literal value.
111 *
112 * @return {@code true} if key is a resource key.
113 * @since 1.1.4
114 */
115 public boolean isResource() {
116 return resource;
117 }
118
119 /**
120 * Sets the resource bundle name.
121 *
122 * @param bundle The new bundle name.
123 * @since 1.1
124 */
125 public void setBundle(final String bundle) {
126 this.bundle = bundle;
127 }
128
129 /**
130 * Sets the key/value.
131 *
132 * @param key The message key/value.
133 */
134 public void setKey(final String key) {
135 this.key = key;
136 }
137
138 /**
139 * Sets the name of the dependency.
140 *
141 * @param name The dependency name.
142 */
143 public void setName(final String name) {
144 this.name = name;
145 }
146
147 /**
148 * Sets whether or not the key is a resource.
149 *
150 * @param resource If true indicates the key is a resource.
151 * @since 1.1.4
152 */
153 public void setResource(final boolean resource) {
154 this.resource = resource;
155 }
156
157 /**
158 * Returns a string representation of the object.
159 *
160 * @return Msg String representation.
161 */
162 @Override
163 public String toString() {
164 final StringBuilder results = new StringBuilder();
165
166 results.append("Msg: name=");
167 results.append(name);
168 results.append(" key=");
169 results.append(key);
170 results.append(" resource=");
171 results.append(resource);
172 results.append(" bundle=");
173 results.append(bundle);
174 results.append("\n");
175
176 return results.toString();
177 }
178
179 }