1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.impl; |
16 |
| |
17 |
| import java.text.MessageFormat; |
18 |
| import java.util.Locale; |
19 |
| |
20 |
| import org.apache.hivemind.HiveMind; |
21 |
| import org.apache.hivemind.Messages; |
22 |
| import org.apache.hivemind.util.Defense; |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public abstract class AbstractMessages implements Messages |
32 |
| { |
33 |
241
| public String format(String key, Object[] args)
|
34 |
| { |
35 |
241
| String pattern = getMessage(key);
|
36 |
| |
37 |
241
| for (int i = 0; i < args.length; i++)
|
38 |
| { |
39 |
475
| Object arg = args[i];
|
40 |
| |
41 |
475
| if (arg != null && arg instanceof Throwable)
|
42 |
51
| args[i] = extractMessage((Throwable) arg);
|
43 |
| } |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
241
| MessageFormat messageFormat = new MessageFormat("");
|
52 |
241
| messageFormat.setLocale(getLocale());
|
53 |
241
| messageFormat.applyPattern(pattern);
|
54 |
| |
55 |
241
| return messageFormat.format(args);
|
56 |
| } |
57 |
| |
58 |
51
| private String extractMessage(Throwable t)
|
59 |
| { |
60 |
51
| String message = t.getMessage();
|
61 |
| |
62 |
51
| return HiveMind.isNonBlank(message) ? message : t.getClass().getName();
|
63 |
| } |
64 |
| |
65 |
69
| public String format(String key, Object arg0)
|
66 |
| { |
67 |
69
| return format(key, new Object[]
|
68 |
| { arg0 }); |
69 |
| } |
70 |
| |
71 |
125
| public String format(String key, Object arg0, Object arg1)
|
72 |
| { |
73 |
125
| return format(key, new Object[]
|
74 |
| { arg0, arg1 }); |
75 |
| } |
76 |
| |
77 |
28
| public String format(String key, Object arg0, Object arg1, Object arg2)
|
78 |
| { |
79 |
28
| return format(key, new Object[]
|
80 |
| { arg0, arg1, arg2 }); |
81 |
| } |
82 |
| |
83 |
301
| public String getMessage(String key)
|
84 |
| { |
85 |
301
| Defense.notNull(key, "key");
|
86 |
| |
87 |
301
| String result = findMessage(key);
|
88 |
| |
89 |
301
| if (result == null)
|
90 |
0
| result = "[" + key.toUpperCase() + "]";
|
91 |
| |
92 |
301
| return result;
|
93 |
| } |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| protected abstract Locale getLocale(); |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| protected abstract String findMessage(String key); |
109 |
| |
110 |
| } |