org.apache.commons.resources
Class Messages

java.lang.Object
  extended by org.apache.commons.resources.Messages
All Implemented Interfaces:
Serializable

public class Messages
extends Object
implements Serializable

Wrapper around any Resources object that performs message string lookups from the Resources instance, and parameter replacement via java.text.MessageFormat. For convenience, the same functionality is also available via static methods that accept a Resources parameter.

Calls to getMessage() variants without a Locale argument are presumed to be requesting a message string in the default Locale for this JVM.

When a getString() call to the underlying Resources instance fails or returns null, a suitable error message String will be returned.

See Also:
Serialized Form

Constructor Summary
Messages(Resources resources)
          Construct a Messages instance that wraps the specified Resources instance.
 
Method Summary
 String getMessage(Locale locale, String key)
          Return a text message for the specified key, for the specified Locale.
 String getMessage(Locale locale, String key, Object arg0)
          Return a text message for the specified key, for the specified Locale, with parametric replacement.
 String getMessage(Locale locale, String key, Object[] args)
          Return a text message for the specified key, for the specified Locale, with parametric replacement.
static String getMessage(Resources resources, Locale locale, String key)
          Return a text message for the specified key, for the specified Locale.
static String getMessage(Resources resources, Locale locale, String key, Object arg0)
          Return a text message for the specified key, for the specified Locale, with parametric replacement.
static String getMessage(Resources resources, Locale locale, String key, Object[] args)
          Return a text message for the specified key, for the specified Locale, with parametric replacement.
static String getMessage(Resources resources, String key)
          Return a text message for the specified key, for the default Locale.
static String getMessage(Resources resources, String key, Object arg0)
          Return a text message for the specified key, for the default Locale, with parametric replacement.
static String getMessage(Resources resources, String key, Object[] args)
          Return a text message for the specified key, for the default Locale, with parametric replacement.
 String getMessage(String key)
          Return a text message for the specified key, for the default Locale.
 String getMessage(String key, Object arg0)
          Return a text message for the specified key, for the default Locale, with parametric replacement.
 String getMessage(String key, Object[] args)
          Return a text message for the specified key, for the default Locale, with parametric replacement.
static Messages getMessages(String name)
          Convenience factory method to create a Messages instance that wraps a Resources instance that contains message resources for the specified properties file.
 Resources getResources()
          Return the Resources instance that we are wrapping.
static void setFactory(ResourcesFactory factory)
          Set the ResourcesFactory that will be used by the getMessages() method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Messages

public Messages(Resources resources)

Construct a Messages instance that wraps the specified Resources instance.

Parameters:
resources - The Resources instance from which message strings are to be retrieved
Method Detail

getResources

public Resources getResources()

Return the Resources instance that we are wrapping.

Returns:
The wrapped resources.

getMessage

public String getMessage(String key)

Return a text message for the specified key, for the default Locale.

Parameters:
key - Message key to retrieve
Returns:
The text message for the specified key.

getMessage

public String getMessage(Locale locale,
                         String key)

Return a text message for the specified key, for the specified Locale.

Parameters:
locale - Locale for which to retrieve the message
key - Message key to retrieve
Returns:
The text message for the specified key and locale.

getMessage

public String getMessage(String key,
                         Object[] args)

Return a text message for the specified key, for the default Locale, with parametric replacement.

Parameters:
key - Message key to retrieve
args - Array of replacement values
Returns:
The text message with parametric replacement.

getMessage

public String getMessage(Locale locale,
                         String key,
                         Object[] args)

Return a text message for the specified key, for the specified Locale, with parametric replacement.

Parameters:
locale - Locale for which to retrieve the message
key - Message key to retrieve
args - Array of replacement values
Returns:
The text message for a spcified locale with parametric replacement.

getMessage

public String getMessage(String key,
                         Object arg0)

Return a text message for the specified key, for the default Locale, with parametric replacement.

Parameters:
key - Message key to retrieve
arg0 - Individual parameter replacement value
Returns:
The text message with parametric replacement.

getMessage

public String getMessage(Locale locale,
                         String key,
                         Object arg0)

Return a text message for the specified key, for the specified Locale, with parametric replacement.

Parameters:
locale - Locale for which to retrieve the message
key - Message key to retrieve
arg0 - Individual parameter replacement value
Returns:
The text message for a spcified locale with parametric replacement.

setFactory

public static void setFactory(ResourcesFactory factory)

Set the ResourcesFactory that will be used by the getMessages() method.

Parameters:
factory - ResourcesFactory instance to set.

getMessage

public static String getMessage(Resources resources,
                                String key)

Return a text message for the specified key, for the default Locale.

Parameters:
resources - Resources instance to retrieve the message from
key - Message key to retrieve
Returns:
The text message.

getMessage

public static String getMessage(Resources resources,
                                Locale locale,
                                String key)

Return a text message for the specified key, for the specified Locale.

Parameters:
resources - Resources instance to retrieve the message from
locale - Locale for which to retrieve the message
key - Message key to retrieve
Returns:
The text message.

getMessage

public static String getMessage(Resources resources,
                                String key,
                                Object[] args)

Return a text message for the specified key, for the default Locale, with parametric replacement.

Parameters:
resources - Resources instance to retrieve the message from
key - Message key to retrieve
args - Array of replacement values
Returns:
The text message.

getMessage

public static String getMessage(Resources resources,
                                Locale locale,
                                String key,
                                Object[] args)

Return a text message for the specified key, for the specified Locale, with parametric replacement.

Parameters:
resources - Resources instance to retrieve the message from
locale - Locale for which to retrieve the message
key - Message key to retrieve
args - Array of replacement values
Returns:
The text message.

getMessage

public static String getMessage(Resources resources,
                                String key,
                                Object arg0)

Return a text message for the specified key, for the default Locale, with parametric replacement.

Parameters:
resources - Resources instance to retrieve the message from
key - Message key to retrieve
arg0 - Individual parameter replacement value
Returns:
The text message.

getMessage

public static String getMessage(Resources resources,
                                Locale locale,
                                String key,
                                Object arg0)

Return a text message for the specified key, for the specified Locale, with parametric replacement.

Parameters:
resources - Resources instance to retrieve the message from
locale - Locale for which to retrieve the message
key - Message key to retrieve
arg0 - Individual parameter replacement value
Returns:
The text message.

getMessages

public static Messages getMessages(String name)

Convenience factory method to create a Messages instance that wraps a Resources instance that contains message resources for the specified properties file. It is expected that the resources for each package will be in properties files that are nested in the package directory, with names like LocalStrings.properties for the default messages, and names like LocalStrings_en_US.properties for messages localized to a particular Locale.

Parameters:
name - Package + file name of the properties file for which local message resources are desired (ie. org.apache.commons.resources.LocalStrings).
Returns:
The text messages.


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.