1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xerces.internal.impl.msg;
23
24import java.util.Locale;
25import java.util.MissingResourceException;
26import java.util.ResourceBundle;
27import java.util.PropertyResourceBundle;
28
29import com.sun.org.apache.xerces.internal.util.MessageFormatter;
30import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
31
32/**
33 * XMLMessageFormatter provides error messages for the XML 1.0 Recommendation and for
34 * the Namespaces Recommendation
35 *
36 * @xerces.internal
37 *
38 * @author Eric Ye, IBM
39 *
40 */
41public class XMLMessageFormatter_fr implements MessageFormatter {
42    /**
43     * The domain of messages concerning the XML 1.0 specification.
44     */
45    public static final String XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210";
46    public static final String XMLNS_DOMAIN = "http://www.w3.org/TR/1999/REC-xml-names-19990114";
47
48    // private objects to cache the locale and resource bundle
49    private Locale fLocale = null;
50    private ResourceBundle fResourceBundle = null;
51
52    //
53    // MessageFormatter methods
54    //
55
56    /**
57     * Formats a message with the specified arguments using the given
58     * locale information.
59     *
60     * @param locale    The locale of the message.
61     * @param key       The message key.
62     * @param arguments The message replacement text arguments. The order
63     *                  of the arguments must match that of the placeholders
64     *                  in the actual message.
65     *
66     * @return Returns the formatted message.
67     *
68     * @throws MissingResourceException Thrown if the message with the
69     *                                  specified key cannot be found.
70     */
71    public String formatMessage(Locale locale, String key, Object[] arguments)
72        throws MissingResourceException {
73
74        if (fResourceBundle == null || locale != fLocale) {
75            if (locale != null) {
76                fResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages", locale);
77                // memorize the most-recent locale
78                fLocale = locale;
79            }
80            if (fResourceBundle == null)
81                fResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages");
82        }
83
84        // format message
85        String msg;
86        try {
87            msg = fResourceBundle.getString(key);
88            if (arguments != null) {
89                try {
90                    msg = java.text.MessageFormat.format(msg, arguments);
91                }
92                catch (Exception e) {
93                    msg = fResourceBundle.getString("FormatFailed");
94                    msg += " " + fResourceBundle.getString(key);
95                }
96            }
97        }
98
99        // error
100        catch (MissingResourceException e) {
101            msg = fResourceBundle.getString("BadMessageKey");
102            throw new MissingResourceException(key, msg, key);
103        }
104
105        // no message
106        if (msg == null) {
107            msg = key;
108            if (arguments.length > 0) {
109                StringBuffer str = new StringBuffer(msg);
110                str.append('?');
111                for (int i = 0; i < arguments.length; i++) {
112                    if (i > 0) {
113                        str.append('&');
114                    }
115                    str.append(String.valueOf(arguments[i]));
116                }
117            }
118        }
119
120        return msg;
121    }
122
123}
124