    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 &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} should be
    034     * resolved in (optional).
    035     *
    036     * @since 1.1
    037     */
    038    protected String bundle;
    039
    040    /**
    041     * The key or value of the argument.
    042     */
    043    protected String key;
    044
    045    /**
    046     * The name dependency that this argument goes with (optional).
    047     */
    048    protected String name;
    049
    050    /**
    051     * Whether or not the key is a message resource (optional).  Defaults to
    052     * true.  If it is 'true', the value will try to be resolved as a message
    053     * resource.
    054     *
    055     * @since 1.1.4
    056     */
    057    protected boolean resource = true;
    058
    059    /**
    060     * Constructs a new instance.
    061     */
    062    public Msg() {
    063        // empty
    064    }
    065
    066    /**
    067     * Creates and returns a copy of this object.
    068     *
    069     * @return A copy of the Msg.
    070     */
    071    @Override
    072    public Object clone() {
    073        try {
    074            return super.clone();
    075
    076        } catch (final CloneNotSupportedException e) {
    077            throw new UnsupportedOperationException(e.toString(), e);
    078        }
    079    }
    080
    081    /**
    082     * Returns the resource bundle name.
    083     *
    084     * @return The bundle name.
    085     * @since 1.1
    086     */
    087    public String getBundle() {
    088        return bundle;
    089    }
    090
    091    /**
    092     * Gets the key/value.
    093     *
    094     * @return The message key/value.
    095     */
    096    public String getKey() {
    097        return key;
    098    }
    099
    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}