1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.impl; |
16 |
| |
17 |
| import java.util.Locale; |
18 |
| import java.util.MissingResourceException; |
19 |
| import java.util.ResourceBundle; |
20 |
| |
21 |
| import org.apache.commons.logging.Log; |
22 |
| import org.apache.commons.logging.LogFactory; |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class MessageFormatter extends AbstractMessages |
31 |
| { |
32 |
| private Log _log; |
33 |
| |
34 |
| private ResourceBundle _bundle; |
35 |
| |
36 |
13
| public MessageFormatter(Log log, ResourceBundle bundle)
|
37 |
| { |
38 |
13
| _log = log;
|
39 |
13
| _bundle = bundle;
|
40 |
| } |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
11
| public MessageFormatter(Class referenceClass)
|
49 |
| { |
50 |
11
| this(referenceClass, getStringsName(referenceClass));
|
51 |
| } |
52 |
| |
53 |
13
| public MessageFormatter(Class referenceClass, String name)
|
54 |
| { |
55 |
13
| this(LogFactory.getLog(referenceClass), referenceClass, name);
|
56 |
| } |
57 |
| |
58 |
13
| public MessageFormatter(Log log, Class referenceClass, String name)
|
59 |
| { |
60 |
13
| this(log, getResourceBundleName(referenceClass, name));
|
61 |
| } |
62 |
| |
63 |
13
| public MessageFormatter(Log log, String bundleName)
|
64 |
| { |
65 |
13
| this(log, ResourceBundle.getBundle(bundleName));
|
66 |
| } |
67 |
| |
68 |
297
| protected String findMessage(String key)
|
69 |
| { |
70 |
297
| try
|
71 |
| { |
72 |
297
| return _bundle.getString(key);
|
73 |
| } |
74 |
| catch (MissingResourceException ex) |
75 |
| { |
76 |
0
| _log.error("Missing resource key: " + key + ".");
|
77 |
0
| return null;
|
78 |
| } |
79 |
| } |
80 |
| |
81 |
241
| protected Locale getLocale()
|
82 |
| { |
83 |
241
| return Locale.getDefault();
|
84 |
| } |
85 |
| |
86 |
11
| private static String getStringsName(Class referenceClass)
|
87 |
| { |
88 |
11
| String className = referenceClass.getName();
|
89 |
| |
90 |
11
| int lastDotIndex = className.lastIndexOf('.');
|
91 |
| |
92 |
11
| String justClass = className.substring(lastDotIndex + 1);
|
93 |
| |
94 |
11
| int mpos = justClass.indexOf("Messages");
|
95 |
| |
96 |
11
| return justClass.substring(0, mpos) + "Strings";
|
97 |
| } |
98 |
| |
99 |
13
| private static String getResourceBundleName(Class referenceClass, String name)
|
100 |
| { |
101 |
13
| String packageName = null;
|
102 |
13
| if (referenceClass.getPackage() != null)
|
103 |
| { |
104 |
12
| packageName = referenceClass.getPackage().getName();
|
105 |
| } |
106 |
| else |
107 |
| { |
108 |
1
| final int lastDotIndex = referenceClass.getName().lastIndexOf('.');
|
109 |
1
| packageName = (lastDotIndex == -1 ? "" : referenceClass.getName().substring(
|
110 |
| 0, |
111 |
| lastDotIndex)); |
112 |
| |
113 |
| } |
114 |
13
| return packageName.equals("") ? name : packageName + "." + name;
|
115 |
| } |
116 |
| } |