KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > viewtools > MessagesTools


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

59 public class MessagesTools extends MessageResourcesTool {
60
61     /**
62      * Default constructor. Tool must be initialized before use.
63      */

64     public MessagesTools() {
65     }
66
67     /**
68      * Looks up and returns the localized message for the specified key. The
69      * user's locale is consulted to determine the language of the message.
70      *
71      * @param key
72      * message key
73      *
74      * @return the localized message for the specified key or <code>null</code>
75      * if no such message exists
76      */

77     public String JavaDoc get(String JavaDoc key) {
78         return get(key, (Object JavaDoc[]) null);
79     }
80
81     /**
82      * Looks up and returns the localized message for the specified key. The
83      * user's locale is consulted to determine the language of the message.
84      *
85      * @param key
86      * message key
87      * @param bundle
88      * The bundle name to look for.
89      *
90      * @return the localized message for the specified key or <code>null</code>
91      * if no such message exists
92      * @since VelocityTools 1.1
93      */

94     public String JavaDoc get(String JavaDoc key, String JavaDoc bundle) {
95         return get(key, bundle, (Object JavaDoc[]) null);
96     }
97
98     /**
99      * Looks up and returns the localized message for the specified key.
100      * Replacement parameters passed with <code>args</code> are inserted into
101      * the message. The user's locale is consulted to determine the language of
102      * the message.
103      *
104      * @param key
105      * message key
106      * @param args
107      * replacement parameters for this message
108      *
109      * @return the localized message for the specified key or <code>null</code>
110      * if no such message exists
111      */

112     public String JavaDoc get(String JavaDoc key, Object JavaDoc args[]) {
113         return get(key, null, args);
114     }
115
116     /**
117      * Looks up and returns the localized message for the specified key.
118      * Replacement parameters passed with <code>args</code> are inserted into
119      * the message. The user's locale is consulted to determine the language of
120      * the message.
121      *
122      * @param key
123      * message key
124      * @param bundle
125      * The bundle name to look for.
126      * @param args
127      * replacement parameters for this message
128      * @since VelocityTools 1.1
129      * @return the localized message for the specified key or <code>null</code>
130      * if no such message exists
131      */

132     public String JavaDoc get(String JavaDoc key, String JavaDoc bundle, Object JavaDoc args[]) {
133         MessageResources res = getResources(bundle);
134         if (res == null) {
135             return key;
136         }
137
138         // return the requested message
139
if (args == null) {
140             String JavaDoc x = res.getMessage(this.locale, key);
141             if (x != null && ! x.startsWith("???")) {
142                 return x;
143             } else {
144                 return key;
145             }
146
147         } else {
148             return res.getMessage(this.locale, key, args);
149         }
150     }
151
152     /**
153      * Same as {@link #get(String key, Object[] args)}, but takes a
154      * <code>java.util.List</code> instead of an array. This is more Velocity
155      * friendly.
156      *
157      * @param key
158      * message key
159      * @param args
160      * replacement parameters for this message
161      *
162      * @return the localized message for the specified key or <code>null</code>
163      * if no such message exists
164      */

165     public String JavaDoc get(String JavaDoc key, List JavaDoc args) {
166         return get(key, args.toArray());
167     }
168
169     /**
170      * Same as {@link #get(String key, Object[] args)}, but takes a
171      * <code>java.util.List</code> instead of an array. This is more Velocity
172      * friendly.
173      *
174      * @param key
175      * message key
176      * @param bundle
177      * The bundle name to look for.
178      * @param args
179      * replacement parameters for this message
180      * @since VelocityTools 1.1
181      * @return the localized message for the specified key or <code>null</code>
182      * if no such message exists
183      */

184     public String JavaDoc get(String JavaDoc key, String JavaDoc bundle, List JavaDoc args) {
185         return get(key, bundle, args.toArray());
186     }
187
188     /**
189      * Checks if a message string for a specified message key exists for the
190      * user's locale.
191      *
192      * @param key
193      * message key
194      *
195      * @return <code>true</code> if a message strings exists,
196      * <code>false</code> otherwise
197      */

198     public boolean exists(String JavaDoc key) {
199         return exists(key, null);
200     }
201
202     /**
203      * Checks if a message string for a specified message key exists for the
204      * user's locale.
205      *
206      * @param key
207      * message key
208      * @param bundle
209      * The bundle name to look for.
210      * @since VelocityTools 1.1
211      * @return <code>true</code> if a message strings exists,
212      * <code>false</code> otherwise
213      */

214     public boolean exists(String JavaDoc key, String JavaDoc bundle) {
215         MessageResources res = getResources(bundle);
216         if (res == null) {
217             return false;
218         }
219
220         // Return the requested message presence indicator
221
return res.isPresent(this.locale, key);
222     }
223
224     /**
225      * Returns the user's locale. If a locale is not found, the default locale
226      * is returned.
227      *
228      * @deprecated This does not fit the purpose of MessageTool and will be
229      * removed in VelocityTools 1.2
230      */

231     public Locale JavaDoc getLocale() {
232         return this.locale;
233     }
234 }
Popular Tags