001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017/* ====================================================================
018 *
019 * The Apache Software License, Version 1.1
020 *
021 * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
022 * reserved.
023 *
024 * Redistribution and use in source and binary forms, with or without
025 * modification, are permitted provided that the following conditions
026 * are met:
027 *
028 * 1. Redistributions of source code must retain the above copyright
029 *    notice, this list of conditions and the following disclaimer.
030 *
031 * 2. Redistributions in binary form must reproduce the above copyright
032 *    notice, this list of conditions and the following disclaimer in
033 *    the documentation and/or other materials provided with the
034 *    distribution.
035 *
036 * 3. The end-user documentation included with the redistribution, if
037 *    any, must include the following acknowlegement:
038 *       "This product includes software developed by the
039 *        Apache Software Foundation (http://www.apache.org/)."
040 *    Alternately, this acknowlegement may appear in the software itself,
041 *    if and wherever such third-party acknowlegements normally appear.
042 *
043 * 4. The names "The Jakarta Project", "Struts", and "Apache Software
044 *    Foundation" must not be used to endorse or promote products derived
045 *    from this software without prior written permission. For written
046 *    permission, please contact apache@apache.org.
047 *
048 * 5. Products derived from this software may not be called "Apache"
049 *    nor may "Apache" appear in their names without prior written
050 *    permission of the Apache Group.
051 *
052 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
053 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
054 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
055 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
056 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
057 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
058 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
059 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
060 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
061 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
062 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
063 * SUCH DAMAGE.
064 * ====================================================================
065 *
066 * This software consists of voluntary contributions made by many
067 * individuals on behalf of the Apache Software Foundation.  For more
068 * information on the Apache Software Foundation, please see
069 * <http://www.apache.org/>.
070 *
071 */
072
073package com.echothree.util.common.message;
074
075import java.io.Serializable;
076
077/**
078 * <p>An encapsulation of an individual message returned by the
079 * <code>validate()</code> method of an <code>ActionForm</code>, consisting
080 * of a message key (to be used to look up message text in an appropriate
081 * message resources database) plus up to four placeholder objects that can
082 * be used for parametric replacement in the message text.</p>
083 *
084 * @since Struts 1.1
085 */
086
087public class Message
088        implements Serializable {
089    
090    /**
091     * Construct an action message with no replacement values.
092     *
093     * @param key Message key for this message
094     */
095    public Message(String key) {
096        this.key = key;
097    }
098    
099    /**
100     * Construct an action message with the specified replacement values.
101     *
102     * @param key Message key for this message
103     * @param values One or more replacement values
104     */
105    public Message(String key, Object... values) {
106        this.key = key;
107        this.values = values;
108    }
109    
110    /**
111     * The message key for this message.
112     */
113    protected String key;
114    
115    /**
116     * The replacement values for this mesasge.
117     */
118    protected Object values[];
119    
120    /**
121     * The message for this message, with all value substitutions completed.
122     */
123    protected String message;
124    
125    /**
126     * Get the message key for this message.
127     */
128    public String getKey() {
129        return this.key;
130    }
131    
132    /**
133     * Get the replacement values for this message.
134     */
135    public Object[] getValues() {
136        return this.values;
137    }
138    
139    /**
140     * Get the message for this message.
141     */
142    public String getMessage() {
143        return message == null ? "??" + key + "??" : message;
144    }
145    
146    /**
147     * Set the message for this message.
148     */
149    public void setMessage(String message) {
150        this.message = message;
151    }
152    
153    private StringBuilder valuesToStringBuilder() {
154        var sb = new StringBuilder();
155        
156        if(values != null) {
157            for(var i = 0; i < values.length ; i++) {
158                if(i != 0)
159                    sb.append(", ");
160                
161                sb.append('"').append(values[i]).append('"');
162            }
163        } else {
164            sb.append((String)null);
165        }
166        
167        return sb;
168    }
169    
170    /**
171     * Converts to a string representing the data contained within this Message.
172     */
173    @Override
174    public String toString() {
175        return "key = \"" + key + "\", values[] = " + valuesToStringBuilder() + ", message = \"" + message + "\"";
176    }
177    
178}