001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.validator;
018
019import java.io.Serializable;
020
021/**
022 * An alternative message can be associated with a <code>Field</code>
023 * and a pluggable validator instead of using the default message
024 * stored in the <code>ValidatorAction</code> (aka pluggable validator).
025 * Instances of this class are configured with a &lt;msg&gt; xml element.
026 */
027//TODO mutable non-private fields
028public class Msg implements Cloneable, Serializable {
029
030    private static final long serialVersionUID = 5690015734364127124L;
031
032    /**
033     * The resource bundle name that this Msg's <code>key</code> should be
034     * resolved in (optional).
035     * @since 1.1
036     */
037    protected String bundle;
038
039    /**
040     * The key or value of the argument.
041     */
042    protected String key;
043
044    /**
045     * The name dependency that this argument goes with (optional).
046     */
047    protected String name;
048
049    /**
050     * Whether or not the key is a message resource (optional).  Defaults to
051     * true.  If it is 'true', the value will try to be resolved as a message
052     * resource.
053     * @since 1.1.4
054     */
055    protected boolean resource = true;
056
057    /**
058     * Creates and returns a copy of this object.
059     * @return A copy of the Msg.
060     */
061    @Override
062    public Object clone() {
063        try {
064            return super.clone();
065
066        } catch (final CloneNotSupportedException e) {
067            throw new UnsupportedOperationException(e.toString(), e);
068        }
069    }
070
071    /**
072     * Returns the resource bundle name.
073     * @return The bundle name.
074     * @since 1.1
075     */
076    public String getBundle() {
077        return this.bundle;
078    }
079
080    /**
081     * Gets the key/value.
082     * @return The message key/value.
083     */
084    public String getKey() {
085        return key;
086    }
087
088    /**
089     * Gets the name of the dependency.
090     * @return The dependency name.
091     */
092    public String getName() {
093        return name;
094    }
095
096    /**
097     * Tests whether or not the key is a resource key or literal value.
098     * @return {@code true} if key is a resource key.
099     * @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}