6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: Messages.java,v 1.1.4.1 2005/09/08 11:03:10 suresh_emailid Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xml.internal.serializer.utils;
|
|
24 |
|
16953
|
25 |
import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
|
6
|
26 |
import java.util.ListResourceBundle;
|
|
27 |
import java.util.Locale;
|
|
28 |
import java.util.MissingResourceException;
|
|
29 |
import java.util.ResourceBundle;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* A utility class for issuing error messages.
|
|
33 |
*
|
|
34 |
* A user of this class normally would create a singleton
|
|
35 |
* instance of this class, passing the name
|
|
36 |
* of the message class on the constructor. For example:
|
|
37 |
* <CODE>
|
|
38 |
* static Messages x = new Messages("org.package.MyMessages");
|
|
39 |
* </CODE>
|
|
40 |
* Later the message is typically generated this way if there are no
|
|
41 |
* substitution arguments:
|
|
42 |
* <CODE>
|
|
43 |
* String msg = x.createMessage(org.package.MyMessages.KEY_ONE, null);
|
|
44 |
* </CODE>
|
|
45 |
* If there are arguments substitutions then something like this:
|
|
46 |
* <CODE>
|
|
47 |
* String filename = ...;
|
|
48 |
* String directory = ...;
|
|
49 |
* String msg = x.createMessage(org.package.MyMessages.KEY_TWO,
|
|
50 |
* new Object[] {filename, directory) );
|
|
51 |
* </CODE>
|
|
52 |
*
|
|
53 |
* The constructor of an instance of this class must be given
|
|
54 |
* the class name of a class that extends java.util.ListResourceBundle
|
|
55 |
* ("org.package.MyMessages" in the example above).
|
|
56 |
* The name should not have any language suffix
|
|
57 |
* which will be added automatically by this utility class.
|
|
58 |
*
|
|
59 |
* The message class ("org.package.MyMessages")
|
|
60 |
* must define the abstract method getContents() that is
|
|
61 |
* declared in its base class, for example:
|
|
62 |
* <CODE>
|
|
63 |
* public Object[][] getContents() {return contents;}
|
|
64 |
* </CODE>
|
|
65 |
*
|
|
66 |
* It is suggested that the message class expose its
|
|
67 |
* message keys like this:
|
|
68 |
* <CODE>
|
|
69 |
* public static final String KEY_ONE = "KEY1";
|
|
70 |
* public static final String KEY_TWO = "KEY2";
|
|
71 |
* . . .
|
|
72 |
* </CODE>
|
|
73 |
* and used through their names (KEY_ONE ...) rather than
|
|
74 |
* their values ("KEY1" ...).
|
|
75 |
*
|
|
76 |
* The field contents (returned by getContents()
|
|
77 |
* should be initialized something like this:
|
|
78 |
* <CODE>
|
|
79 |
* public static final Object[][] contents = {
|
|
80 |
* { KEY_ONE, "Something has gone wrong!" },
|
|
81 |
* { KEY_TWO, "The file ''{0}'' does not exist in directory ''{1}''." },
|
|
82 |
* . . .
|
|
83 |
* { KEY_N, "Message N" } }
|
|
84 |
* </CODE>
|
|
85 |
*
|
|
86 |
* Where that section of code with the KEY to Message mappings
|
|
87 |
* (where the message classes 'contents' field is initialized)
|
|
88 |
* can have the Message strings translated in an alternate language
|
|
89 |
* in a errorResourceClass with a language suffix.
|
|
90 |
*
|
|
91 |
*
|
|
92 |
* This class is not a public API, it is only public because it is
|
|
93 |
* used in com.sun.org.apache.xml.internal.serializer.
|
|
94 |
*
|
|
95 |
* @xsl.usage internal
|
|
96 |
*/
|
|
97 |
public final class Messages
|
|
98 |
{
|
|
99 |
/** The local object to use. */
|
|
100 |
private final Locale m_locale = Locale.getDefault();
|
|
101 |
|
|
102 |
/** The language specific resource object for messages. */
|
|
103 |
private ListResourceBundle m_resourceBundle;
|
|
104 |
|
|
105 |
/** The class name of the error message string table with no language suffix. */
|
|
106 |
private String m_resourceBundleName;
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Constructor.
|
|
112 |
* @param resourceBundle the class name of the ListResourceBundle
|
|
113 |
* that the instance of this class is associated with and will use when
|
|
114 |
* creating messages.
|
|
115 |
* The class name is without a language suffix. If the value passed
|
|
116 |
* is null then loadResourceBundle(errorResourceClass) needs to be called
|
|
117 |
* explicitly before any messages are created.
|
|
118 |
*
|
|
119 |
* @xsl.usage internal
|
|
120 |
*/
|
|
121 |
Messages(String resourceBundle)
|
|
122 |
{
|
|
123 |
|
|
124 |
m_resourceBundleName = resourceBundle;
|
|
125 |
}
|
|
126 |
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Get the Locale object that is being used.
|
|
130 |
*
|
|
131 |
* @return non-null reference to Locale object.
|
|
132 |
* @xsl.usage internal
|
|
133 |
*/
|
|
134 |
private Locale getLocale()
|
|
135 |
{
|
|
136 |
return m_locale;
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
|
140 |
* Creates a message from the specified key and replacement
|
|
141 |
* arguments, localized to the given locale.
|
|
142 |
*
|
|
143 |
* @param msgKey The key for the message text.
|
|
144 |
* @param args The arguments to be used as replacement text
|
|
145 |
* in the message created.
|
|
146 |
*
|
|
147 |
* @return The formatted message string.
|
|
148 |
* @xsl.usage internal
|
|
149 |
*/
|
|
150 |
public final String createMessage(String msgKey, Object args[])
|
|
151 |
{
|
|
152 |
if (m_resourceBundle == null)
|
16953
|
153 |
m_resourceBundle = SecuritySupport.getResourceBundle(m_resourceBundleName);
|
6
|
154 |
|
|
155 |
if (m_resourceBundle != null)
|
|
156 |
{
|
|
157 |
return createMsg(m_resourceBundle, msgKey, args);
|
|
158 |
}
|
|
159 |
else
|
|
160 |
return "Could not load the resource bundles: "+ m_resourceBundleName;
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Creates a message from the specified key and replacement
|
|
165 |
* arguments, localized to the given locale.
|
|
166 |
*
|
|
167 |
* @param errorCode The key for the message text.
|
|
168 |
*
|
|
169 |
* @param fResourceBundle The resource bundle to use.
|
|
170 |
* @param msgKey The message key to use.
|
|
171 |
* @param args The arguments to be used as replacement text
|
|
172 |
* in the message created.
|
|
173 |
*
|
|
174 |
* @return The formatted message string.
|
|
175 |
* @xsl.usage internal
|
|
176 |
*/
|
|
177 |
private final String createMsg(
|
|
178 |
ListResourceBundle fResourceBundle,
|
|
179 |
String msgKey,
|
|
180 |
Object args[]) //throws Exception
|
|
181 |
{
|
|
182 |
|
|
183 |
String fmsg = null;
|
|
184 |
boolean throwex = false;
|
|
185 |
String msg = null;
|
|
186 |
|
|
187 |
if (msgKey != null)
|
|
188 |
msg = fResourceBundle.getString(msgKey);
|
|
189 |
else
|
|
190 |
msgKey = "";
|
|
191 |
|
|
192 |
if (msg == null)
|
|
193 |
{
|
|
194 |
throwex = true;
|
|
195 |
/* The message is not in the bundle . . . this is bad,
|
|
196 |
* so try to get the message that the message is not in the bundle
|
|
197 |
*/
|
|
198 |
try
|
|
199 |
{
|
|
200 |
|
|
201 |
msg =
|
|
202 |
java.text.MessageFormat.format(
|
|
203 |
MsgKey.BAD_MSGKEY,
|
|
204 |
new Object[] { msgKey, m_resourceBundleName });
|
|
205 |
}
|
|
206 |
catch (Exception e)
|
|
207 |
{
|
|
208 |
/* even the message that the message is not in the bundle is
|
|
209 |
* not there ... this is really bad
|
|
210 |
*/
|
|
211 |
msg =
|
|
212 |
"The message key '"
|
|
213 |
+ msgKey
|
|
214 |
+ "' is not in the message class '"
|
|
215 |
+ m_resourceBundleName+"'";
|
|
216 |
}
|
|
217 |
}
|
|
218 |
else if (args != null)
|
|
219 |
{
|
|
220 |
try
|
|
221 |
{
|
|
222 |
// Do this to keep format from crying.
|
|
223 |
// This is better than making a bunch of conditional
|
|
224 |
// code all over the place.
|
|
225 |
int n = args.length;
|
|
226 |
|
|
227 |
for (int i = 0; i < n; i++)
|
|
228 |
{
|
|
229 |
if (null == args[i])
|
|
230 |
args[i] = "";
|
|
231 |
}
|
|
232 |
|
|
233 |
fmsg = java.text.MessageFormat.format(msg, args);
|
|
234 |
// if we get past the line above we have create the message ... hurray!
|
|
235 |
}
|
|
236 |
catch (Exception e)
|
|
237 |
{
|
|
238 |
throwex = true;
|
|
239 |
try
|
|
240 |
{
|
|
241 |
// Get the message that the format failed.
|
|
242 |
fmsg =
|
|
243 |
java.text.MessageFormat.format(
|
|
244 |
MsgKey.BAD_MSGFORMAT,
|
|
245 |
new Object[] { msgKey, m_resourceBundleName });
|
|
246 |
fmsg += " " + msg;
|
|
247 |
}
|
|
248 |
catch (Exception formatfailed)
|
|
249 |
{
|
|
250 |
// We couldn't even get the message that the format of
|
|
251 |
// the message failed ... so fall back to English.
|
|
252 |
fmsg =
|
|
253 |
"The format of message '"
|
|
254 |
+ msgKey
|
|
255 |
+ "' in message class '"
|
|
256 |
+ m_resourceBundleName
|
|
257 |
+ "' failed.";
|
|
258 |
}
|
|
259 |
}
|
|
260 |
}
|
|
261 |
else
|
|
262 |
fmsg = msg;
|
|
263 |
|
|
264 |
if (throwex)
|
|
265 |
{
|
|
266 |
throw new RuntimeException(fmsg);
|
|
267 |
}
|
|
268 |
|
|
269 |
return fmsg;
|
|
270 |
}
|
|
271 |
|
|
272 |
}
|