ehsbe CommonsTM
Version 1.1.0rc3

com.ehsbe.commons.util
Class Message

java.lang.Object
  extended by com.ehsbe.commons.util.Message

public class Message
extends Object

Utility to format i18n messages. It is just a wrapper around MessageFormat.

Version:
$Revision: 488 $
Author:
Jens Elkner

Constructor Summary
Message()
           
 
Method Summary
static String format(Locale locale, String msg, Object... args)
          Actually the same as MessageFormat.format(String, Object[]), however, arguments are first checked to be not null and format exceptions are caught and logged as an error message prefixed with the string 'Software bug: '.
static String format(ResourceBundle bundle, String key, Object... args)
          Lookup the specified key in the specified resource bundle and if a string value was found, format this string with the given arguments.
static String format(String msg, Object... args)
          Actually the same as MessageFormat.format(String, Object[]), however, arguments are first checked to be not null and format exceptions are caught and logged as an error message prefixed with the string 'Software bug: '.
static String formatSilent(ResourceBundle bundle, String key, Object... args)
          Same as format(ResourceBundle, String, Object[]), however, if the property with the given key could not be found, no warning occurs and null is returned immediately.
static char getMnemonic(ResourceBundle bundle, String key)
          Get the mnemonic character for the given key.
static String getValue(ResourceBundle bundle, String key)
          Get a certain (unformatted) message for the specified key from the specified resource bundle.
static String getValue(ResourceBundle bundle, String key, String defaultValue)
          Get a certain (unformatted) message for the specified key from the specified resource bundle.
static String getValueSilent(ResourceBundle bundle, String key)
          Get a unformatted message for the specified key from the specified resource bundle.
static String getValueSilent(ResourceBundle bundle, String key, String defaultValue)
          Get a unformatted message for the specified key from the specified resource bundle.
static ResourceBundle loadResBundle(Object o, String file, Locale locale)
          Load a language specific resource bundle wrt. to the given object.
static ResourceBundle loadResBundle(Object o, String file, String language)
          Load a language specific resource bundle wrt. to the given object.
static ResourceBundle loadResBundle(String path, Locale locale)
          load a language specific resource bundle.
static ResourceBundle loadResBundle(String path, String language)
          load a language specific resource bundle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Message

public Message()
Method Detail

getMnemonic

public static char getMnemonic(ResourceBundle bundle,
                               String key)
Get the mnemonic character for the given key.

Parameters:
bundle - bundle to use for lookup
key - key to use. _MN will be append before the lookup starts.
Returns:
0 if not found, the mnemonic character otherwise.

getValue

public static String getValue(ResourceBundle bundle,
                              String key)
Get a certain (unformatted) message for the specified key from the specified resource bundle.

Parameters:
bundle - the lookup source
key - the key to lookup
Returns:
the key itself, if bundle or key is null or could not be found in the specified bundle or if the object found for the given key is not a string (in this case an error message with the prefix 'Software bug;' is logged), the unformatted message string otherwise.

getValue

public static String getValue(ResourceBundle bundle,
                              String key,
                              String defaultValue)
Get a certain (unformatted) message for the specified key from the specified resource bundle.

Parameters:
bundle - the lookup source
key - the key to lookup
defaultValue - the value to return, if the lookup fails.
Returns:
the defaultValue itself, if bundle or key is null or could not be found in the specified bundle or if the object found for the given key is not a string (in this case an error message with the prefix 'Software bug;' is logged), the unformatted message string otherwise.

getValueSilent

public static String getValueSilent(ResourceBundle bundle,
                                    String key)
Get a unformatted message for the specified key from the specified resource bundle. If one of the given parameters are null, this fact is silently ignored and null will be returned.

Parameters:
bundle - the lookup source
key - the key to lookup
Returns:
null if the key was not found, the value for the given key otherwise.

getValueSilent

public static String getValueSilent(ResourceBundle bundle,
                                    String key,
                                    String defaultValue)
Get a unformatted message for the specified key from the specified resource bundle. If one of the given parameters are null, this fact is silently ignored and the defaultValue will be returned.

Parameters:
bundle - the lookup source
key - the key to lookup
defaultValue - the value to return, if the lookup fails.
Returns:
defaultValue if the key was not found, the value for the given key otherwise.

format

public static String format(ResourceBundle bundle,
                            String key,
                            Object... args)
Lookup the specified key in the specified resource bundle and if a string value was found, format this string with the given arguments.

If no arguments are given, the unformatted value is returned.

Parameters:
bundle - source for the key lookup
key - the key to lookup
args - arguments to use for formatting the found value for key
Returns:
null if the bundle does not contain a key with a propper value or if an error ocurred when formatting the found value, the formatted message otherwise.
See Also:
getValue(ResourceBundle, String), format(String, Object[])

formatSilent

public static String formatSilent(ResourceBundle bundle,
                                  String key,
                                  Object... args)
Same as format(ResourceBundle, String, Object[]), however, if the property with the given key could not be found, no warning occurs and null is returned immediately.

Parameters:
bundle - bundle to use for lookup
key - key to use for lookup
args - values to pass to format the key value
Returns:
null if the bundle is null or a pair with the given key could not be found, the formatted key otherwise.

format

public static String format(String msg,
                            Object... args)
Actually the same as MessageFormat.format(String, Object[]), however, arguments are first checked to be not null and format exceptions are caught and logged as an error message prefixed with the string 'Software bug: '.

Parameters:
msg - the unformatted message
args - arguments to be used for message formatting
Returns:
null, if msg or args is null or an error occurred during message formatting, the formatted message otherwise.
See Also:
MessageFormat.format(String, Object[])

format

public static String format(Locale locale,
                            String msg,
                            Object... args)
Actually the same as MessageFormat.format(String, Object[]), however, arguments are first checked to be not null and format exceptions are caught and logged as an error message prefixed with the string 'Software bug: '.

Parameters:
locale - locale to use for format
msg - the unformatted message
args - arguments to be used for message formatting
Returns:
null, if msg or args is null or an error occurred during message formatting, the formatted message otherwise.
See Also:
MessageFormat.format(String, Object[])

loadResBundle

public static ResourceBundle loadResBundle(String path,
                                           Locale locale)
load a language specific resource bundle.

Parameters:
path - resource path to th bundle (e.g. com/ims/misc/laber tries to load com/ims/misc/laber.properties)
locale - locale to use. If null or empty, en locale is used as fallback.
Returns:
true if a new, other than the current used Locale was set, false otherwise
Throws:
MissingResourceException - if the resource can not be found

loadResBundle

public static ResourceBundle loadResBundle(String path,
                                           String language)
load a language specific resource bundle.

Parameters:
path - resource path to th bundle (e.g. com/ims/misc/laber tries to load com/ims/misc/laber.properties)
language - the ISO 639 language code. if null or empty, en is used as fallback.
Returns:
true if a new, other than the current used Locale was set, false otherwise
Throws:
MissingResourceException - if the resource can not be found

loadResBundle

public static ResourceBundle loadResBundle(Object o,
                                           String file,
                                           String language)
Load a language specific resource bundle wrt. to the given object. The resource path is constructed as follows: PackageNameOfTheGivenObject/res/file With the so constructed path loadResBundle(String, String) gets called.

Parameters:
o - object to use for package name determination
file - a resource file name (without the language and properties extension)
language - language of the bundle to get
Returns:
a resource bundle

loadResBundle

public static ResourceBundle loadResBundle(Object o,
                                           String file,
                                           Locale locale)
Load a language specific resource bundle wrt. to the given object. The resource path is constructed as follows: PackageNameOfTheGivenObject/res/file With the so constructed path loadResBundle(String, String) gets called.

Parameters:
o - object to use for package name determination
file - a resource file name (without the language and properties extension)
locale - locale of the bundle to get
Returns:
a resource bundle

ehsbe CommonsTM
Version 1.1.0rc3

Copyright ©2005-2009 EHSBE, Walther-Rathenau-Str. 58, 39104 Magdeburg, Sachsen-Anhalt, Germany. All Rights Reserved.