Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LocaleMessageHandler |
|
| 2.25;2.25 |
1 | /* | |
2 | * $Id: LocaleMessageHandler.java 8083 2007-08-28 02:25:36Z aperepel $ | |
3 | * -------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com | |
5 | * | |
6 | * The software in this package is published under the terms of the CPAL v1.0 | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
9 | */ | |
10 | ||
11 | package org.mule.config.i18n; | |
12 | ||
13 | import java.text.MessageFormat; | |
14 | import java.util.Locale; | |
15 | import java.util.ResourceBundle; | |
16 | ||
17 | import org.apache.commons.logging.Log; | |
18 | import org.apache.commons.logging.LogFactory; | |
19 | ||
20 | /** | |
21 | * The <code>LocaleMessageHandler</code> is essentially a merging of the Message | |
22 | * and Messages classes, since there is no good reason to have them separate. A | |
23 | * key point is that this Handler is meant to be used for application-specific | |
24 | * messages, rather than core system messages. (That's not to say it couldn't | |
25 | * eventually replace the latter, however). Note that message codes are Strings | |
26 | * here, instead of the ints in Message. | |
27 | * | |
28 | * The LocaleMessageHandler can be called directly, but is really meant to be | |
29 | * called by LocaleMessage classes as done in the examples. | |
30 | * | |
31 | * Note that this class assumes the resource bundle is in the format | |
32 | * <bundle-name>-messages and is located at the top of the jar or classes | |
33 | * directory. We can later add the ability to specify a path prefix. | |
34 | */ | |
35 | 0 | public class LocaleMessageHandler |
36 | { | |
37 | /** | |
38 | * logger used by this class | |
39 | */ | |
40 | 0 | protected static final Log logger = LogFactory.getLog(LocaleMessageHandler.class); |
41 | ||
42 | /** | |
43 | * Get the resource string for the given bundle name and resource code | |
44 | */ | |
45 | public static String getString(String bundleName, String code) | |
46 | { | |
47 | 0 | return getString(bundleName, code, new Object[] {}); |
48 | } | |
49 | ||
50 | /** | |
51 | * Get the resource string for the given bundle name, resource code and | |
52 | * one argument | |
53 | */ | |
54 | public static String getString(String bundleName, String code, Object arg1) | |
55 | { | |
56 | 0 | if (arg1 == null) |
57 | { | |
58 | 0 | arg1 = "null"; |
59 | } | |
60 | ||
61 | 0 | return getString(bundleName, code, new Object[]{arg1}); |
62 | } | |
63 | ||
64 | /** | |
65 | * Get the resource string for the given bundle name, resource code and | |
66 | * two arguments | |
67 | */ | |
68 | public static String getString(String bundleName, String code, Object arg1, Object arg2) | |
69 | { | |
70 | 0 | if (arg1 == null) |
71 | { | |
72 | 0 | arg1 = "null"; |
73 | } | |
74 | ||
75 | 0 | if (arg2 == null) |
76 | { | |
77 | 0 | arg2 = "null"; |
78 | } | |
79 | ||
80 | 0 | return getString(bundleName, code, new Object[]{arg1, arg2}); |
81 | } | |
82 | ||
83 | /** | |
84 | * Get the resource string for the given bundle name, resource code and array | |
85 | * of arguments. All above methods invoke this one. | |
86 | */ | |
87 | public static String getString(String bundleName, String code, Object[] args) | |
88 | { | |
89 | 0 | String path = bundleName + "-messages"; |
90 | 0 | Locale locale = Locale.getDefault(); |
91 | 0 | ResourceBundle bundle = ResourceBundle.getBundle(path, locale); |
92 | 0 | String m = bundle.getString(code); |
93 | ||
94 | 0 | if (m == null) |
95 | { | |
96 | 0 | logger.error("Failed to find message for id " + code + " in resource bundle " + path); |
97 | 0 | return ""; |
98 | } | |
99 | ||
100 | 0 | return MessageFormat.format(m, args); |
101 | } | |
102 | ||
103 | } | |
104 |