001: /*
002: * $Id: ActionMessage.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.action;
022:
023: import java.io.Serializable;
024:
025: /**
026: * <p>An encapsulation of an individual message returned by the
027: * <code>validate</code> method of an <code>ActionForm</code>, consisting of a
028: * message key (to be used to look up message text in an appropriate message
029: * resources database) plus up to four placeholder objects that can be used
030: * for parametric replacement in the message text.</p>
031: *
032: * @version $Rev: 471754 $ $Date: 2005-05-14 01:09:32 -0400 (Sat, 14 May 2005)
033: * $
034: * @since Struts 1.1
035: */
036: public class ActionMessage implements Serializable {
037: // ----------------------------------------------------- Instance Variables
038:
039: /**
040: * <p>The message key for this message.</p>
041: */
042: protected String key = null;
043:
044: /**
045: * <p>The replacement values for this mesasge.</p>
046: */
047: protected Object[] values = null;
048:
049: /**
050: * <p>Indicates whether the key is taken to be as a bundle key [true] or
051: * literal value [false].</p>
052: */
053: protected boolean resource = true;
054:
055: // ----------------------------------------------------------- Constructors
056:
057: /**
058: * <p>Construct an action message with no replacement values.</p>
059: *
060: * @param key Message key for this message
061: */
062: public ActionMessage(String key) {
063: this (key, null);
064: }
065:
066: /**
067: * <p>Construct an action message with the specified replacement
068: * values.</p>
069: *
070: * @param key Message key for this message
071: * @param value0 First replacement value
072: */
073: public ActionMessage(String key, Object value0) {
074: this (key, new Object[] { value0 });
075: }
076:
077: /**
078: * <p>Construct an action message with the specified replacement
079: * values.</p>
080: *
081: * @param key Message key for this message
082: * @param value0 First replacement value
083: * @param value1 Second replacement value
084: */
085: public ActionMessage(String key, Object value0, Object value1) {
086: this (key, new Object[] { value0, value1 });
087: }
088:
089: /**
090: * <p>Construct an action message with the specified replacement
091: * values.</p>
092: *
093: * @param key Message key for this message
094: * @param value0 First replacement value
095: * @param value1 Second replacement value
096: * @param value2 Third replacement value
097: */
098: public ActionMessage(String key, Object value0, Object value1,
099: Object value2) {
100: this (key, new Object[] { value0, value1, value2 });
101: }
102:
103: /**
104: * <p>Construct an action message with the specified replacement
105: * values.</p>
106: *
107: * @param key Message key for this message
108: * @param value0 First replacement value
109: * @param value1 Second replacement value
110: * @param value2 Third replacement value
111: * @param value3 Fourth replacement value
112: */
113: public ActionMessage(String key, Object value0, Object value1,
114: Object value2, Object value3) {
115: this (key, new Object[] { value0, value1, value2, value3 });
116: }
117:
118: /**
119: * <p>Construct an action message with the specified replacement
120: * values.</p>
121: *
122: * @param key Message key for this message
123: * @param values Array of replacement values
124: */
125: public ActionMessage(String key, Object[] values) {
126: this .key = key;
127: this .values = values;
128: this .resource = true;
129: }
130:
131: /**
132: * <p>Construct an action message with the specified replacement
133: * values.</p>
134: *
135: * @param key Message key for this message
136: * @param resource Indicates whether the key is a bundle key or literal
137: * value
138: */
139: public ActionMessage(String key, boolean resource) {
140: this .key = key;
141: this .resource = resource;
142: }
143:
144: // --------------------------------------------------------- Public Methods
145:
146: /**
147: * <p>Get the message key for this message.</p>
148: *
149: * @return The message key for this message.
150: */
151: public String getKey() {
152: return (this .key);
153: }
154:
155: /**
156: * <p>Get the replacement values for this message.</p>
157: *
158: * @return The replacement values for this message.
159: */
160: public Object[] getValues() {
161: return (this .values);
162: }
163:
164: /**
165: * <p>Indicate whether the key is taken to be as a bundle key [true] or
166: * literal value [false].</p>
167: *
168: * @return <code>true</code> if the key is a bundle key;
169: * <code>false</code> otherwise.
170: */
171: public boolean isResource() {
172: return (this .resource);
173: }
174:
175: /**
176: * <p>Returns a String in the format: key[value1, value2, etc].</p>
177: *
178: * @return String representation of this message
179: * @see java.lang.Object#toString()
180: */
181: public String toString() {
182: StringBuffer buff = new StringBuffer();
183:
184: buff.append(this .key);
185: buff.append("[");
186:
187: if (this .values != null) {
188: for (int i = 0; i < this .values.length; i++) {
189: buff.append(this .values[i]);
190:
191: // don't append comma to last entry
192: if (i < (this .values.length - 1)) {
193: buff.append(", ");
194: }
195: }
196: }
197:
198: buff.append("]");
199:
200: return buff.toString();
201: }
202: }
|