|
|||||||||||||||||||
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 | |||||||||||||||
ComponentMessages.java | 58.3% | 63% | 62.5% | 61.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.tapestry.services.impl;
|
|
16 |
|
|
17 |
import java.text.MessageFormat;
|
|
18 |
import java.util.Locale;
|
|
19 |
import java.util.Properties;
|
|
20 |
|
|
21 |
import org.apache.hivemind.Messages;
|
|
22 |
|
|
23 |
/**
|
|
24 |
* Implementation of {@link org.apache.hivemind.Messages}. This is basically a wrapper around an
|
|
25 |
* instance of {@link Properties}. This ensures that the properties are, in fact, read-only (which
|
|
26 |
* ensures that they don't have to be synchronized).
|
|
27 |
* <p>
|
|
28 |
* TODO: Merge this code with HiveMind's implemention.
|
|
29 |
*
|
|
30 |
* @author Howard Lewis Ship
|
|
31 |
* @since 2.0.4
|
|
32 |
*/
|
|
33 |
|
|
34 |
public class ComponentMessages implements Messages |
|
35 |
{ |
|
36 |
private Properties _properties;
|
|
37 |
|
|
38 |
private Locale _locale;
|
|
39 |
|
|
40 | 32 |
public ComponentMessages(Locale locale, Properties properties)
|
41 |
{ |
|
42 | 32 |
_locale = locale; |
43 | 32 |
_properties = properties; |
44 |
} |
|
45 |
|
|
46 | 0 |
public String getMessage(String key, String defaultValue)
|
47 |
{ |
|
48 | 0 |
return _properties.getProperty(key, defaultValue);
|
49 |
} |
|
50 |
|
|
51 | 45 |
public String getMessage(String key)
|
52 |
{ |
|
53 | 45 |
String result = _properties.getProperty(key); |
54 |
|
|
55 | 45 |
if (result == null) |
56 | 1 |
result = "[" + key.toUpperCase() + "]"; |
57 |
|
|
58 | 45 |
return result;
|
59 |
} |
|
60 |
|
|
61 | 0 |
public String format(String key, Object argument1, Object argument2, Object argument3)
|
62 |
{ |
|
63 | 0 |
return format(key, new Object[] |
64 |
{ argument1, argument2, argument3 }); |
|
65 |
} |
|
66 |
|
|
67 | 0 |
public String format(String key, Object argument1, Object argument2)
|
68 |
{ |
|
69 | 0 |
return format(key, new Object[] |
70 |
{ argument1, argument2 }); |
|
71 |
} |
|
72 |
|
|
73 | 2 |
public String format(String key, Object argument)
|
74 |
{ |
|
75 | 2 |
return format(key, new Object[] |
76 |
{ argument }); |
|
77 |
} |
|
78 |
|
|
79 | 2 |
public String format(String key, Object[] arguments)
|
80 |
{ |
|
81 | 2 |
String pattern = getMessage(key); |
82 |
|
|
83 |
// This ugliness is mandated for JDK 1.3 compatibility, which has a bug
|
|
84 |
// in MessageFormat ... the
|
|
85 |
// pattern is applied in the constructor, using the system default Locale,
|
|
86 |
// regardless of what locale is later specified!
|
|
87 |
// It appears that the problem does not exist in JDK 1.4.
|
|
88 |
|
|
89 | 2 |
MessageFormat messageFormat = new MessageFormat(""); |
90 | 2 |
messageFormat.setLocale(_locale); |
91 | 2 |
messageFormat.applyPattern(pattern); |
92 |
|
|
93 | 2 |
convert(arguments); |
94 |
|
|
95 | 2 |
return messageFormat.format(arguments);
|
96 |
} |
|
97 |
|
|
98 | 2 |
private void convert(Object[] arguments) |
99 |
{ |
|
100 | 2 |
if (arguments == null) |
101 | 0 |
return;
|
102 |
|
|
103 | 2 |
for (int i = 0; i < arguments.length; i++) |
104 |
{ |
|
105 | 2 |
if (arguments[i] == null) |
106 | 0 |
continue;
|
107 |
|
|
108 | 2 |
if (arguments[i] instanceof Throwable) |
109 |
{ |
|
110 | 0 |
Throwable t = (Throwable) arguments[i]; |
111 |
|
|
112 | 0 |
String message = t.getMessage(); |
113 |
|
|
114 | 0 |
if (message == null) |
115 | 0 |
message = t.getClass().getName(); |
116 |
|
|
117 | 0 |
arguments[i] = message; |
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
} |
|