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 * @version $Revision: 1713331 $
028 */
029//TODO mutable non-private fields
030public class Msg implements Cloneable, Serializable {
031
032    private static final long serialVersionUID = 5690015734364127124L;
033
034    /**
035     * The resource bundle name that this Msg's <code>key</code> should be
036     * resolved in (optional).
037     * @since Validator 1.1
038     */
039    protected String bundle = null;
040
041    /**
042     * The key or value of the argument.
043     */
044    protected String key = null;
045
046    /**
047     * The name dependency that this argument goes with (optional).
048     */
049    protected String name = null;
050
051    /**
052     * Whether or not the key is a message resource (optional).  Defaults to
053     * true.  If it is 'true', the value will try to be resolved as a message
054     * resource.
055     * @since Validator 1.1.4
056     */
057    protected boolean resource = true;
058
059    /**
060     * Returns the resource bundle name.
061     * @return The bundle name.
062     * @since Validator 1.1
063     */
064    public String getBundle() {
065        return this.bundle;
066    }
067
068    /**
069     * Sets the resource bundle name.
070     * @param bundle The new bundle name.
071     * @since Validator 1.1
072     */
073    public void setBundle(String bundle) {
074        this.bundle = bundle;
075    }
076
077    /**
078     * Gets the name of the dependency.
079     * @return The dependency name.
080     */
081    public String getName() {
082        return name;
083    }
084
085    /**
086     * Sets the name of the dependency.
087     * @param name The dependency name.
088     */
089    public void setName(String name) {
090        this.name = name;
091    }
092
093    /**
094     * Gets the key/value.
095     * @return The message key/value.
096     */
097    public String getKey() {
098        return key;
099    }
100
101    /**
102     * Sets the key/value.
103     * @param key The message key/value.
104     */
105    public void setKey(String key) {
106        this.key = key;
107    }
108
109    /**
110     * Tests whether or not the key is a resource key or literal value.
111     * @return <code>true</code> if key is a resource key.
112     * @since Validator 1.1.4
113     */
114    public boolean isResource() {
115        return this.resource;
116    }
117
118    /**
119     * Sets whether or not the key is a resource.
120     * @param resource If true indicates the key is a resource.
121     * @since Validator 1.1.4
122     */
123    public void setResource(boolean resource) {
124        this.resource = resource;
125    }
126
127    /**
128     * Creates and returns a copy of this object.
129     * @return A copy of the Msg.
130     */
131    public Object clone() {
132        try {
133            return super.clone();
134
135        } catch(CloneNotSupportedException e) {
136            throw new RuntimeException(e.toString());
137        }
138    }
139
140    /**
141     * Returns a string representation of the object.
142     * @return Msg String representation.
143     */
144    public String toString() {
145        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}