src/java.xml/share/classes/com/sun/org/apache/xml/internal/res/XMLMessages.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
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
6
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
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xml.internal.res;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import java.util.Locale;
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    25
import java.util.ResourceBundle;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    26
import jdk.xml.internal.SecuritySupport;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 * A utility class for issuing XML error messages.
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * @xsl.usage internal
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
public class XMLMessages
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
  /** The local object to use.  */
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
  protected Locale fLocale = Locale.getDefault();
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
  /** The language specific resource object for XML messages.  */
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    39
  private static ResourceBundle XMLBundle = null;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
  /** The class name of the XML error message string table.    */
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
  private static final String XML_ERROR_RESOURCES =
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
    "com.sun.org.apache.xml.internal.res.XMLErrorResources";
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
  /** String to use if a bad message code is used. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
  protected static final String BAD_CODE = "BAD_CODE";
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
  /** String to use if the message format operation failed.  */
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  protected static final String FORMAT_FAILED = "FORMAT_FAILED";
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
   * Set the Locale object to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
   * @param locale non-null reference to Locale object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   public void setLocale(Locale locale)
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    fLocale = locale;
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
   * Get the Locale object that is being used.
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
   * @return non-null reference to Locale object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
  public Locale getLocale()
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    return fLocale;
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   * Creates a message from the specified key and replacement
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
   * arguments, localized to the given locale.
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
   * @param msgKey    The key for the message text.
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
   * @param args      The arguments to be used as replacement text
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
   *                  in the message created.
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * @return The formatted message string.
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
  public static final String createXMLMessage(String msgKey, Object args[])
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
  {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    83
    if (XMLBundle == null) {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    84
        XMLBundle = SecuritySupport.getResourceBundle(XML_ERROR_RESOURCES);
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    85
    }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    if (XMLBundle != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
      return createMsg(XMLBundle, msgKey, args);
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
      return "Could not load any resource bundles.";
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
   * Creates a message from the specified key and replacement
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
   * arguments, localized to the given locale.
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   * @param fResourceBundle The resource bundle to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
   * @param msgKey  The message key to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
   * @param args      The arguments to be used as replacement text
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
   *                  in the message created.
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
   * @return The formatted message string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
   */
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   106
  public static final String createMsg(ResourceBundle fResourceBundle,
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        String msgKey, Object args[])  //throws Exception
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    String fmsg = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    boolean throwex = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    String msg = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    if (msgKey != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
      msg = fResourceBundle.getString(msgKey);
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    if (msg == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
      msg = fResourceBundle.getString(BAD_CODE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
      throwex = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    if (args != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
      try
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        // Do this to keep format from crying.
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        // This is better than making a bunch of conditional
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        // code all over the place.
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
        int n = args.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        for (int i = 0; i < n; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
          if (null == args[i])
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
            args[i] = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
        fmsg = java.text.MessageFormat.format(msg, args);
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
      catch (Exception e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        fmsg = fResourceBundle.getString(FORMAT_FAILED);
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
        fmsg += " " + msg;
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
      fmsg = msg;
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    if (throwex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
      throw new RuntimeException(fmsg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    return fmsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
}