FileDocCategorySizeDatePackage
Messages.javaAPI DocJava SE 6 API11695Tue Jun 10 00:23:10 BST 2008com.sun.org.apache.xml.internal.serializer.utils

Messages

public final class Messages extends Object
A utility class for issuing error messages. A user of this class normally would create a singleton instance of this class, passing the name of the message class on the constructor. For example: static Messages x = new Messages("org.package.MyMessages"); Later the message is typically generated this way if there are no substitution arguments: String msg = x.createMessage(org.package.MyMessages.KEY_ONE, null); If there are arguments substitutions then something like this: String filename = ...; String directory = ...; String msg = x.createMessage(org.package.MyMessages.KEY_TWO, new Object[] {filename, directory) ); The constructor of an instance of this class must be given the class name of a class that extends java.util.ListResourceBundle ("org.package.MyMessages" in the example above). The name should not have any language suffix which will be added automatically by this utility class. The message class ("org.package.MyMessages") must define the abstract method getContents() that is declared in its base class, for example: public Object[][] getContents() {return contents;} It is suggested that the message class expose its message keys like this: public static final String KEY_ONE = "KEY1"; public static final String KEY_TWO = "KEY2"; . . . and used through their names (KEY_ONE ...) rather than their values ("KEY1" ...). The field contents (returned by getContents() should be initialized something like this: public static final Object[][] contents = { { KEY_ONE, "Something has gone wrong!" }, { KEY_TWO, "The file ''{0}'' does not exist in directory ''{1}''." }, . . . { KEY_N, "Message N" } } Where that section of code with the KEY to Message mappings (where the message classes 'contents' field is initialized) can have the Message strings translated in an alternate language in a errorResourceClass with a language suffix. More sophisticated use of this class would be to pass null when contructing it, but then call loadResourceBundle() before creating any messages. This class is not a public API, it is only public because it is used in com.sun.org.apache.xml.internal.serializer.
xsl.usage
internal

Fields Summary
private final Locale
m_locale
The local object to use.
private ListResourceBundle
m_resourceBundle
The language specific resource object for messages.
private String
m_resourceBundleName
The class name of the error message string table with no language suffix.
Constructors Summary
Messages(String resourceBundle)
Constructor.

param
resourceBundle the class name of the ListResourceBundle that the instance of this class is associated with and will use when creating messages. The class name is without a language suffix. If the value passed is null then loadResourceBundle(errorResourceClass) needs to be called explicitly before any messages are created.
xsl.usage
internal




                                                              
     
    

        m_resourceBundleName = resourceBundle;
    
Methods Summary
public final java.lang.StringcreateMessage(java.lang.String msgKey, java.lang.Object[] args)
Creates a message from the specified key and replacement arguments, localized to the given locale.

param
msgKey The key for the message text.
param
args The arguments to be used as replacement text in the message created.
return
The formatted message string.
xsl.usage
internal

        if (m_resourceBundle == null)
            m_resourceBundle = loadResourceBundle(m_resourceBundleName);

        if (m_resourceBundle != null)
        {
            return createMsg(m_resourceBundle, msgKey, args);
        }
        else
            return "Could not load the resource bundles: "+ m_resourceBundleName;
    
private final java.lang.StringcreateMsg(java.util.ListResourceBundle fResourceBundle, java.lang.String msgKey, java.lang.Object[] args)
Creates a message from the specified key and replacement arguments, localized to the given locale.

param
errorCode The key for the message text.
param
fResourceBundle The resource bundle to use.
param
msgKey The message key to use.
param
args The arguments to be used as replacement text in the message created.
return
The formatted message string.
xsl.usage
internal


        String fmsg = null;
        boolean throwex = false;
        String msg = null;

        if (msgKey != null)
            msg = fResourceBundle.getString(msgKey);
        else
            msgKey = "";

        if (msg == null)
        {
            throwex = true;
            /* The message is not in the bundle . . . this is bad,
             * so try to get the message that the message is not in the bundle
             */
            try
            {

                msg =
                    java.text.MessageFormat.format(
                        MsgKey.BAD_MSGKEY,
                        new Object[] { msgKey, m_resourceBundleName });
            }
            catch (Exception e)
            {
                /* even the message that the message is not in the bundle is
                 * not there ... this is really bad
                 */
                msg =
                    "The message key '"
                        + msgKey
                        + "' is not in the message class '"
                        + m_resourceBundleName+"'";
            }
        }
        else if (args != null)
        {
            try
            {
                // Do this to keep format from crying.
                // This is better than making a bunch of conditional
                // code all over the place.
                int n = args.length;

                for (int i = 0; i < n; i++)
                {
                    if (null == args[i])
                        args[i] = "";
                }

                fmsg = java.text.MessageFormat.format(msg, args);
                // if we get past the line above we have create the message ... hurray!
            }
            catch (Exception e)
            {
                throwex = true;
                try
                {
                    // Get the message that the format failed.
                    fmsg =
                        java.text.MessageFormat.format(
                            MsgKey.BAD_MSGFORMAT,
                            new Object[] { msgKey, m_resourceBundleName });
                    fmsg += " " + msg;
                }
                catch (Exception formatfailed)
                {
                    // We couldn't even get the message that the format of
                    // the message failed ... so fall back to English.
                    fmsg =
                        "The format of message '"
                            + msgKey
                            + "' in message class '"
                            + m_resourceBundleName
                            + "' failed.";
                }
            }
        }
        else
            fmsg = msg;

        if (throwex)
        {
            throw new RuntimeException(fmsg);
        }

        return fmsg;
    
private java.util.LocalegetLocale()
Get the Locale object that is being used.

return
non-null reference to Locale object.
xsl.usage
internal

        return m_locale;
    
private java.util.ListResourceBundlegetResourceBundle()
Get the ListResourceBundle being used by this Messages instance which was previously set by a call to loadResourceBundle(className)

xsl.usage
internal

        return m_resourceBundle;
    
private static java.lang.StringgetResourceSuffix(java.util.Locale locale)
Return the resource file suffic for the indicated locale For most locales, this will be based the language code. However for Chinese, we do distinguish between Taiwan and PRC

param
locale the locale
return
an String suffix which can be appended to a resource name
xsl.usage
internal


        String suffix = "_" + locale.getLanguage();
        String country = locale.getCountry();

        if (country.equals("TW"))
            suffix += "_" + country;

        return suffix;
    
private java.util.ListResourceBundleloadResourceBundle(java.lang.String resourceBundle)
Return a named ResourceBundle for a particular locale. This method mimics the behavior of ResourceBundle.getBundle().

param
className the name of the class that implements ListResourceBundle, without language suffix.
return
the ResourceBundle
throws
MissingResourceException
xsl.usage
internal

        m_resourceBundleName = resourceBundle;
        Locale locale = getLocale();

        ListResourceBundle lrb;

        try
        {

            ResourceBundle rb =
                ResourceBundle.getBundle(m_resourceBundleName, locale);
            lrb = (ListResourceBundle) rb;
        }
        catch (MissingResourceException e)
        {
            try // try to fall back to en_US if we can't load
                {

                // Since we can't find the localized property file,
                // fall back to en_US.
                lrb =
                    (ListResourceBundle) ResourceBundle.getBundle(
                        m_resourceBundleName,
                        new Locale("en", "US"));
            }
            catch (MissingResourceException e2)
            {

                // Now we are really in trouble.
                // very bad, definitely very bad...not going to get very far
                throw new MissingResourceException(
                    "Could not load any resource bundles." + m_resourceBundleName,
                    m_resourceBundleName,
                    "");
            }
        }
        m_resourceBundle = lrb;
        return lrb;