1 /* |
1 /* |
2 * reserved comment block |
2 * reserved comment block |
3 * DO NOT REMOVE OR ALTER! |
3 * DO NOT REMOVE OR ALTER! |
4 */ |
4 */ |
5 /* |
5 /** |
6 * Copyright 1999-2004 The Apache Software Foundation. |
6 * Licensed to the Apache Software Foundation (ASF) under one |
|
7 * or more contributor license agreements. See the NOTICE file |
|
8 * distributed with this work for additional information |
|
9 * regarding copyright ownership. The ASF licenses this file |
|
10 * to you under the Apache License, Version 2.0 (the |
|
11 * "License"); you may not use this file except in compliance |
|
12 * with the License. You may obtain a copy of the License at |
7 * |
13 * |
8 * Licensed under the Apache License, Version 2.0 (the "License"); |
14 * http://www.apache.org/licenses/LICENSE-2.0 |
9 * you may not use this file except in compliance with the License. |
|
10 * You may obtain a copy of the License at |
|
11 * |
15 * |
12 * http://www.apache.org/licenses/LICENSE-2.0 |
16 * Unless required by applicable law or agreed to in writing, |
13 * |
17 * software distributed under the License is distributed on an |
14 * Unless required by applicable law or agreed to in writing, software |
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 * distributed under the License is distributed on an "AS IS" BASIS, |
19 * KIND, either express or implied. See the License for the |
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 * specific language governing permissions and limitations |
17 * See the License for the specific language governing permissions and |
21 * under the License. |
18 * limitations under the License. |
|
19 * |
|
20 */ |
22 */ |
21 package com.sun.org.apache.xml.internal.security.utils; |
23 package com.sun.org.apache.xml.internal.security.utils; |
22 |
24 |
23 import java.text.MessageFormat; |
25 import java.text.MessageFormat; |
24 import java.util.Locale; |
26 import java.util.Locale; |
29 * |
31 * |
30 * @author Christian Geuer-Pollmann |
32 * @author Christian Geuer-Pollmann |
31 */ |
33 */ |
32 public class I18n { |
34 public class I18n { |
33 |
35 |
34 /** Field NOT_INITIALIZED_MSG */ |
36 /** Field NOT_INITIALIZED_MSG */ |
35 public static final String NOT_INITIALIZED_MSG = |
37 public static final String NOT_INITIALIZED_MSG = |
36 "You must initialize the xml-security library correctly before you use it. " |
38 "You must initialize the xml-security library correctly before you use it. " |
37 + "Call the static method \"com.sun.org.apache.xml.internal.security.Init.init();\" to do that " |
39 + "Call the static method \"com.sun.org.apache.xml.internal.security.Init.init();\" to do that " |
38 + "before you use any functionality from that library."; |
40 + "before you use any functionality from that library."; |
39 |
41 |
40 /** Field defaultLanguageCode */ |
42 /** Field resourceBundle */ |
41 private static String defaultLanguageCode; // will be set in static{} block |
43 private static ResourceBundle resourceBundle; |
42 |
44 |
43 /** Field defaultCountryCode */ |
45 /** Field alreadyInitialized */ |
44 private static String defaultCountryCode; // will be set in static{} block |
46 private static boolean alreadyInitialized = false; |
45 |
47 |
46 /** Field resourceBundle */ |
48 /** |
47 private static ResourceBundle resourceBundle = |
49 * Constructor I18n |
48 ResourceBundle.getBundle |
50 * |
49 (Constants.exceptionMessagesResourceBundleBase, Locale.US); |
51 */ |
|
52 private I18n() { |
|
53 // we don't allow instantiation |
|
54 } |
50 |
55 |
51 /** Field alreadyInitialized */ |
56 /** |
52 private static boolean alreadyInitialized = false; |
57 * Method translate |
|
58 * |
|
59 * translates a message ID into an internationalized String, see alse |
|
60 * <CODE>XMLSecurityException.getExceptionMEssage()</CODE>. The strings are |
|
61 * stored in the <CODE>ResourceBundle</CODE>, which is identified in |
|
62 * <CODE>exceptionMessagesResourceBundleBase</CODE> |
|
63 * |
|
64 * @param message |
|
65 * @param args is an <CODE>Object[]</CODE> array of strings which are inserted into |
|
66 * the String which is retrieved from the <CODE>ResouceBundle</CODE> |
|
67 * @return message translated |
|
68 */ |
|
69 public static String translate(String message, Object[] args) { |
|
70 return getExceptionMessage(message, args); |
|
71 } |
53 |
72 |
54 /** Field _languageCode */ |
73 /** |
55 private static String _languageCode = null; |
74 * Method translate |
|
75 * |
|
76 * translates a message ID into an internationalized String, see also |
|
77 * <CODE>XMLSecurityException.getExceptionMessage()</CODE> |
|
78 * |
|
79 * @param message |
|
80 * @return message translated |
|
81 */ |
|
82 public static String translate(String message) { |
|
83 return getExceptionMessage(message); |
|
84 } |
56 |
85 |
57 /** Field _countryCode */ |
86 /** |
58 private static String _countryCode = null; |
87 * Method getExceptionMessage |
|
88 * |
|
89 * @param msgID |
|
90 * @return message translated |
|
91 * |
|
92 */ |
|
93 public static String getExceptionMessage(String msgID) { |
|
94 try { |
|
95 return resourceBundle.getString(msgID); |
|
96 } catch (Throwable t) { |
|
97 if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) { |
|
98 return "No message with ID \"" + msgID |
|
99 + "\" found in resource bundle \"" |
|
100 + Constants.exceptionMessagesResourceBundleBase + "\""; |
|
101 } |
|
102 return I18n.NOT_INITIALIZED_MSG; |
|
103 } |
|
104 } |
59 |
105 |
60 /** |
106 /** |
61 * Constructor I18n |
107 * Method getExceptionMessage |
62 * |
108 * |
63 */ |
109 * @param msgID |
64 private I18n() { |
110 * @param originalException |
|
111 * @return message translated |
|
112 */ |
|
113 public static String getExceptionMessage(String msgID, Exception originalException) { |
|
114 try { |
|
115 Object exArgs[] = { originalException.getMessage() }; |
|
116 return MessageFormat.format(resourceBundle.getString(msgID), exArgs); |
|
117 } catch (Throwable t) { |
|
118 if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) { |
|
119 return "No message with ID \"" + msgID |
|
120 + "\" found in resource bundle \"" |
|
121 + Constants.exceptionMessagesResourceBundleBase |
|
122 + "\". Original Exception was a " |
|
123 + originalException.getClass().getName() + " and message " |
|
124 + originalException.getMessage(); |
|
125 } |
|
126 return I18n.NOT_INITIALIZED_MSG; |
|
127 } |
|
128 } |
65 |
129 |
66 // we don't allow instantiation |
130 /** |
67 } |
131 * Method getExceptionMessage |
|
132 * |
|
133 * @param msgID |
|
134 * @param exArgs |
|
135 * @return message translated |
|
136 */ |
|
137 public static String getExceptionMessage(String msgID, Object exArgs[]) { |
|
138 try { |
|
139 return MessageFormat.format(resourceBundle.getString(msgID), exArgs); |
|
140 } catch (Throwable t) { |
|
141 if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) { |
|
142 return "No message with ID \"" + msgID |
|
143 + "\" found in resource bundle \"" |
|
144 + Constants.exceptionMessagesResourceBundleBase + "\""; |
|
145 } |
|
146 return I18n.NOT_INITIALIZED_MSG; |
|
147 } |
|
148 } |
68 |
149 |
69 /** |
150 /** |
70 * Method translate |
151 * Method init |
71 * |
152 * |
72 * translates a message ID into an internationalized String, see alse |
153 * @param languageCode |
73 * <CODE>XMLSecurityException.getExceptionMEssage()</CODE>. The strings are |
154 * @param countryCode |
74 * stored in the <CODE>ResourceBundle</CODE>, which is identified in |
155 */ |
75 * <CODE>exceptionMessagesResourceBundleBase</CODE> |
156 public synchronized static void init(String languageCode, String countryCode) { |
76 * |
157 if (alreadyInitialized) { |
77 * @param message |
158 return; |
78 * @param args is an <CODE>Object[]</CODE> array of strings which are inserted into the String which is retrieved from the <CODE>ResouceBundle</CODE> |
159 } |
79 * @return message translated |
|
80 */ |
|
81 public static String translate(String message, Object[] args) { |
|
82 return getExceptionMessage(message, args); |
|
83 } |
|
84 |
160 |
85 /** |
161 I18n.resourceBundle = |
86 * Method translate |
162 ResourceBundle.getBundle( |
87 * |
163 Constants.exceptionMessagesResourceBundleBase, |
88 * translates a message ID into an internationalized String, see alse |
164 new Locale(languageCode, countryCode) |
89 * <CODE>XMLSecurityException.getExceptionMEssage()</CODE> |
165 ); |
90 * |
166 alreadyInitialized = true; |
91 * @param message |
167 } |
92 * @return message translated |
|
93 */ |
|
94 public static String translate(String message) { |
|
95 return getExceptionMessage(message); |
|
96 } |
|
97 |
|
98 /** |
|
99 * Method getExceptionMessage |
|
100 * |
|
101 * @param msgID |
|
102 * @return message translated |
|
103 * |
|
104 */ |
|
105 public static String getExceptionMessage(String msgID) { |
|
106 |
|
107 try { |
|
108 String s = resourceBundle.getString(msgID); |
|
109 |
|
110 return s; |
|
111 } catch (Throwable t) { |
|
112 if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) { |
|
113 return "No message with ID \"" + msgID |
|
114 + "\" found in resource bundle \"" |
|
115 + Constants.exceptionMessagesResourceBundleBase + "\""; |
|
116 } |
|
117 return I18n.NOT_INITIALIZED_MSG; |
|
118 } |
|
119 } |
|
120 |
|
121 /** |
|
122 * Method getExceptionMessage |
|
123 * |
|
124 * @param msgID |
|
125 * @param originalException |
|
126 * @return message translated |
|
127 */ |
|
128 public static String getExceptionMessage(String msgID, |
|
129 Exception originalException) { |
|
130 |
|
131 try { |
|
132 Object exArgs[] = { originalException.getMessage() }; |
|
133 String s = MessageFormat.format(resourceBundle.getString(msgID), |
|
134 exArgs); |
|
135 |
|
136 return s; |
|
137 } catch (Throwable t) { |
|
138 if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) { |
|
139 return "No message with ID \"" + msgID |
|
140 + "\" found in resource bundle \"" |
|
141 + Constants.exceptionMessagesResourceBundleBase |
|
142 + "\". Original Exception was a " |
|
143 + originalException.getClass().getName() + " and message " |
|
144 + originalException.getMessage(); |
|
145 } |
|
146 return I18n.NOT_INITIALIZED_MSG; |
|
147 } |
|
148 } |
|
149 |
|
150 /** |
|
151 * Method getExceptionMessage |
|
152 * |
|
153 * @param msgID |
|
154 * @param exArgs |
|
155 * @return message translated |
|
156 */ |
|
157 public static String getExceptionMessage(String msgID, Object exArgs[]) { |
|
158 |
|
159 try { |
|
160 String s = MessageFormat.format(resourceBundle.getString(msgID), |
|
161 exArgs); |
|
162 |
|
163 return s; |
|
164 } catch (Throwable t) { |
|
165 if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) { |
|
166 return "No message with ID \"" + msgID |
|
167 + "\" found in resource bundle \"" |
|
168 + Constants.exceptionMessagesResourceBundleBase + "\""; |
|
169 } |
|
170 return I18n.NOT_INITIALIZED_MSG; |
|
171 } |
|
172 } |
|
173 |
|
174 // |
|
175 // Commented out because it modifies shared static |
|
176 // state which could be maliciously called by untrusted code |
|
177 // |
|
178 // /** |
|
179 // * Method init |
|
180 // * |
|
181 // * @param _defaultLanguageCode |
|
182 // * @param _defaultCountryCode |
|
183 // */ |
|
184 // public static void init(String _defaultLanguageCode, |
|
185 // String _defaultCountryCode) { |
|
186 // |
|
187 // I18n.defaultLanguageCode = _defaultLanguageCode; |
|
188 // |
|
189 // if (I18n.defaultLanguageCode == null) { |
|
190 // I18n.defaultLanguageCode = Locale.getDefault().getLanguage(); |
|
191 // } |
|
192 // |
|
193 // I18n.defaultCountryCode = _defaultCountryCode; |
|
194 // |
|
195 // if (I18n.defaultCountryCode == null) { |
|
196 // I18n.defaultCountryCode = Locale.getDefault().getCountry(); |
|
197 // } |
|
198 // |
|
199 // initLocale(I18n.defaultLanguageCode, I18n.defaultCountryCode); |
|
200 // } |
|
201 |
|
202 // |
|
203 // Commented out because it modifies shared static |
|
204 // state which could be maliciously called by untrusted code |
|
205 // |
|
206 // /** |
|
207 // * Method initLocale |
|
208 // * |
|
209 // * @param languageCode |
|
210 // * @param countryCode |
|
211 // */ |
|
212 // public static void initLocale(String languageCode, String countryCode) { |
|
213 // |
|
214 // if (alreadyInitialized && languageCode.equals(_languageCode) |
|
215 // && countryCode.equals(_countryCode)) { |
|
216 // return; |
|
217 // } |
|
218 // |
|
219 // if ((languageCode != null) && (countryCode != null) |
|
220 // && (languageCode.length() > 0) && (countryCode.length() > 0)) { |
|
221 // _languageCode = languageCode; |
|
222 // _countryCode = countryCode; |
|
223 // } else { |
|
224 // _countryCode = I18n.defaultCountryCode; |
|
225 // _languageCode = I18n.defaultLanguageCode; |
|
226 // } |
|
227 // |
|
228 // I18n.resourceBundle = |
|
229 // ResourceBundle.getBundle(Constants.exceptionMessagesResourceBundleBase, |
|
230 // new Locale(_languageCode, _countryCode)); |
|
231 // } |
|
232 } |
168 } |