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 *      https://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}
023 * and a pluggable validator instead of using the default message
024 * stored in the {@code ValidatorAction} (aka pluggable validator).
025 * Instances of this class are configured with a <msg> 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} 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     * Constructs a new instance.
059     */
060    public Msg() {
061        // empty
062    }
063
064    /**
065     * Creates and returns a copy of this object.
066     * @return A copy of the Msg.
067     */
068    @Override
069    public Object clone() {
070        try {
071            return super.clone();
072
073        } catch (final CloneNotSupportedException e) {
074            throw new UnsupportedOperationException(e.toString(), e);
075        }
076    }
077
078    /**
079     * Returns the resource bundle name.
080     * @return The bundle name.
081     * @since 1.1
082     */
083    public String getBundle() {
084        return bundle;
085    }
086
087    /**
088     * Gets the key/value.
089     * @return The message key/value.
090     */
091    public String getKey() {
092        return key;
093    }
094
095    /**
096     * Gets the name of the dependency.
097     * @return The dependency name.
098     */
099    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}