1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /**
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  */
23 package com.sun.org.apache.xml.internal.security.utils;
24 
25 import java.text.MessageFormat;
26 import java.util.Locale;
27 import java.util.ResourceBundle;
28 
29 /**
30  * The Internationalization (I18N) pack.
31  *
32  */
33 public final class I18n {
34 
35     /** Field NOT_INITIALIZED_MSG */
36     public static final String 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 resourceBundle */
42     private static ResourceBundle resourceBundle;
43 
44     /** Field alreadyInitialized */
45     private static boolean alreadyInitialized = false;
46 
47     /**
48      * Constructor I18n
49      *
50      */
I18n()51     private I18n() {
52         // we don't allow instantiation
53     }
54 
55     /**
56      * Method translate
57      *
58      * translates a message ID into an internationalized String, see alse
59      * {@code XMLSecurityException.getExceptionMEssage()}. The strings are
60      * stored in the {@code ResourceBundle}, which is identified in
61      * {@code exceptionMessagesResourceBundleBase}
62      *
63      * @param message
64      * @param args is an {@code Object[]} array of strings which are inserted into
65      * the String which is retrieved from the {@code ResouceBundle}
66      * @return message translated
67      */
translate(String message, Object[] args)68     public static String translate(String message, Object[] args) {
69         return getExceptionMessage(message, args);
70     }
71 
72     /**
73      * Method translate
74      *
75      * translates a message ID into an internationalized String, see also
76      * {@code XMLSecurityException.getExceptionMessage()}
77      *
78      * @param message
79      * @return message translated
80      */
translate(String message)81     public static String translate(String message) {
82         return getExceptionMessage(message);
83     }
84 
85     /**
86      * Method getExceptionMessage
87      *
88      * @param msgID
89      * @return message translated
90      *
91      */
getExceptionMessage(String msgID)92     public static String getExceptionMessage(String msgID) {
93         try {
94             return resourceBundle.getString(msgID);
95         } catch (Throwable t) {
96             if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) {
97                 return "No message with ID \"" + msgID
98                 + "\" found in resource bundle \""
99                 + Constants.exceptionMessagesResourceBundleBase + "\"";
100             }
101             return I18n.NOT_INITIALIZED_MSG;
102         }
103     }
104 
105     /**
106      * Method getExceptionMessage
107      *
108      * @param msgID
109      * @param originalException
110      * @return message translated
111      */
getExceptionMessage(String msgID, Exception originalException)112     public static String getExceptionMessage(String msgID, Exception originalException) {
113         try {
114             Object[] exArgs = { originalException.getMessage() };
115             return MessageFormat.format(resourceBundle.getString(msgID), exArgs);
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                 + "\". Original Exception was a "
122                 + originalException.getClass().getName() + " and message "
123                 + originalException.getMessage();
124             }
125             return I18n.NOT_INITIALIZED_MSG;
126         }
127     }
128 
129     /**
130      * Method getExceptionMessage
131      *
132      * @param msgID
133      * @param exArgs
134      * @return message translated
135      */
getExceptionMessage(String msgID, Object[] exArgs)136     public static String getExceptionMessage(String msgID, Object[] exArgs) {
137         try {
138             return MessageFormat.format(resourceBundle.getString(msgID), exArgs);
139         } catch (Throwable t) {
140             if (com.sun.org.apache.xml.internal.security.Init.isInitialized()) {
141                 return "No message with ID \"" + msgID
142                 + "\" found in resource bundle \""
143                 + Constants.exceptionMessagesResourceBundleBase + "\"";
144             }
145             return I18n.NOT_INITIALIZED_MSG;
146         }
147     }
148 
149     /**
150      * Method init
151      *
152      * @param languageCode
153      * @param countryCode
154      */
init(String languageCode, String countryCode)155     public static synchronized void init(String languageCode, String countryCode) {
156         if (alreadyInitialized) {
157             return;
158         }
159 
160         I18n.resourceBundle =
161             ResourceBundle.getBundle(
162                 Constants.exceptionMessagesResourceBundleBase,
163                 new Locale(languageCode, countryCode)
164             );
165         alreadyInitialized = true;
166     }
167 
168     /**
169      * Method init
170      * @param resourceBundle
171      */
init(ResourceBundle resourceBundle)172     public static synchronized void init(ResourceBundle resourceBundle) {
173         if (alreadyInitialized) {
174             return;
175         }
176 
177         I18n.resourceBundle = resourceBundle;
178         alreadyInitialized = true;
179     }
180 }
181