|
|||||||||||||||||||
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 | |||||||||||||||
MessageFormatter.java | 80% | 79.3% | 100% | 83.7% |
|
1 |
// Copyright 2004, 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.MissingResourceException;
|
|
19 |
import java.util.ResourceBundle;
|
|
20 |
|
|
21 |
import org.apache.commons.logging.Log;
|
|
22 |
import org.apache.commons.logging.LogFactory;
|
|
23 |
import org.apache.hivemind.HiveMind;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* A wrapper around {@link java.util.ResourceBundle} that makes
|
|
27 |
* it easier to access and format messages.
|
|
28 |
*
|
|
29 |
* @author Howard Lewis Ship
|
|
30 |
*/
|
|
31 |
public class MessageFormatter |
|
32 |
{ |
|
33 |
private Log _log;
|
|
34 |
private ResourceBundle _bundle;
|
|
35 |
|
|
36 | 12 |
public MessageFormatter(Log log, ResourceBundle bundle)
|
37 |
{ |
|
38 | 12 |
_log = log; |
39 | 12 |
_bundle = bundle; |
40 |
} |
|
41 |
|
|
42 | 12 |
public MessageFormatter(Class referenceClass, String name)
|
43 |
{ |
|
44 | 12 |
this(LogFactory.getLog(referenceClass), referenceClass, name);
|
45 |
} |
|
46 |
|
|
47 | 12 |
public MessageFormatter(Log log, Class referenceClass, String name)
|
48 |
{ |
|
49 | 12 |
this(log, referenceClass.getPackage().getName() + "." + name); |
50 |
} |
|
51 |
|
|
52 | 12 |
public MessageFormatter(Log log, String bundleName)
|
53 |
{ |
|
54 | 12 |
this(log, ResourceBundle.getBundle(bundleName));
|
55 |
} |
|
56 |
|
|
57 | 280 |
public String getMessage(String key)
|
58 |
{ |
|
59 | 280 |
try
|
60 |
{ |
|
61 | 280 |
return _bundle.getString(key);
|
62 |
} |
|
63 |
catch (MissingResourceException ex)
|
|
64 |
{ |
|
65 | 0 |
_log.error("Missing resource key: " + key + "."); |
66 | 0 |
return "[" + key.toUpperCase() + "]"; |
67 |
} |
|
68 |
} |
|
69 |
|
|
70 | 63 |
public String format(String key, Object arg)
|
71 |
{ |
|
72 | 63 |
return format(key, new Object[] { arg }); |
73 |
} |
|
74 |
|
|
75 | 118 |
public String format(String key, Object arg1, Object arg2)
|
76 |
{ |
|
77 | 118 |
return format(key, new Object[] { arg1, arg2 }); |
78 |
} |
|
79 |
|
|
80 | 33 |
public String format(String key, Object arg1, Object arg2, Object arg3)
|
81 |
{ |
|
82 | 33 |
return format(key, new Object[] { arg1, arg2, arg3 }); |
83 |
} |
|
84 |
|
|
85 |
/**
|
|
86 |
* Formats a message using the key to obtain a pattern, and passing the arguments.
|
|
87 |
*
|
|
88 |
* <p>
|
|
89 |
* It is common to pass an exception instance as an arg. Those are treated specially:
|
|
90 |
* The exception instance is replaced with its message {@link Throwable#getMessage()}. If the
|
|
91 |
* message is blank (null or empty), then the exception's class name is used.
|
|
92 |
*/
|
|
93 | 230 |
public String format(String key, Object[] args)
|
94 |
{ |
|
95 | 230 |
String pattern = getMessage(key); |
96 |
|
|
97 | 230 |
if (args == null) |
98 | 0 |
return pattern;
|
99 |
|
|
100 | 230 |
for (int i = 0; i < args.length; i++) |
101 |
{ |
|
102 | 456 |
if (args[i] instanceof Throwable) |
103 |
{ |
|
104 | 48 |
Throwable t = (Throwable) args[i]; |
105 |
|
|
106 | 48 |
args[i] = extractMessage(t); |
107 |
} |
|
108 |
} |
|
109 |
|
|
110 | 230 |
try
|
111 |
{ |
|
112 | 230 |
return MessageFormat.format(pattern, args);
|
113 |
} |
|
114 |
catch (Exception ex)
|
|
115 |
{ |
|
116 | 0 |
_log.error("Unable to format message: \"" + pattern + "\" from key " + key + ".", ex); |
117 |
|
|
118 | 0 |
return null; |
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
/**
|
|
123 |
* Extracts the message from an exception. If the message is null, the the class name
|
|
124 |
* of the exception is returned.
|
|
125 |
*
|
|
126 |
*/
|
|
127 | 48 |
private String extractMessage(Throwable t)
|
128 |
{ |
|
129 | 48 |
if (t == null) |
130 | 0 |
return null; |
131 |
|
|
132 | 48 |
String message = t.getMessage(); |
133 |
|
|
134 | 48 |
if (HiveMind.isBlank(message))
|
135 | 5 |
return t.getClass().getName();
|
136 |
|
|
137 | 43 |
return message;
|
138 |
} |
|
139 |
|
|
140 |
} |
|
141 |
|
|