|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractMessages.java | 87.5% | 94.7% | 100% | 93.9% |
|
1 |
// Copyright 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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 |
* Abstract base class for implementations of {@link org.apache.hivemind.Messages}. Subclasses must
|
|
26 |
* provide {@link #getLocale()}and {@link #findMessage(String)}implementations.
|
|
27 |
*
|
|
28 |
* @author Howard M. Lewis Ship
|
|
29 |
* @since 1.1
|
|
30 |
*/
|
|
31 |
public abstract class AbstractMessages implements Messages |
|
32 |
{ |
|
33 | 239 |
public String format(String key, Object[] args)
|
34 |
{ |
|
35 | 239 |
String pattern = getMessage(key); |
36 |
|
|
37 | 239 |
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 |
// This ugliness is mandated for JDK 1.3 compatibility, which has a bug
|
|
46 |
// in MessageFormat ... the
|
|
47 |
// pattern is applied in the constructor, using the system default Locale,
|
|
48 |
// regardless of what locale is later specified!
|
|
49 |
// It appears that the problem does not exist in JDK 1.4.
|
|
50 |
|
|
51 | 239 |
MessageFormat messageFormat = new MessageFormat(""); |
52 | 239 |
messageFormat.setLocale(getLocale()); |
53 | 239 |
messageFormat.applyPattern(pattern); |
54 |
|
|
55 | 239 |
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 | 66 |
public String format(String key, Object arg0)
|
66 |
{ |
|
67 | 66 |
return format(key, new Object[] |
68 |
{ arg0 }); |
|
69 |
} |
|
70 |
|
|
71 | 124 |
public String format(String key, Object arg0, Object arg1)
|
72 |
{ |
|
73 | 124 |
return format(key, new Object[] |
74 |
{ arg0, arg1 }); |
|
75 |
} |
|
76 |
|
|
77 | 31 |
public String format(String key, Object arg0, Object arg1, Object arg2)
|
78 |
{ |
|
79 | 31 |
return format(key, new Object[] |
80 |
{ arg0, arg1, arg2 }); |
|
81 |
} |
|
82 |
|
|
83 | 299 |
public String getMessage(String key)
|
84 |
{ |
|
85 | 299 |
Defense.notNull(key, "key");
|
86 |
|
|
87 | 299 |
String result = findMessage(key); |
88 |
|
|
89 | 299 |
if (result == null) |
90 | 0 |
result = "[" + key.toUpperCase() + "]"; |
91 |
|
|
92 | 299 |
return result;
|
93 |
} |
|
94 |
|
|
95 |
/**
|
|
96 |
* Concrete implementations must provide a non-null Locale.
|
|
97 |
*/
|
|
98 |
|
|
99 |
protected abstract Locale getLocale();
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Concrete implementations must implement this method.
|
|
103 |
*
|
|
104 |
* @param key
|
|
105 |
* @return the localized message for the key, or null if no such message exists.
|
|
106 |
*/
|
|
107 |
|
|
108 |
protected abstract String findMessage(String key);
|
|
109 |
|
|
110 |
} |
|