View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts.action;
22  
23  import java.io.Serializable;
24  
25  /**
26   * <p>An encapsulation of an individual message returned by the
27   * <code>validate</code> method of an <code>ActionForm</code>, consisting of a
28   * message key (to be used to look up message text in an appropriate message
29   * resources database) plus up to four placeholder objects that can be used
30   * for parametric replacement in the message text.</p>
31   *
32   * @version $Rev$ $Date: 2005-05-14 01:09:32 -0400 (Sat, 14 May 2005)
33   *          $
34   * @since Struts 1.1
35   */
36  public class ActionMessage implements Serializable {
37      private static final long serialVersionUID = 5038146636915080725L;
38  
39      // ----------------------------------------------------- Instance Variables
40  
41      /**
42       * <p>The message key for this message.</p>
43       */
44      protected String key = null;
45  
46      /**
47       * The replacement values for this message.
48       */
49      protected Object[] values = null;
50  
51      /**
52       * <p>Indicates whether the key is taken to be as a  bundle key [true] or
53       * literal value [false].</p>
54       */
55      protected boolean resource = true;
56  
57      // ----------------------------------------------------------- Constructors
58  
59      /**
60       * <p>Construct an action message with no replacement values.</p>
61       *
62       * @param key Message key for this message
63       */
64      public ActionMessage(String key) {
65          this(key, null);
66      }
67  
68      /**
69       * <p>Construct an action message with the specified replacement
70       * values.</p>
71       *
72       * @param key    Message key for this message
73       * @param value0 First replacement value
74       */
75      public ActionMessage(String key, Object value0) {
76          this(key, new Object[] { value0 });
77      }
78  
79      /**
80       * <p>Construct an action message with the specified replacement
81       * values.</p>
82       *
83       * @param key    Message key for this message
84       * @param value0 First replacement value
85       * @param value1 Second replacement value
86       */
87      public ActionMessage(String key, Object value0, Object value1) {
88          this(key, new Object[] { value0, value1 });
89      }
90  
91      /**
92       * <p>Construct an action message with the specified replacement
93       * values.</p>
94       *
95       * @param key    Message key for this message
96       * @param value0 First replacement value
97       * @param value1 Second replacement value
98       * @param value2 Third replacement value
99       */
100     public ActionMessage(String key, Object value0, Object value1, Object value2) {
101         this(key, new Object[] { value0, value1, value2 });
102     }
103 
104     /**
105      * <p>Construct an action message with the specified replacement
106      * values.</p>
107      *
108      * @param key    Message key for this message
109      * @param value0 First replacement value
110      * @param value1 Second replacement value
111      * @param value2 Third replacement value
112      * @param value3 Fourth replacement value
113      */
114     public ActionMessage(String key, Object value0, Object value1,
115         Object value2, Object value3) {
116         this(key, new Object[] { value0, value1, value2, value3 });
117     }
118 
119     /**
120      * <p>Construct an action message with the specified replacement
121      * values.</p>
122      *
123      * @param key    Message key for this message
124      * @param values Array of replacement values
125      */
126     public ActionMessage(String key, Object[] values) {
127         this.key = key;
128         this.values = values;
129         this.resource = true;
130     }
131 
132     /**
133      * <p>Construct an action message with the specified replacement
134      * values.</p>
135      *
136      * @param key      Message key for this message
137      * @param resource Indicates whether the key is a bundle key or literal
138      *                 value
139      */
140     public ActionMessage(String key, boolean resource) {
141         this.key = key;
142         this.resource = resource;
143     }
144 
145     // --------------------------------------------------------- Public Methods
146 
147     /**
148      * <p>Get the message key for this message.</p>
149      *
150      * @return The message key for this message.
151      */
152     public String getKey() {
153         return (this.key);
154     }
155 
156     /**
157      * <p>Get the replacement values for this message.</p>
158      *
159      * @return The replacement values for this message.
160      */
161     public Object[] getValues() {
162         return (this.values);
163     }
164 
165     /**
166      * <p>Indicate whether the key is taken to be as a  bundle key [true] or
167      * literal value [false].</p>
168      *
169      * @return <code>true</code> if the key is a bundle key;
170      *         <code>false</code> otherwise.
171      */
172     public boolean isResource() {
173         return (this.resource);
174     }
175 
176     /**
177      * <p>Returns a String in the format: key[value1, value2, etc].</p>
178      *
179      * @return String representation of this message
180      * @see java.lang.Object#toString()
181      */
182     public String toString() {
183         StringBuilder buff = new StringBuilder();
184 
185         buff.append(this.key);
186         buff.append("[");
187 
188         if (this.values != null) {
189             for (int i = 0; i < this.values.length; i++) {
190                 buff.append(this.values[i]);
191 
192                 // don't append comma to last entry
193                 if (i < (this.values.length - 1)) {
194                     buff.append(", ");
195                 }
196             }
197         }
198 
199         buff.append("]");
200 
201         return buff.toString();
202     }
203 }