Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Messages |
|
| 1.375;1.375 |
1 | /* | |
2 | * $Id: Messages.java 354330 2005-12-06 06:05:19Z niallp $ | |
3 | * $Revision: 354330 $ | |
4 | * $Date: 2005-12-06 06:05:19 +0000 (Tue, 06 Dec 2005) $ | |
5 | * | |
6 | * ==================================================================== | |
7 | * | |
8 | * Copyright 2003-2005 The Apache Software Foundation | |
9 | * | |
10 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
11 | * you may not use this file except in compliance with the License. | |
12 | * You may obtain a copy of the License at | |
13 | * | |
14 | * http://www.apache.org/licenses/LICENSE-2.0 | |
15 | * | |
16 | * Unless required by applicable law or agreed to in writing, software | |
17 | * distributed under the License is distributed on an "AS IS" BASIS, | |
18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
19 | * See the License for the specific language governing permissions and | |
20 | * limitations under the License. | |
21 | * | |
22 | */ | |
23 | ||
24 | package org.apache.commons.resources; | |
25 | ||
26 | import java.io.Serializable; | |
27 | import java.text.MessageFormat; | |
28 | import java.util.Locale; | |
29 | ||
30 | import org.apache.commons.logging.Log; | |
31 | import org.apache.commons.logging.LogFactory; | |
32 | import org.apache.commons.resources.impl.ResourceBundleResourcesFactory; | |
33 | ||
34 | /** | |
35 | * <p>Wrapper around any {@link Resources} object that performs message | |
36 | * string lookups from the {@link Resources} instance, and parameter | |
37 | * replacement via <code>java.text.MessageFormat</code>. For convenience, | |
38 | * the same functionality is also available via static methods that accept | |
39 | * a {@link Resources} parameter.</p> | |
40 | * | |
41 | * <p>Calls to <code>getMessage()</code> variants without a <code>Locale</code> | |
42 | * argument are presumed to be requesting a message string in the default | |
43 | * <code>Locale</code> for this JVM.</p> | |
44 | * | |
45 | * <p>When a <code>getString()</code> call to the underlying {@link Resources} | |
46 | * instance fails or returns null, a suitable error message String will be | |
47 | * returned.</p> | |
48 | */ | |
49 | public class Messages implements Serializable { | |
50 | ||
51 | ||
52 | // ----------------------------------------------------------- Constructors | |
53 | ||
54 | ||
55 | /** | |
56 | * <p>Construct a {@link Messages} instance that wraps the specified | |
57 | * {@link Resources} instance.</p> | |
58 | * | |
59 | * @param resources The {@link Resources} instance from which | |
60 | * message strings are to be retrieved | |
61 | */ | |
62 | 0 | public Messages(Resources resources) { |
63 | ||
64 | 0 | this.resources = resources; |
65 | ||
66 | 0 | } |
67 | ||
68 | ||
69 | // ----------------------------------------------------- Instance Variables | |
70 | ||
71 | ||
72 | /** | |
73 | * <p>The {@link Resources} instance that we are wrapping.</p> | |
74 | */ | |
75 | 0 | private Resources resources = null; |
76 | ||
77 | ||
78 | // ------------------------------------------------------------- Properties | |
79 | ||
80 | ||
81 | /** | |
82 | * <p>Return the {@link Resources} instance that we are wrapping.</p> | |
83 | * | |
84 | * @return The wrapped resources. | |
85 | */ | |
86 | public Resources getResources() { | |
87 | ||
88 | 0 | return (this.resources); |
89 | ||
90 | } | |
91 | ||
92 | ||
93 | // --------------------------------------------------------- Public Methods | |
94 | ||
95 | ||
96 | /** | |
97 | * <p>Return a text message for the specified key, for the default | |
98 | * <code>Locale</code>.</p> | |
99 | * | |
100 | * @param key Message key to retrieve | |
101 | * @return The text message for the specified key. | |
102 | */ | |
103 | public String getMessage(String key) { | |
104 | ||
105 | 0 | return (getMessage(resources, key)); |
106 | ||
107 | } | |
108 | ||
109 | ||
110 | /** | |
111 | * <p>Return a text message for the specified key, for the specified | |
112 | * <code>Locale</code>.</p> | |
113 | * | |
114 | * @param locale <code>Locale</code> for which to retrieve the message | |
115 | * @param key Message key to retrieve | |
116 | * @return The text message for the specified key and locale. | |
117 | */ | |
118 | public String getMessage(Locale locale, String key) { | |
119 | ||
120 | 0 | return (getMessage(resources, locale, key)); |
121 | ||
122 | } | |
123 | ||
124 | ||
125 | /** | |
126 | * <p>Return a text message for the specified key, for the default | |
127 | * <code>Locale</code>, with parametric replacement.</p> | |
128 | * | |
129 | * @param key Message key to retrieve | |
130 | * @param args Array of replacement values | |
131 | * @return The text message with parametric replacement. | |
132 | */ | |
133 | public String getMessage(String key, Object[] args) { | |
134 | 0 | return getMessage(resources, key, args); |
135 | } | |
136 | ||
137 | ||
138 | /** | |
139 | * <p>Return a text message for the specified key, for the specified | |
140 | * <code>Locale</code>, with parametric replacement.</p> | |
141 | * | |
142 | * @param locale <code>Locale</code> for which to retrieve the message | |
143 | * @param key Message key to retrieve | |
144 | * @param args Array of replacement values | |
145 | * @return The text message for a spcified locale with parametric replacement. | |
146 | */ | |
147 | public String getMessage(Locale locale, String key, Object[] args) { | |
148 | 0 | return getMessage(resources, locale, key, args); |
149 | } | |
150 | ||
151 | ||
152 | /** | |
153 | * <p>Return a text message for the specified key, for the default | |
154 | * <code>Locale</code>, with parametric replacement.</p> | |
155 | * | |
156 | * @param key Message key to retrieve | |
157 | * @param arg0 Individual parameter replacement value | |
158 | * @return The text message with parametric replacement. | |
159 | */ | |
160 | public String getMessage(String key, Object arg0) { | |
161 | ||
162 | 0 | return (getMessage(resources, key, arg0)); |
163 | ||
164 | } | |
165 | ||
166 | ||
167 | /** | |
168 | * <p>Return a text message for the specified key, for the specified | |
169 | * <code>Locale</code>, with parametric replacement.</p> | |
170 | * | |
171 | * @param locale <code>Locale</code> for which to retrieve the message | |
172 | * @param key Message key to retrieve | |
173 | * @param arg0 Individual parameter replacement value | |
174 | * @return The text message for a spcified locale with parametric replacement. | |
175 | */ | |
176 | public String getMessage(Locale locale, String key, Object arg0) { | |
177 | ||
178 | 0 | return (getMessage(resources, locale, key, arg0)); |
179 | ||
180 | } | |
181 | ||
182 | ||
183 | // ------------------------------------------------------- Static Variables | |
184 | ||
185 | ||
186 | /** | |
187 | * <p>The {@link org.apache.commons.resources.ResourcesFactory} that will be used by the | |
188 | * <code>getMessages()</code> method.</p> | |
189 | */ | |
190 | 0 | private static ResourcesFactory factory = null; |
191 | ||
192 | ||
193 | // --------------------------------------------------------- Static Methods | |
194 | ||
195 | /** | |
196 | * <p>Set the {@link org.apache.commons.resources.ResourcesFactory} that | |
197 | * will be used by the <code>getMessages()</code> method.</p> | |
198 | * | |
199 | * @param factory ResourcesFactory instance to set. | |
200 | */ | |
201 | public static void setFactory(ResourcesFactory factory) { | |
202 | 0 | Messages.factory = factory; |
203 | 0 | } |
204 | ||
205 | /** | |
206 | * <p>Return a text message for the specified key, for the default | |
207 | * <code>Locale</code>.</p> | |
208 | * | |
209 | * @param resources {@link Resources} instance to retrieve the message from | |
210 | * @param key Message key to retrieve | |
211 | * @return The text message. | |
212 | */ | |
213 | public static String getMessage(Resources resources, String key) { | |
214 | ||
215 | 0 | return (getMessage(resources, (Locale) null, key)); |
216 | ||
217 | } | |
218 | ||
219 | ||
220 | /** | |
221 | * <p>Return a text message for the specified key, for the specified | |
222 | * <code>Locale</code>.</p> | |
223 | * | |
224 | * @param resources {@link Resources} instance to retrieve the message from | |
225 | * @param locale <code>Locale</code> for which to retrieve the message | |
226 | * @param key Message key to retrieve | |
227 | * @return The text message. | |
228 | */ | |
229 | public static String getMessage( | |
230 | Resources resources, | |
231 | Locale locale, | |
232 | String key) { | |
233 | ||
234 | 0 | String message = null; |
235 | try { | |
236 | 0 | message = resources.getString(key, locale); |
237 | 0 | } catch (ResourcesException e) { |
238 | 0 | Log log = LogFactory.getLog(Messages.class); |
239 | 0 | if (log.isDebugEnabled()) { |
240 | 0 | log.debug("Failed retrieving message for key: '" + key + "'.", e); |
241 | } | |
242 | 0 | } |
243 | ||
244 | 0 | if (message == null && !resources.isReturnNull()) { |
245 | 0 | message = "???" + key + "???"; |
246 | } | |
247 | ||
248 | 0 | return message; |
249 | } | |
250 | ||
251 | ||
252 | /** | |
253 | * <p>Return a text message for the specified key, for the default | |
254 | * <code>Locale</code>, with parametric replacement.</p> | |
255 | * | |
256 | * @param resources {@link Resources} instance to retrieve the message from | |
257 | * @param key Message key to retrieve | |
258 | * @param args Array of replacement values | |
259 | * @return The text message. | |
260 | */ | |
261 | public static String getMessage( | |
262 | Resources resources, | |
263 | String key, | |
264 | Object[] args) { | |
265 | ||
266 | 0 | return getMessage(resources, (Locale) null, key, args); |
267 | } | |
268 | ||
269 | ||
270 | /** | |
271 | * <p>Return a text message for the specified key, for the specified | |
272 | * <code>Locale</code>, with parametric replacement.</p> | |
273 | * | |
274 | * @param resources {@link Resources} instance to retrieve the message from | |
275 | * @param locale <code>Locale</code> for which to retrieve the message | |
276 | * @param key Message key to retrieve | |
277 | * @param args Array of replacement values | |
278 | * @return The text message. | |
279 | */ | |
280 | public static String getMessage( | |
281 | Resources resources, | |
282 | Locale locale, | |
283 | String key, | |
284 | Object[] args) { | |
285 | ||
286 | // TODO - Cache MessageFormat instances? | |
287 | 0 | String message = getMessage(resources, locale, key); |
288 | 0 | MessageFormat format = new MessageFormat(message); |
289 | 0 | return (format.format(args)); |
290 | } | |
291 | ||
292 | ||
293 | /** | |
294 | * <p>Return a text message for the specified key, for the default | |
295 | * <code>Locale</code>, with parametric replacement.</p> | |
296 | * | |
297 | * @param resources {@link Resources} instance to retrieve the message from | |
298 | * @param key Message key to retrieve | |
299 | * @param arg0 Individual parameter replacement value | |
300 | * @return The text message. | |
301 | */ | |
302 | public static String getMessage(Resources resources, | |
303 | String key, Object arg0) { | |
304 | ||
305 | 0 | return (getMessage(resources, (Locale) null, key, arg0)); |
306 | ||
307 | } | |
308 | ||
309 | /** | |
310 | * <p>Return a text message for the specified key, for the specified | |
311 | * <code>Locale</code>, with parametric replacement.</p> | |
312 | * | |
313 | * @param resources {@link Resources} instance to retrieve the message from | |
314 | * @param locale <code>Locale</code> for which to retrieve the message | |
315 | * @param key Message key to retrieve | |
316 | * @param arg0 Individual parameter replacement value | |
317 | * @return The text message. | |
318 | */ | |
319 | public static String getMessage( | |
320 | Resources resources, | |
321 | Locale locale, | |
322 | String key, | |
323 | Object arg0) { | |
324 | ||
325 | 0 | return getMessage(resources, locale, key, new Object[] { arg0 }); |
326 | } | |
327 | ||
328 | /** | |
329 | * <p>Convenience factory method to create a {@link Messages} instance | |
330 | * that wraps a {@link Resources} instance that contains message resources | |
331 | * for the specified properties file. It is expected that the resources | |
332 | * for each package will be in properties files that are nested in the | |
333 | * package directory, with names like <code>LocalStrings.properties</code> | |
334 | * for the default messages, and names like | |
335 | * <code>LocalStrings_en_US.properties</code> for messages localized to | |
336 | * a particular <code>Locale</code>.</p> | |
337 | * | |
338 | * @param name Package + file name of the properties file for which | |
339 | * local message resources are desired (ie. org.apache.commons.resources.LocalStrings). | |
340 | * @return The text messages. | |
341 | */ | |
342 | public static Messages getMessages(String name) { | |
343 | ||
344 | 0 | if (factory == null) { |
345 | 0 | factory = new ResourceBundleResourcesFactory(); |
346 | } | |
347 | ||
348 | try { | |
349 | 0 | Resources resources = factory.getResources(name); |
350 | 0 | return (new Messages(resources)); |
351 | ||
352 | 0 | } catch (ResourcesException e) { |
353 | 0 | return (null); |
354 | } | |
355 | ||
356 | } | |
357 | ||
358 | ||
359 | } |