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