src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/validation/JAXPValidationMessageFormatter.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
package com.sun.org.apache.xerces.internal.jaxp.validation;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
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
 * <p>Used to format JAXP Validation API error messages using a specified locale.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * @author Michael Glavassevich, IBM
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
final class JAXPValidationMessageFormatter {
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
     * Formats a message with the specified arguments using the given
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
     * locale information.
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
     * @param locale    The locale of the message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
     * @param key       The message key.
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
     * @param arguments The message replacement text arguments. The order
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
     *                  of the arguments must match that of the placeholders
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
     *                  in the actual message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
     * @return          the formatted message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
     * @throws MissingResourceException Thrown if the message with the
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     *                                  specified key cannot be found.
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    public static String formatMessage(Locale locale,
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
        String key, Object[] arguments)
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
        throws MissingResourceException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
        ResourceBundle resourceBundle = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
        if (locale != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
            resourceBundle =
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    58
                SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.JAXPValidationMessages", locale);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
            resourceBundle =
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    62
                SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.JAXPValidationMessages");
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
        // format message
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
        String msg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
            msg = resourceBundle.getString(key);
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
            if (arguments != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
                    msg = java.text.MessageFormat.format(msg, arguments);
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
                catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
                    msg = resourceBundle.getString("FormatFailed");
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
                    msg += " " + resourceBundle.getString(key);
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        // error
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
        catch (MissingResourceException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
            msg = resourceBundle.getString("BadMessageKey");
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
            throw new MissingResourceException(key, msg, key);
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
        // no message
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        if (msg == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
            msg = key;
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
            if (arguments.length > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
                StringBuffer str = new StringBuffer(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
                str.append('?');
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
                for (int i = 0; i < arguments.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
                    if (i > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
                        str.append('&');
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
                    str.append(String.valueOf(arguments[i]));
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        return msg;
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
}