286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 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: Messages.java,v 1.1.4.1 2005/09/08 11:03:10 suresh_emailid Exp $
286N/A */
286N/Apackage com.sun.org.apache.xml.internal.serializer.utils;
286N/A
524N/Aimport com.sun.org.apache.xalan.internal.utils.SecuritySupport;
286N/Aimport java.util.ListResourceBundle;
286N/Aimport java.util.Locale;
286N/Aimport java.util.MissingResourceException;
286N/Aimport java.util.ResourceBundle;
286N/A
286N/A/**
286N/A * A utility class for issuing error messages.
286N/A *
286N/A * A user of this class normally would create a singleton
286N/A * instance of this class, passing the name
286N/A * of the message class on the constructor. For example:
286N/A * <CODE>
286N/A * static Messages x = new Messages("org.package.MyMessages");
286N/A * </CODE>
286N/A * Later the message is typically generated this way if there are no
286N/A * substitution arguments:
286N/A * <CODE>
286N/A * String msg = x.createMessage(org.package.MyMessages.KEY_ONE, null);
286N/A * </CODE>
286N/A * If there are arguments substitutions then something like this:
286N/A * <CODE>
286N/A * String filename = ...;
286N/A * String directory = ...;
286N/A * String msg = x.createMessage(org.package.MyMessages.KEY_TWO,
286N/A * new Object[] {filename, directory) );
286N/A * </CODE>
286N/A *
286N/A * The constructor of an instance of this class must be given
286N/A * the class name of a class that extends java.util.ListResourceBundle
286N/A * ("org.package.MyMessages" in the example above).
286N/A * The name should not have any language suffix
286N/A * which will be added automatically by this utility class.
286N/A *
286N/A * The message class ("org.package.MyMessages")
286N/A * must define the abstract method getContents() that is
286N/A * declared in its base class, for example:
286N/A * <CODE>
286N/A * public Object[][] getContents() {return contents;}
286N/A * </CODE>
286N/A *
286N/A * It is suggested that the message class expose its
286N/A * message keys like this:
286N/A * <CODE>
286N/A * public static final String KEY_ONE = "KEY1";
286N/A * public static final String KEY_TWO = "KEY2";
286N/A * . . .
286N/A * </CODE>
286N/A * and used through their names (KEY_ONE ...) rather than
286N/A * their values ("KEY1" ...).
286N/A *
286N/A * The field contents (returned by getContents()
286N/A * should be initialized something like this:
286N/A * <CODE>
286N/A * public static final Object[][] contents = {
286N/A * { KEY_ONE, "Something has gone wrong!" },
286N/A * { KEY_TWO, "The file ''{0}'' does not exist in directory ''{1}''." },
286N/A * . . .
286N/A * { KEY_N, "Message N" } }
286N/A * </CODE>
286N/A *
286N/A * Where that section of code with the KEY to Message mappings
286N/A * (where the message classes 'contents' field is initialized)
286N/A * can have the Message strings translated in an alternate language
286N/A * in a errorResourceClass with a language suffix.
286N/A *
286N/A *
286N/A * This class is not a public API, it is only public because it is
286N/A * used in com.sun.org.apache.xml.internal.serializer.
286N/A *
286N/A * @xsl.usage internal
286N/A */
286N/Apublic final class Messages
286N/A{
286N/A /** The local object to use. */
286N/A private final Locale m_locale = Locale.getDefault();
286N/A
286N/A /** The language specific resource object for messages. */
286N/A private ListResourceBundle m_resourceBundle;
286N/A
286N/A /** The class name of the error message string table with no language suffix. */
286N/A private String m_resourceBundleName;
286N/A
286N/A
286N/A
286N/A /**
286N/A * Constructor.
286N/A * @param resourceBundle the class name of the ListResourceBundle
286N/A * that the instance of this class is associated with and will use when
286N/A * creating messages.
286N/A * The class name is without a language suffix. If the value passed
286N/A * is null then loadResourceBundle(errorResourceClass) needs to be called
286N/A * explicitly before any messages are created.
286N/A *
286N/A * @xsl.usage internal
286N/A */
286N/A Messages(String resourceBundle)
286N/A {
286N/A
286N/A m_resourceBundleName = resourceBundle;
286N/A }
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 * @xsl.usage internal
286N/A */
286N/A private Locale getLocale()
286N/A {
286N/A return m_locale;
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 * @xsl.usage internal
286N/A */
286N/A public final String createMessage(String msgKey, Object args[])
286N/A {
286N/A if (m_resourceBundle == null)
524N/A m_resourceBundle = SecuritySupport.getResourceBundle(m_resourceBundleName);
286N/A
286N/A if (m_resourceBundle != null)
286N/A {
286N/A return createMsg(m_resourceBundle, msgKey, args);
286N/A }
286N/A else
286N/A return "Could not load the resource bundles: "+ m_resourceBundleName;
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 errorCode The key for the message text.
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 * @xsl.usage internal
286N/A */
286N/A private final String createMsg(
286N/A ListResourceBundle fResourceBundle,
286N/A String msgKey,
286N/A 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 else
286N/A msgKey = "";
286N/A
286N/A if (msg == null)
286N/A {
286N/A throwex = true;
286N/A /* The message is not in the bundle . . . this is bad,
286N/A * so try to get the message that the message is not in the bundle
286N/A */
286N/A try
286N/A {
286N/A
286N/A msg =
286N/A java.text.MessageFormat.format(
286N/A MsgKey.BAD_MSGKEY,
286N/A new Object[] { msgKey, m_resourceBundleName });
286N/A }
286N/A catch (Exception e)
286N/A {
286N/A /* even the message that the message is not in the bundle is
286N/A * not there ... this is really bad
286N/A */
286N/A msg =
286N/A "The message key '"
286N/A + msgKey
286N/A + "' is not in the message class '"
286N/A + m_resourceBundleName+"'";
286N/A }
286N/A }
286N/A else if (args != null)
286N/A {
286N/A try
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 // if we get past the line above we have create the message ... hurray!
286N/A }
286N/A catch (Exception e)
286N/A {
286N/A throwex = true;
286N/A try
286N/A {
286N/A // Get the message that the format failed.
286N/A fmsg =
286N/A java.text.MessageFormat.format(
286N/A MsgKey.BAD_MSGFORMAT,
286N/A new Object[] { msgKey, m_resourceBundleName });
286N/A fmsg += " " + msg;
286N/A }
286N/A catch (Exception formatfailed)
286N/A {
286N/A // We couldn't even get the message that the format of
286N/A // the message failed ... so fall back to English.
286N/A fmsg =
286N/A "The format of message '"
286N/A + msgKey
286N/A + "' in message class '"
286N/A + m_resourceBundleName
286N/A + "' failed.";
286N/A }
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}