/*
* $Id: BundleUtils.java,v 1.1.1.1 2005/04/07 18:36:22 pocho Exp $
*/
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* Provides convinience methods for receving messages from resource bundles.
*
* @version $Name: $ - $Revision: 1.1.1.1 $ - $Date: 2005/04/07 18:36:22 $
*/
public class BundleUtils {
private BundleUtils() {}
/**
* Convenience method for receving message of given key from bundle.
* It handles {@linkplain MissingResourceException} by returning key
* surrounded by exclamation mark ("!"). Exception's message is also
* printed out.
*
* @param bundle
* @param key
* @return
*/
public static String getString(ResourceBundle bundle, String key) {
try {
return bundle.getString(key);
}
catch (Exception ex) {
System.out.println("Error: " + BundleUtils.class + ": method getString(ResourceBundle, String) - bundle: " +
bundle + " key: " + key);
return '!' + key + '!';
}
}
/**
* Convenience method for formatting returned message with array of objects.
*
* @param bundle
* @param key
* @param objects
* @return
*/
public static String getMessageFormattedString(ResourceBundle bundle, String key,
Object[] objects) {
String message = getString(bundle, key);
return MessageFormat.format(message, objects);
}
public static Object[] createParams(Object o) {
Object[] params = null;
if (o instanceof Object[])
params = (Object[]) o;
else if (o != null)
params = new Object[] {o};
return params;
}
/**
* Convenience method for formatting returned message with a single objects.
*
* @param bundle
* @param key
* @param objects
* @return
*/
public static String getMessageFormattedString(ResourceBundle bundle, String key,
Object param) {
Object[] o = new Object[] {param};
return getMessageFormattedString(bundle, key, param);
}
}
|