KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > struts > ActionMessagesTool


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.velocity.tools.struts;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.struts.util.MessageResources;
24 import org.apache.struts.action.ActionMessage;
25 import org.apache.struts.action.ActionMessages;
26
27 import org.apache.velocity.app.Velocity;
28 import org.apache.velocity.tools.struts.StrutsUtils;
29
30 /**
31  * <p>View tool to work with the Struts action messages.</p>
32  * <p><pre>
33  * Template example(s):
34  * #if( $messages.exist() )
35  * #foreach( $e in $messages.all )
36  * $e &lt;br&gt;
37  * #end
38  * #end
39  *
40  * Toolbox configuration:
41  *
42  * &lt;tool&gt;
43  * &lt;key&gt;messages&lt;/key&gt;
44  * &lt;scope&gt;request&lt;/scope&gt;
45  * &lt;class&gt;org.apache.velocity.tools.struts.ActionMessagesTool&lt;/class&gt;
46  * &lt;/tool&gt;
47  * </pre></p>
48  *
49  * <p>This tool should only be used in the request scope.</p>
50  *
51  * @author <a HREF="mailto:sidler@teamup.com">Gabe Sidler</a>
52  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
53  * @since VelocityTools 1.1
54  * @version $Id: ActionMessagesTool.java,v 1.8.2.1 2004/03/12 23:36:19 nbubna Exp $
55  */

56 public class ActionMessagesTool extends MessageResourcesTool
57 {
58
59     /** A reference to the queued action messages. */
60     protected ActionMessages actionMsgs;
61
62
63     /**
64      * Default constructor. Tool must be initialized before use.
65      */

66     public ActionMessagesTool()
67     {}
68     
69     
70     /**
71      * Initializes this tool.
72      *
73      * @param obj the current ViewContext
74      * @throws IllegalArgumentException if the param is not a ViewContext
75      */

76     public void init(Object JavaDoc obj)
77     {
78         //setup superclass instance members
79
super.init(obj);
80
81         this.actionMsgs = StrutsUtils.getMessages(this.request);
82     }
83
84
85     /*************************** Public Methods ***********************/
86
87     /**
88      * <p>Returns <code>true</code> if there are action messages queued,
89      * otherwise <code>false</code>.</p>
90      */

91     public boolean exist()
92     {
93         if (actionMsgs == null)
94         {
95             return false;
96         }
97         return !actionMsgs.isEmpty();
98     }
99
100
101     /**
102      * <p>Returns true if there are action messages queued for the specified
103      * category of messages, otherwise <code>false</code>.</p>
104      *
105      * @param property the category of messages to check for
106      */

107     public boolean exist(String JavaDoc property)
108     {
109         if (actionMsgs == null)
110         {
111             return false;
112         }
113         return (actionMsgs.size(property) > 0);
114     }
115
116
117     /**
118      * Returns the number of action messages queued.
119      */

120     public int getSize()
121     {
122         if (actionMsgs == null)
123         {
124             return 0;
125         }
126         return actionMsgs.size();
127     }
128
129
130     /**
131      * Returns the number of action messages queued for a particular property.
132      *
133      * @param property the category of messages to check for
134      */

135     public int getSize(String JavaDoc property)
136     {
137         if (actionMsgs == null)
138         {
139             return 0;
140         }
141         return actionMsgs.size(property);
142     }
143
144
145     /**
146      * <p>
147      * This a convenience method and the equivalent of
148      * <code>$messages.get($messages.globalName)</code>.
149      * </p>
150      * <p>
151      * Returns the set of localized action messages as an
152      * list of strings for all action messages queued of the
153      * global category or <code>null</code> if no messages
154      * are queued for the specified category. If the message
155      * resources don't contain an action message for a
156      * particular message key, the key itself is used.
157      * </p>
158      *
159      * @return a list of all messages stored under the "global" property
160      */

161     public List JavaDoc getGlobal()
162     {
163         return get(getGlobalName());
164     }
165
166
167     /**
168      * Returns the set of localized action messages as an
169      * <code>java.util.List</code> of strings for all actionMsgs
170      * queued or <code>null</code> if no messages are queued.
171      * If the message resources don't contain a message for a
172      * particular key, the key itself is used as the message.
173      */

174     public List JavaDoc getAll()
175     {
176         return get(null);
177     }
178
179
180     /**
181      * Returns a List of all queued action messages using
182      * the specified message resource bundle.
183      *
184      * @param bundle the message resource bundle to use
185      * @see #getAll()
186      */

187     public List JavaDoc getAll(String JavaDoc bundle)
188     {
189         return get(null, bundle);
190     }
191
192
193     /**
194      * Returns the set of localized action messages as an
195      * <code>java.util.List</code> of strings for all actionMsgs
196      * queued of the specified category or <code>null</code>
197      * if no messages are queued for the specified category. If the
198      * message resources don't contain a message for a particular
199      * key, the key itself is used as the message.
200      *
201      * @param property the category of actionMsgs to operate on
202      */

203     public List JavaDoc get(String JavaDoc property)
204     {
205         return get(property, null);
206     }
207
208
209     /**
210      * Returns the set of localized action messages as a
211      * <code>java.util.List</code> of strings for all action messages
212      * queued of the specified category or <code>null</code>
213      * if no action messages are queued for the specified category. If the
214      * message resources don't contain an action message for a particular
215      * action key, the key itself is used as action message.
216      *
217      * @param property the category of actionMsgs to operate on
218      * @param bundle the message resource bundle to use
219      */

220     public List JavaDoc get(String JavaDoc property, String JavaDoc bundle)
221     {
222         if (actionMsgs == null || actionMsgs.isEmpty())
223         {
224             return null;
225         }
226         
227         Iterator JavaDoc msgs;
228         if (property == null)
229         {
230             msgs = actionMsgs.get();
231         }
232         else
233         {
234             msgs = actionMsgs.get(property);
235         }
236         
237         if (!msgs.hasNext())
238         {
239             return null;
240         }
241
242         MessageResources res = getResources(bundle);
243         List JavaDoc list = new ArrayList JavaDoc();
244          
245         while (msgs.hasNext())
246         {
247             ActionMessage msg = (ActionMessage)msgs.next();
248
249             String JavaDoc message = null;
250             if (res != null)
251             {
252                 message =
253                     res.getMessage(this.locale, msg.getKey(), msg.getValues());
254
255                 if (message == null)
256                 {
257                     Velocity.warn("ActionMessagesTool: Message for key " +
258                                   msg.getKey() +
259                                   " could not be found in message resources.");
260                 }
261             }
262             else
263             {
264                 // if the resource bundle wasn't found, use the key
265
message = msg.getKey();
266             }
267             list.add(message);
268         }
269         return list;
270     }
271
272
273     /**
274      * Returns the default "GLOBAL" category name that can be used for
275      * messages that are not associated with a particular property.
276      */

277     public String JavaDoc getGlobalName()
278     {
279         return ActionMessages.GLOBAL_MESSAGE;
280     }
281
282 }
283
Popular Tags