KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > utils > I18n


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.utils;
18
19
20
21 import java.text.MessageFormat JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24
25
26 /**
27  * The Internationalization (I18N) pack.
28  *
29  *
30  *
31  * @author Christian Geuer-Pollmann
32  */

33 public class I18n {
34
35    /** Field NOT_INITIALIZED_MSG */
36    public static final String JavaDoc NOT_INITIALIZED_MSG =
37       "You must initialize the xml-security library correctly before you use it. "
38       + "Call the static method \"com.sun.org.apache.xml.internal.security.Init.init();\" to do that "
39       + "before you use any functionality from that library.";
40
41    /** Field defaultLanguageCode */
42    private static String JavaDoc defaultLanguageCode; // will be set in static{} block
43

44    /** Field defaultCountryCode */
45    private static String JavaDoc defaultCountryCode; // will be set in static{} block
46

47    /** Field resourceBundle */
48    private static ResourceBundle JavaDoc resourceBundle =
49       ResourceBundle.getBundle
50     (Constants.exceptionMessagesResourceBundleBase, Locale.US);
51
52    /** Field alreadyInitialized */
53    private static boolean alreadyInitialized = false;
54
55    /** Field _languageCode */
56    private static String JavaDoc _languageCode = null;
57
58    /** Field _countryCode */
59    private static String JavaDoc _countryCode = null;
60
61    /**
62     * Constructor I18n
63     *
64     */

65    private I18n() {
66
67       // we don't allow instantiation
68
}
69
70    /**
71     * Method translate
72     *
73     * translates a message ID into an internationalized String, see alse
74     * <CODE>XMLSecurityException.getExceptionMEssage()</CODE>. The strings are
75     * stored in the <CODE>ResourceBundle</CODE>, which is identified in
76     * <CODE>exceptionMessagesResourceBundleBase</CODE>
77     *
78     * @param message
79     * @param args is an <CODE>Object[]</CODE> array of strings which are inserted into the String which is retrieved from the <CODE>ResouceBundle</CODE>
80     * @return message translated
81     */

82    public static String JavaDoc translate(String JavaDoc message, Object JavaDoc[] args) {
83       return getExceptionMessage(message, args);
84    }
85
86    /**
87     * Method translate
88     *
89     * translates a message ID into an internationalized String, see alse
90     * <CODE>XMLSecurityException.getExceptionMEssage()</CODE>
91     *
92     * @param message
93     * @return message translated
94     */

95    public static String JavaDoc translate(String JavaDoc message) {
96       return getExceptionMessage(message);
97    }
98
99    /**
100     * Method getExceptionMessage
101     *
102     * @param msgID
103     * @return message translated
104     *
105     */

106    public static String JavaDoc getExceptionMessage(String JavaDoc msgID) {
107
108       try {
109          String JavaDoc s = resourceBundle.getString(msgID);
110
111          return s;
112       } catch (Throwable JavaDoc t) {
113          if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) {
114             return "No message with ID \"" + msgID
115                    + "\" found in resource bundle \""
116                    + Constants.exceptionMessagesResourceBundleBase + "\"";
117          }
118          return I18n.NOT_INITIALIZED_MSG;
119       }
120    }
121
122    /**
123     * Method getExceptionMessage
124     *
125     * @param msgID
126     * @param originalException
127     * @return message translated
128     */

129    public static String JavaDoc getExceptionMessage(String JavaDoc msgID,
130                                             Exception JavaDoc originalException) {
131
132       try {
133          Object JavaDoc exArgs[] = { originalException.getMessage() };
134          String JavaDoc s = MessageFormat.format(resourceBundle.getString(msgID),
135                                          exArgs);
136
137          return s;
138       } catch (Throwable JavaDoc t) {
139          if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) {
140             return "No message with ID \"" + msgID
141                    + "\" found in resource bundle \""
142                    + Constants.exceptionMessagesResourceBundleBase
143                    + "\". Original Exception was a "
144                    + originalException.getClass().getName() + " and message "
145                    + originalException.getMessage();
146          }
147           return I18n.NOT_INITIALIZED_MSG;
148       }
149    }
150
151    /**
152     * Method getExceptionMessage
153     *
154     * @param msgID
155     * @param exArgs
156     * @return message translated
157     */

158    public static String JavaDoc getExceptionMessage(String JavaDoc msgID, Object JavaDoc exArgs[]) {
159
160       try {
161          String JavaDoc s = MessageFormat.format(resourceBundle.getString(msgID),
162                                          exArgs);
163
164          return s;
165       } catch (Throwable JavaDoc t) {
166          if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) {
167             return "No message with ID \"" + msgID
168                    + "\" found in resource bundle \""
169                    + Constants.exceptionMessagesResourceBundleBase + "\"";
170          }
171          return I18n.NOT_INITIALIZED_MSG;
172       }
173    }
174
175 //
176
// Commented out because it modifies shared static
177
// state which could be maliciously called by untrusted code
178
//
179
// /**
180
// * Method init
181
// *
182
// * @param _defaultLanguageCode
183
// * @param _defaultCountryCode
184
// */
185
// public static void init(String _defaultLanguageCode,
186
// String _defaultCountryCode) {
187
//
188
// I18n.defaultLanguageCode = _defaultLanguageCode;
189
//
190
// if (I18n.defaultLanguageCode == null) {
191
// I18n.defaultLanguageCode = Locale.getDefault().getLanguage();
192
// }
193
//
194
// I18n.defaultCountryCode = _defaultCountryCode;
195
//
196
// if (I18n.defaultCountryCode == null) {
197
// I18n.defaultCountryCode = Locale.getDefault().getCountry();
198
// }
199
//
200
// initLocale(I18n.defaultLanguageCode, I18n.defaultCountryCode);
201
// }
202

203 //
204
// Commented out because it modifies shared static
205
// state which could be maliciously called by untrusted code
206
//
207
// /**
208
// * Method initLocale
209
// *
210
// * @param languageCode
211
// * @param countryCode
212
// */
213
// public static void initLocale(String languageCode, String countryCode) {
214
//
215
// if (alreadyInitialized && languageCode.equals(_languageCode)
216
// && countryCode.equals(_countryCode)) {
217
// return;
218
// }
219
//
220
// if ((languageCode != null) && (countryCode != null)
221
// && (languageCode.length() > 0) && (countryCode.length() > 0)) {
222
// _languageCode = languageCode;
223
// _countryCode = countryCode;
224
// } else {
225
// _countryCode = I18n.defaultCountryCode;
226
// _languageCode = I18n.defaultLanguageCode;
227
// }
228
//
229
// I18n.resourceBundle =
230
// ResourceBundle.getBundle(Constants.exceptionMessagesResourceBundleBase,
231
// new Locale(_languageCode, _countryCode));
232
// }
233
}
234
Popular Tags