blob: 6c4d15a531410e0c61262d7d2775538300bd6fe4 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 */
21package com.sun.org.apache.xml.internal.security.utils;
22
23
24
25import java.text.MessageFormat;
26import java.util.Locale;
27import java.util.ResourceBundle;
28
29
30/**
31 * The Internationalization (I18N) pack.
32 *
33 *
34 *
35 * @author Christian Geuer-Pollmann
36 */
37public class I18n {
38
39 /** Field NOT_INITIALIZED_MSG */
40 public static final String NOT_INITIALIZED_MSG =
41 "You must initialize the xml-security library correctly before you use it. "
42 + "Call the static method \"com.sun.org.apache.xml.internal.security.Init.init();\" to do that "
43 + "before you use any functionality from that library.";
44
45 /** Field defaultLanguageCode */
46 private static String defaultLanguageCode; // will be set in static{} block
47
48 /** Field defaultCountryCode */
49 private static String defaultCountryCode; // will be set in static{} block
50
51 /** Field resourceBundle */
52 private static ResourceBundle resourceBundle =
53 ResourceBundle.getBundle
54 (Constants.exceptionMessagesResourceBundleBase, Locale.US);
55
56 /** Field alreadyInitialized */
57 private static boolean alreadyInitialized = false;
58
59 /** Field _languageCode */
60 private static String _languageCode = null;
61
62 /** Field _countryCode */
63 private static String _countryCode = null;
64
65 /**
66 * Constructor I18n
67 *
68 */
69 private I18n() {
70
71 // we don't allow instantiation
72 }
73
74 /**
75 * Method translate
76 *
77 * translates a message ID into an internationalized String, see alse
78 * <CODE>XMLSecurityException.getExceptionMEssage()</CODE>. The strings are
79 * stored in the <CODE>ResourceBundle</CODE>, which is identified in
80 * <CODE>exceptionMessagesResourceBundleBase</CODE>
81 *
82 * @param message
83 * @param args is an <CODE>Object[]</CODE> array of strings which are inserted into the String which is retrieved from the <CODE>ResouceBundle</CODE>
84 * @return message translated
85 */
86 public static String translate(String message, Object[] args) {
87 return getExceptionMessage(message, args);
88 }
89
90 /**
91 * Method translate
92 *
93 * translates a message ID into an internationalized String, see alse
94 * <CODE>XMLSecurityException.getExceptionMEssage()</CODE>
95 *
96 * @param message
97 * @return message translated
98 */
99 public static String translate(String message) {
100 return getExceptionMessage(message);
101 }
102
103 /**
104 * Method getExceptionMessage
105 *
106 * @param msgID
107 * @return message translated
108 *
109 */
110 public static String getExceptionMessage(String msgID) {
111
112 try {
113 String s = resourceBundle.getString(msgID);
114
115 return s;
116 } catch (Throwable t) {
117 if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) {
118 return "No message with ID \"" + msgID
119 + "\" found in resource bundle \""
120 + Constants.exceptionMessagesResourceBundleBase + "\"";
121 }
122 return I18n.NOT_INITIALIZED_MSG;
123 }
124 }
125
126 /**
127 * Method getExceptionMessage
128 *
129 * @param msgID
130 * @param originalException
131 * @return message translated
132 */
133 public static String getExceptionMessage(String msgID,
134 Exception originalException) {
135
136 try {
137 Object exArgs[] = { originalException.getMessage() };
138 String s = MessageFormat.format(resourceBundle.getString(msgID),
139 exArgs);
140
141 return s;
142 } catch (Throwable t) {
143 if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) {
144 return "No message with ID \"" + msgID
145 + "\" found in resource bundle \""
146 + Constants.exceptionMessagesResourceBundleBase
147 + "\". Original Exception was a "
148 + originalException.getClass().getName() + " and message "
149 + originalException.getMessage();
150 }
151 return I18n.NOT_INITIALIZED_MSG;
152 }
153 }
154
155 /**
156 * Method getExceptionMessage
157 *
158 * @param msgID
159 * @param exArgs
160 * @return message translated
161 */
162 public static String getExceptionMessage(String msgID, Object exArgs[]) {
163
164 try {
165 String s = MessageFormat.format(resourceBundle.getString(msgID),
166 exArgs);
167
168 return s;
169 } catch (Throwable t) {
170 if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) {
171 return "No message with ID \"" + msgID
172 + "\" found in resource bundle \""
173 + Constants.exceptionMessagesResourceBundleBase + "\"";
174 }
175 return I18n.NOT_INITIALIZED_MSG;
176 }
177 }
178
179//
180// Commented out because it modifies shared static
181// state which could be maliciously called by untrusted code
182//
183// /**
184// * Method init
185// *
186// * @param _defaultLanguageCode
187// * @param _defaultCountryCode
188// */
189// public static void init(String _defaultLanguageCode,
190// String _defaultCountryCode) {
191//
192// I18n.defaultLanguageCode = _defaultLanguageCode;
193//
194// if (I18n.defaultLanguageCode == null) {
195// I18n.defaultLanguageCode = Locale.getDefault().getLanguage();
196// }
197//
198// I18n.defaultCountryCode = _defaultCountryCode;
199//
200// if (I18n.defaultCountryCode == null) {
201// I18n.defaultCountryCode = Locale.getDefault().getCountry();
202// }
203//
204// initLocale(I18n.defaultLanguageCode, I18n.defaultCountryCode);
205// }
206
207//
208// Commented out because it modifies shared static
209// state which could be maliciously called by untrusted code
210//
211// /**
212// * Method initLocale
213// *
214// * @param languageCode
215// * @param countryCode
216// */
217// public static void initLocale(String languageCode, String countryCode) {
218//
219// if (alreadyInitialized && languageCode.equals(_languageCode)
220// && countryCode.equals(_countryCode)) {
221// return;
222// }
223//
224// if ((languageCode != null) && (countryCode != null)
225// && (languageCode.length() > 0) && (countryCode.length() > 0)) {
226// _languageCode = languageCode;
227// _countryCode = countryCode;
228// } else {
229// _countryCode = I18n.defaultCountryCode;
230// _languageCode = I18n.defaultLanguageCode;
231// }
232//
233// I18n.resourceBundle =
234// ResourceBundle.getBundle(Constants.exceptionMessagesResourceBundleBase,
235// new Locale(_languageCode, _countryCode));
236// }
237}