1 import java.text.MessageFormat;
   2 import java.util.MissingResourceException;
   3 import java.util.ResourceBundle;
   4 
   5 /**
   6  * Utility class which helps with managing messages.
   7  */
   8 class MessageUtil {
   9   private static final String RESOURCE_BUNDLE = "messages";
  10 
  11   private static ResourceBundle fgResourceBundle 
  12     = ResourceBundle.getBundle(RESOURCE_BUNDLE);
  13 
  14   private MessageUtil() {
  15     // prevent instantiation of class
  16   }
  17 
  18   /**
  19    * Returns the formatted message for the given key in
  20    * the resource bundle. 
  21    *
  22    * @param key the resource name
  23    * @param args the message arguments
  24    * @return the string
  25    */
  26   public static String format(String key, Object[] args) {
  27     return MessageFormat.format(getString(key), args);
  28   }
  29 
  30   /**
  31    * Returns the resource object with the given key in
  32    * the resource bundle. If there isn't any value under
  33    * the given key, the key is returned, surrounded by '!'s.
  34    *
  35    * @param key the resource name
  36    * @return the string
  37    */
  38   public static String getString(String key) {
  39     try {
  40       return this.fgResourceBundle.getString(key);
  41     } catch (MissingResourceException e) {
  42       return "!" + key + "!";
  43     }
  44   }
  45 
  46   public static ResourceBundle getResourceBundle() {
  47     return this.fgResourceBundle;
  48   }
  49 }


CategoryJava

ZbmonWiki: MessageUtil.java (2005-11-07 14:30:29에 zbmon가(이) 마지막으로 수정)