KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
20 import java.util.Locale JavaDoc;
21 import org.apache.struts.util.MessageResources;
22
23 /**
24  * <p>View tool that provides methods to render Struts
25  * application resources for internationalized text.</p>
26  *
27  * <p><pre>
28  * Template example(s):
29  * #if( $text.exists('greeting') )
30  * $text.greeting
31  * #end
32  *
33  * Toolbox configuration:
34  * &lt;tool&gt;
35  * &lt;key&gt;text&lt;/key&gt;
36  * &lt;scope&gt;request&lt;/scope&gt;
37  * &lt;class&gt;org.apache.velocity.tools.struts.MessageTool&lt;/class&gt;
38  * &lt;/tool&gt;
39  * </pre></p>
40  *
41  * <p>This tool should only be used in the request scope.</p>
42  *
43  * @author <a HREF="mailto:sidler@teamup.com">Gabe Sidler</a>
44  * @since VelocityTools 1.0
45  * @version $Id: MessageTool.java,v 1.12 2004/02/18 20:09:51 nbubna Exp $
46  */

47 public class MessageTool extends MessageResourcesTool
48 {
49
50     /**
51      * Default constructor. Tool must be initialized before use.
52      */

53     public MessageTool()
54     {}
55
56
57     /**
58      * Looks up and returns the localized message for the specified key.
59      * The user's locale is consulted to determine the language of the
60      * message.
61      *
62      * @param key message key
63      *
64      * @return the localized message for the specified key or
65      * <code>null</code> if no such message exists
66      */

67     public String JavaDoc get(String JavaDoc key)
68     {
69         return get(key, (Object JavaDoc[])null);
70     }
71
72     /**
73      * Looks up and returns the localized message for the specified key.
74      * The user's locale is consulted to determine the language of the
75      * message.
76      *
77      * @param key message key
78      * @param bundle The bundle name to look for.
79      *
80      * @return the localized message for the specified key or
81      * <code>null</code> if no such message exists
82      * @since VelocityTools 1.1
83      */

84     public String JavaDoc get(String JavaDoc key, String JavaDoc bundle)
85     {
86         return get(key, bundle, (Object JavaDoc[])null);
87     }
88
89
90     /**
91      * Looks up and returns the localized message for the specified key.
92      * Replacement parameters passed with <code>args</code> are
93      * inserted into the message. The user's locale is consulted to
94      * determine the language of the message.
95      *
96      * @param key message key
97      * @param args replacement parameters for this message
98      *
99      * @return the localized message for the specified key or
100      * <code>null</code> if no such message exists
101      */

102     public String JavaDoc get(String JavaDoc key, Object JavaDoc args[])
103     {
104        return get(key, null, args);
105     }
106
107     /**
108      * Looks up and returns the localized message for the specified key.
109      * Replacement parameters passed with <code>args</code> are
110      * inserted into the message. The user's locale is consulted to
111      * determine the language of the message.
112      *
113      * @param key message key
114      * @param bundle The bundle name to look for.
115      * @param args replacement parameters for this message
116      * @since VelocityTools 1.1
117      * @return the localized message for the specified key or
118      * <code>null</code> if no such message exists
119      */

120     public String JavaDoc get(String JavaDoc key, String JavaDoc bundle, Object JavaDoc args[])
121     {
122         MessageResources res = getResources(bundle);
123         if (res == null)
124         {
125             return null;
126         }
127
128         // return the requested message
129
if (args == null)
130         {
131             return res.getMessage(this.locale, key);
132         }
133         else
134         {
135             return res.getMessage(this.locale, key, args);
136         }
137     }
138
139     /**
140      * Same as {@link #get(String key, Object[] args)}, but takes a
141      * <code>java.util.List</code> instead of an array. This is more
142      * Velocity friendly.
143      *
144      * @param key message key
145      * @param args replacement parameters for this message
146      *
147      * @return the localized message for the specified key or
148      * <code>null</code> if no such message exists
149      */

150     public String JavaDoc get(String JavaDoc key, List JavaDoc args)
151     {
152         return get(key, args.toArray());
153     }
154
155     /**
156      * Same as {@link #get(String key, Object[] args)}, but takes a
157      * <code>java.util.List</code> instead of an array. This is more
158      * Velocity friendly.
159      *
160      * @param key message key
161      * @param bundle The bundle name to look for.
162      * @param args replacement parameters for this message
163      * @since VelocityTools 1.1
164      * @return the localized message for the specified key or
165      * <code>null</code> if no such message exists
166      */

167     public String JavaDoc get(String JavaDoc key, String JavaDoc bundle, List JavaDoc args)
168     {
169         return get(key, bundle, args.toArray());
170     }
171
172
173     /**
174      * Checks if a message string for a specified message key exists
175      * for the user's locale.
176      *
177      * @param key message key
178      *
179      * @return <code>true</code> if a message strings exists,
180      * <code>false</code> otherwise
181      */

182     public boolean exists(String JavaDoc key)
183     {
184         return exists(key, null);
185     }
186
187     /**
188      * Checks if a message string for a specified message key exists
189      * for the user's locale.
190      *
191      * @param key message key
192      * @param bundle The bundle name to look for.
193      * @since VelocityTools 1.1
194      * @return <code>true</code> if a message strings exists,
195      * <code>false</code> otherwise
196      */

197     public boolean exists(String JavaDoc key, String JavaDoc bundle)
198     {
199         MessageResources res = getResources(bundle);
200         if (res == null)
201         {
202             return false;
203         }
204
205         // Return the requested message presence indicator
206
return res.isPresent(this.locale, key);
207     }
208
209
210     /**
211      * Returns the user's locale. If a locale is not found, the default
212      * locale is returned.
213      * @deprecated This does not fit the purpose of MessageTool and will be
214      * removed in VelocityTools 1.2
215      */

216     public Locale JavaDoc getLocale()
217     {
218         return this.locale;
219     }
220
221 }
222
Popular Tags