jaxp/src/com/sun/org/apache/xerces/internal/xpointer/XPointerMessageFormatter.java
author joehw
Mon, 18 Feb 2013 11:33:35 -0800
changeset 16953 a44e04deb948
parent 12457 c348e06f0e82
child 23955 67c850464591
permissions -rw-r--r--
6657673: Issues with JAXP Reviewed-by: alanb, lancea, ahgross, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 2005 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
package com.sun.org.apache.xerces.internal.xpointer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
import java.util.Locale;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
import java.util.MissingResourceException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import java.util.ResourceBundle;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.util.PropertyResourceBundle;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.xerces.internal.util.MessageFormatter;
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    27
import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * XPointerMessageFormatter provides error messages for the XPointer Framework
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * and element() Scheme Recommendations.
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * @xerces.internal
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 *
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    35
 * @version $Id: XPointerMessageFormatter.java,v 1.5 2010-11-01 04:40:26 joehw Exp $
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
class XPointerMessageFormatter implements MessageFormatter {
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
    public static final String XPOINTER_DOMAIN = "http://www.w3.org/TR/XPTR";
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
    // private objects to cache the locale and resource bundle
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
    private Locale fLocale = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    private ResourceBundle fResourceBundle = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
     * Formats a message with the specified arguments using the given locale
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
     * information.
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
     * @param locale
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
     *            The locale of the message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
     * @param key
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
     *            The message key.
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
     * @param arguments
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
     *            The message replacement text arguments. The order of the
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
     *            arguments must match that of the placeholders in the actual
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
     *            message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
     * @return Returns the formatted message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
     * @throws MissingResourceException
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
     *             Thrown if the message with the specified key cannot be found.
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    public String formatMessage(Locale locale, String key, Object[] arguments)
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
            throws MissingResourceException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
        if (fResourceBundle == null || locale != fLocale) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
            if (locale != null) {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    69
                fResourceBundle = SecuritySupport.getResourceBundle(
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
                        "com.sun.org.apache.xerces.internal.impl.msg.XPointerMessages", locale);
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
                // memorize the most-recent locale
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
                fLocale = locale;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
            if (fResourceBundle == null)
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    75
                fResourceBundle = SecuritySupport.getResourceBundle(
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    76
                        "com.sun.org.apache.xerces.internal.impl.msg.XPointerMessages");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
        String msg = fResourceBundle.getString(key);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        if (arguments != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
                msg = java.text.MessageFormat.format(msg, arguments);
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
            } catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
                msg = fResourceBundle.getString("FormatFailed");
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
                msg += " " + fResourceBundle.getString(key);
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        if (msg == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
            msg = fResourceBundle.getString("BadMessageKey");
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
            throw new MissingResourceException(msg,
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
                    "com.sun.org.apache.xerces.internal.impl.msg.XPointerMessages", key);
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        return msg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
}