View Javadoc

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