6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-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: XMLMessages.java,v 1.2.4.1 2005/09/15 07:45:48 suresh_emailid Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xml.internal.res;
|
|
24 |
|
16953
|
25 |
import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
|
6
|
26 |
import java.util.ListResourceBundle;
|
|
27 |
import java.util.Locale;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* A utility class for issuing XML error messages.
|
|
31 |
* @xsl.usage internal
|
|
32 |
*/
|
|
33 |
public class XMLMessages
|
|
34 |
{
|
|
35 |
|
|
36 |
/** The local object to use. */
|
|
37 |
protected Locale fLocale = Locale.getDefault();
|
|
38 |
|
|
39 |
/** The language specific resource object for XML messages. */
|
|
40 |
private static ListResourceBundle XMLBundle = null;
|
|
41 |
|
|
42 |
/** The class name of the XML error message string table. */
|
|
43 |
private static final String XML_ERROR_RESOURCES =
|
|
44 |
"com.sun.org.apache.xml.internal.res.XMLErrorResources";
|
|
45 |
|
|
46 |
/** String to use if a bad message code is used. */
|
|
47 |
protected static final String BAD_CODE = "BAD_CODE";
|
|
48 |
|
|
49 |
/** String to use if the message format operation failed. */
|
|
50 |
protected static final String FORMAT_FAILED = "FORMAT_FAILED";
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Set the Locale object to use.
|
|
54 |
*
|
|
55 |
* @param locale non-null reference to Locale object.
|
|
56 |
*/
|
|
57 |
public void setLocale(Locale locale)
|
|
58 |
{
|
|
59 |
fLocale = locale;
|
|
60 |
}
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Get the Locale object that is being used.
|
|
64 |
*
|
|
65 |
* @return non-null reference to Locale object.
|
|
66 |
*/
|
|
67 |
public Locale getLocale()
|
|
68 |
{
|
|
69 |
return fLocale;
|
|
70 |
}
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Creates a message from the specified key and replacement
|
|
74 |
* arguments, localized to the given locale.
|
|
75 |
*
|
|
76 |
* @param msgKey The key for the message text.
|
|
77 |
* @param args The arguments to be used as replacement text
|
|
78 |
* in the message created.
|
|
79 |
*
|
|
80 |
* @return The formatted message string.
|
|
81 |
*/
|
|
82 |
public static final String createXMLMessage(String msgKey, Object args[])
|
|
83 |
{
|
16953
|
84 |
if (XMLBundle == null) {
|
|
85 |
XMLBundle = SecuritySupport.getResourceBundle(XML_ERROR_RESOURCES);
|
|
86 |
}
|
6
|
87 |
|
|
88 |
if (XMLBundle != null)
|
|
89 |
{
|
|
90 |
return createMsg(XMLBundle, msgKey, args);
|
|
91 |
}
|
|
92 |
else
|
|
93 |
return "Could not load any resource bundles.";
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Creates a message from the specified key and replacement
|
|
98 |
* arguments, localized to the given locale.
|
|
99 |
*
|
|
100 |
* @param fResourceBundle The resource bundle to use.
|
|
101 |
* @param msgKey The message key to use.
|
|
102 |
* @param args The arguments to be used as replacement text
|
|
103 |
* in the message created.
|
|
104 |
*
|
|
105 |
* @return The formatted message string.
|
|
106 |
*/
|
|
107 |
public static final String createMsg(ListResourceBundle fResourceBundle,
|
|
108 |
String msgKey, Object args[]) //throws Exception
|
|
109 |
{
|
|
110 |
|
|
111 |
String fmsg = null;
|
|
112 |
boolean throwex = false;
|
|
113 |
String msg = null;
|
|
114 |
|
|
115 |
if (msgKey != null)
|
|
116 |
msg = fResourceBundle.getString(msgKey);
|
|
117 |
|
|
118 |
if (msg == null)
|
|
119 |
{
|
|
120 |
msg = fResourceBundle.getString(BAD_CODE);
|
|
121 |
throwex = true;
|
|
122 |
}
|
|
123 |
|
|
124 |
if (args != null)
|
|
125 |
{
|
|
126 |
try
|
|
127 |
{
|
|
128 |
|
|
129 |
// Do this to keep format from crying.
|
|
130 |
// This is better than making a bunch of conditional
|
|
131 |
// code all over the place.
|
|
132 |
int n = args.length;
|
|
133 |
|
|
134 |
for (int i = 0; i < n; i++)
|
|
135 |
{
|
|
136 |
if (null == args[i])
|
|
137 |
args[i] = "";
|
|
138 |
}
|
|
139 |
|
|
140 |
fmsg = java.text.MessageFormat.format(msg, args);
|
|
141 |
}
|
|
142 |
catch (Exception e)
|
|
143 |
{
|
|
144 |
fmsg = fResourceBundle.getString(FORMAT_FAILED);
|
|
145 |
fmsg += " " + msg;
|
|
146 |
}
|
|
147 |
}
|
|
148 |
else
|
|
149 |
fmsg = msg;
|
|
150 |
|
|
151 |
if (throwex)
|
|
152 |
{
|
|
153 |
throw new RuntimeException(fmsg);
|
|
154 |
}
|
|
155 |
|
|
156 |
return fmsg;
|
|
157 |
}
|
|
158 |
|
|
159 |
}
|