org.apache.xml.serializer.utils
Class Messages

java.lang.Object
  extended by org.apache.xml.serializer.utils.Messages

public final class Messages
extends java.lang.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 org.apache.xml.serializer.


Field Summary
private  java.util.Locale m_locale
          The local object to use.
private  java.util.ListResourceBundle m_resourceBundle
          The language specific resource object for messages.
private  java.lang.String m_resourceBundleName
          The class name of the error message string table with no language suffix.
 
Constructor Summary
Messages(java.lang.String resourceBundle)
          Constructor.
 
Method Summary
 java.lang.String createMessage(java.lang.String msgKey, java.lang.Object[] args)
          Creates a message from the specified key and replacement arguments, localized to the given locale.
private  java.lang.String createMsg(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.
private  java.util.Locale getLocale()
          Get the Locale object that is being used.
private  java.util.ListResourceBundle getResourceBundle()
          Get the ListResourceBundle being used by this Messages instance which was previously set by a call to loadResourceBundle(className)
private static java.lang.String getResourceSuffix(java.util.Locale locale)
          Return the resource file suffic for the indicated locale For most locales, this will be based the language code.
private  java.util.ListResourceBundle loadResourceBundle(java.lang.String resourceBundle)
          Return a named ResourceBundle for a particular locale.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_locale

private final java.util.Locale m_locale
The local object to use.


m_resourceBundle

private java.util.ListResourceBundle m_resourceBundle
The language specific resource object for messages.


m_resourceBundleName

private java.lang.String m_resourceBundleName
The class name of the error message string table with no language suffix.

Constructor Detail

Messages

Messages(java.lang.String resourceBundle)
Constructor.

Parameters:
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.
Method Detail

getLocale

private java.util.Locale getLocale()
Get the Locale object that is being used.

Returns:
non-null reference to Locale object.

getResourceBundle

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


createMessage

public final java.lang.String createMessage(java.lang.String msgKey,
                                            java.lang.Object[] args)
Creates a message from the specified key and replacement arguments, localized to the given locale.

Parameters:
msgKey - The key for the message text.
args - The arguments to be used as replacement text in the message created.
Returns:
The formatted message string.

createMsg

private final java.lang.String createMsg(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.

Parameters:
errorCode - The key for the message text.
fResourceBundle - The resource bundle to use.
msgKey - The message key to use.
args - The arguments to be used as replacement text in the message created.
Returns:
The formatted message string.

loadResourceBundle

private java.util.ListResourceBundle loadResourceBundle(java.lang.String resourceBundle)
                                                 throws java.util.MissingResourceException
Return a named ResourceBundle for a particular locale. This method mimics the behavior of ResourceBundle.getBundle().

Parameters:
className - the name of the class that implements ListResourceBundle, without language suffix.
Returns:
the ResourceBundle
Throws:
java.util.MissingResourceException

getResourceSuffix

private static java.lang.String getResourceSuffix(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

Parameters:
locale - the locale
Returns:
an String suffix which can be appended to a resource name