001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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 /** Creates a new instance of Message */ 096 public Message(String key) { 097 this.key = key; 098 } 099 100 /** 101 * Construct an action message with the specified replacement values. 102 * 103 * @param key Message key for this message 104 * @param values One or more replacement values 105 */ 106 /** Creates a new instance of Message */ 107 public Message(String key, Object... values) { 108 this.key = key; 109 this.values = values; 110 } 111 112 /** 113 * The message key for this message. 114 */ 115 protected String key; 116 117 /** 118 * The replacement values for this mesasge. 119 */ 120 protected Object values[]; 121 122 /** 123 * The message for this message, with all value substitutions completed. 124 */ 125 protected String message; 126 127 /** 128 * Get the message key for this message. 129 */ 130 public String getKey() { 131 return this.key; 132 } 133 134 /** 135 * Get the replacement values for this message. 136 */ 137 public Object[] getValues() { 138 return this.values; 139 } 140 141 /** 142 * Get the message for this message. 143 */ 144 public String getMessage() { 145 return message == null ? new StringBuilder().append("??").append(key).append("??").toString() : message; 146 } 147 148 /** 149 * Set the message for this message. 150 */ 151 public void setMessage(String message) { 152 this.message = message; 153 } 154 155 private StringBuilder valuesToStringBuilder() { 156 StringBuilder sb = new StringBuilder(); 157 158 if(values != null) { 159 for(int i = 0 ; i < values.length ; i++) { 160 if(i != 0) 161 sb.append(", "); 162 163 sb.append('"').append(values[i]).append('"'); 164 } 165 } else { 166 sb.append((String)null); 167 } 168 169 return sb; 170 } 171 172 /** 173 * Converts to a string representing the data contained within this Message. 174 */ 175 @Override 176 public String toString() { 177 return new StringBuilder().append("key = \"").append(key).append("\", values[] = ").append(valuesToStringBuilder()).append(", message = \"").append(message).append("\"").toString(); 178 } 179 180}