286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: XMLMessages.java,v 1.2.4.1 2005/09/15 07:45:48 suresh_emailid Exp $
286N/A */
286N/Apackage com.sun.org.apache.xml.internal.res;
286N/A
524N/Aimport com.sun.org.apache.xalan.internal.utils.SecuritySupport;
286N/Aimport java.util.ListResourceBundle;
286N/Aimport java.util.Locale;
286N/A
286N/A/**
286N/A * A utility class for issuing XML error messages.
286N/A * @xsl.usage internal
286N/A */
286N/Apublic class XMLMessages
286N/A{
286N/A
286N/A /** The local object to use. */
286N/A protected Locale fLocale = Locale.getDefault();
286N/A
286N/A /** The language specific resource object for XML messages. */
286N/A private static ListResourceBundle XMLBundle = null;
286N/A
286N/A /** The class name of the XML error message string table. */
286N/A private static final String XML_ERROR_RESOURCES =
286N/A "com.sun.org.apache.xml.internal.res.XMLErrorResources";
286N/A
286N/A /** String to use if a bad message code is used. */
286N/A protected static final String BAD_CODE = "BAD_CODE";
286N/A
286N/A /** String to use if the message format operation failed. */
286N/A protected static final String FORMAT_FAILED = "FORMAT_FAILED";
286N/A
286N/A /**
286N/A * Set the Locale object to use.
286N/A *
286N/A * @param locale non-null reference to Locale object.
286N/A */
286N/A public void setLocale(Locale locale)
286N/A {
286N/A fLocale = locale;
286N/A }
286N/A
286N/A /**
286N/A * Get the Locale object that is being used.
286N/A *
286N/A * @return non-null reference to Locale object.
286N/A */
286N/A public Locale getLocale()
286N/A {
286N/A return fLocale;
286N/A }
286N/A
286N/A /**
286N/A * Creates a message from the specified key and replacement
286N/A * arguments, localized to the given locale.
286N/A *
286N/A * @param msgKey The key for the message text.
286N/A * @param args The arguments to be used as replacement text
286N/A * in the message created.
286N/A *
286N/A * @return The formatted message string.
286N/A */
286N/A public static final String createXMLMessage(String msgKey, Object args[])
286N/A {
524N/A if (XMLBundle == null) {
524N/A XMLBundle = SecuritySupport.getResourceBundle(XML_ERROR_RESOURCES);
524N/A }
286N/A
286N/A if (XMLBundle != null)
286N/A {
286N/A return createMsg(XMLBundle, msgKey, args);
286N/A }
286N/A else
286N/A return "Could not load any resource bundles.";
286N/A }
286N/A
286N/A /**
286N/A * Creates a message from the specified key and replacement
286N/A * arguments, localized to the given locale.
286N/A *
286N/A * @param fResourceBundle The resource bundle to use.
286N/A * @param msgKey The message key to use.
286N/A * @param args The arguments to be used as replacement text
286N/A * in the message created.
286N/A *
286N/A * @return The formatted message string.
286N/A */
286N/A public static final String createMsg(ListResourceBundle fResourceBundle,
286N/A String msgKey, Object args[]) //throws Exception
286N/A {
286N/A
286N/A String fmsg = null;
286N/A boolean throwex = false;
286N/A String msg = null;
286N/A
286N/A if (msgKey != null)
286N/A msg = fResourceBundle.getString(msgKey);
286N/A
286N/A if (msg == null)
286N/A {
286N/A msg = fResourceBundle.getString(BAD_CODE);
286N/A throwex = true;
286N/A }
286N/A
286N/A if (args != null)
286N/A {
286N/A try
286N/A {
286N/A
286N/A // Do this to keep format from crying.
286N/A // This is better than making a bunch of conditional
286N/A // code all over the place.
286N/A int n = args.length;
286N/A
286N/A for (int i = 0; i < n; i++)
286N/A {
286N/A if (null == args[i])
286N/A args[i] = "";
286N/A }
286N/A
286N/A fmsg = java.text.MessageFormat.format(msg, args);
286N/A }
286N/A catch (Exception e)
286N/A {
286N/A fmsg = fResourceBundle.getString(FORMAT_FAILED);
286N/A fmsg += " " + msg;
286N/A }
286N/A }
286N/A else
286N/A fmsg = msg;
286N/A
286N/A if (throwex)
286N/A {
286N/A throw new RuntimeException(fmsg);
286N/A }
286N/A
286N/A return fmsg;
286N/A }
286N/A
286N/A}