src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/DOMMessageFormatter.java
author joehw
Wed, 04 Oct 2017 10:54:18 -0700
changeset 47312 d4f959806fe9
parent 47216 71c04702a3d5
child 48409 5ab69533994b
permissions -rw-r--r--
8187593: Cleanup: removing SecuritySupport files Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
     3
 * @LastModified: Sep 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xerces.internal.dom;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import java.util.Locale;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.util.MissingResourceException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.util.ResourceBundle;
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    27
import jdk.xml.internal.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
 * Used to format DOM error messages, using the system locale.
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * @xerces.internal
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * @author Sandy Gao, IBM
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
public class DOMMessageFormatter {
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
    public static final String DOM_DOMAIN = "http://www.w3.org/dom/DOMTR";
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
    public static final String XML_DOMAIN = "http://www.w3.org/TR/1998/REC-xml-19980210";
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
    public static final String SERIALIZER_DOMAIN = "http://apache.org/xml/serializer";
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
    private static ResourceBundle domResourceBundle = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
    private static ResourceBundle xmlResourceBundle = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
    private static ResourceBundle serResourceBundle = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    private static Locale locale = 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
    DOMMessageFormatter(){
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
        locale = Locale.getDefault();
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
     * Formats a message with the specified arguments using the given
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
     * locale information.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
     * @param domain    domain from which error string is to come.
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
     * @param key       The message key.
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
     * @param arguments The message replacement text arguments. The order
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
     *                  of the arguments must match that of the placeholders
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
     *                  in the actual message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
     * @return          the formatted message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
     * @throws MissingResourceException Thrown if the message with the
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
     *                                  specified key cannot be found.
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    public static String formatMessage(String domain,
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    String key, Object[] arguments)
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    throws MissingResourceException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
        ResourceBundle resourceBundle = getResourceBundle(domain);
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
        if(resourceBundle == null){
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
            init();
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
            resourceBundle = getResourceBundle(domain);
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
            if(resourceBundle == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
                throw new MissingResourceException("Unknown domain" + domain, null, key);
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        // format message
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
        String msg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
            msg = key + ": " + resourceBundle.getString(key);
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
            if (arguments != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
                    msg = java.text.MessageFormat.format(msg, arguments);
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
                catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
                    msg = resourceBundle.getString("FormatFailed");
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
                    msg += " " + resourceBundle.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
        } // error
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        catch (MissingResourceException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
            msg = resourceBundle.getString("BadMessageKey");
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
            throw new MissingResourceException(key, msg, key);
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        // no message
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        if (msg == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
            msg = key;
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            if (arguments.length > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
                StringBuffer str = new StringBuffer(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
                str.append('?');
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
                for (int i = 0; i < arguments.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
                    if (i > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
                        str.append('&');
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
                    str.append(String.valueOf(arguments[i]));
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
        return msg;
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    static ResourceBundle getResourceBundle(String domain){
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        if(domain == DOM_DOMAIN || domain.equals(DOM_DOMAIN))
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
            return domResourceBundle;
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        else if( domain == XML_DOMAIN || domain.equals(XML_DOMAIN))
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
            return xmlResourceBundle;
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        else if(domain == SERIALIZER_DOMAIN || domain.equals(SERIALIZER_DOMAIN))
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
            return serResourceBundle;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     * Initialize Message Formatter.
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    public static void init(){
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        if (locale != null) {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
   126
            domResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.DOMMessages", locale);
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
   127
            serResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSerializerMessages", locale);
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
   128
            xmlResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages", locale);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        }else{
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
   130
            domResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.DOMMessages");
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
   131
            serResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSerializerMessages");
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
   132
            xmlResourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
     * setLocale to be used by the formatter.
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
     * @param locale
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    public static void setLocale(Locale dlocale){
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        locale = dlocale;
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
}