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 */
23package com.sun.org.apache.xml.internal.security.utils;
24
25import java.text.MessageFormat;
26import java.util.Locale;
27import java.util.ResourceBundle;
28
29/**
30 * The Internationalization (I18N) pack.
31 *
32 * @author Christian Geuer-Pollmann
33 */
34public class I18n {
35
36    /** Field NOT_INITIALIZED_MSG */
37    public static final String NOT_INITIALIZED_MSG =
38        "You must initialize the xml-security library correctly before you use it. "
39        + "Call the static method \"com.sun.org.apache.xml.internal.security.Init.init();\" to do that "
40        + "before you use any functionality from that library.";
41
42    /** Field resourceBundle */
43    private static ResourceBundle resourceBundle;
44
45    /** Field alreadyInitialized */
46    private static boolean alreadyInitialized = false;
47
48    /**
49     * Constructor I18n
50     *
51     */
52    private I18n() {
53        // we don't allow instantiation
54    }
55
56    /**
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    }
72
73    /**
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    }
85
86    /**
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    }
105
106    /**
107     * Method getExceptionMessage
108     *
109     * @param msgID
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    }
129
130    /**
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    }
149
150    /**
151     * Method init
152     *
153     * @param languageCode
154     * @param countryCode
155     */
156    public synchronized static void init(String languageCode, String countryCode) {
157        if (alreadyInitialized) {
158            return;
159        }
160
161        I18n.resourceBundle =
162            ResourceBundle.getBundle(
163                Constants.exceptionMessagesResourceBundleBase,
164                new Locale(languageCode, countryCode)
165            );
166        alreadyInitialized = true;
167    }
168}
169