View Javadoc

1   /**
2    *
3    */
4   package net.sf.m2prototype.util;
5   
6   import java.text.MessageFormat;
7   import java.util.MissingResourceException;
8   import java.util.ResourceBundle;
9   
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  
13  /**
14   * <code>Messages</code> provides access to human-presented text.
15   * Text for <code>m2prototype</code> may be modified in
16   * <code>net/sf/m2prototype/util/messages.properties</code>.
17   * <p>Design Pattern:  Static Factory Method
18   *
19   * @author <a href="mailto:gnu_know_who@users.sourceforge.net">GNU
20   * Know Who!</a>
21   * @version 1.0
22   *
23   */
24  public final class Messages {
25      /**
26       * Allocate a Log.
27       */
28      private static final Log LOG = LogFactory.getLog(Messages.class);
29  
30      /**
31       * Allocate a String to hold the name of a resource bundle.
32       */
33      private static final String BUNDLE_NAME
34              = "net.sf.m2prototype.util.messages"; //$NON-NLS-1$
35  
36      /**
37       * Allocate a Resource Bundle object.
38       */
39      private static final ResourceBundle RESOURCE_BUNDLE
40              = ResourceBundle.getBundle(BUNDLE_NAME);
41  
42      /**
43       * Prevent instantiation.  This is a Static Factory Method class.
44       */
45      Messages() {
46      }
47  
48      /**
49       * Gets a <code>String</code> value corresponding to a particular lookup
50       * key.
51       * @param key The key to be used for looking up a message.
52       * @return the Message requested by the lookup key.
53       */
54      public static String getString(final String key) {
55          try {
56              return RESOURCE_BUNDLE.getString(key);
57          } catch (MissingResourceException e) {
58              LOG.warn("Could not fulfill request for KEY (" + key + ")", e);
59              return '!' + key + '!';
60          }
61      }
62  
63      /**
64       * Gets a <code>String</code> value corresponding to a particular lookup
65       * key and performs the appropriate substitution based on additional
66       * params.
67       *
68       * @param key the key to be used for looking up a message template.
69       * @param params the values to be substituted into the message template.
70       * @return the result of substituting the params into the message template.
71       */
72      public static String getString(final String key, final Object[] params) {
73          try {
74              return MessageFormat.format(RESOURCE_BUNDLE.getString(key), params);
75          } catch (MissingResourceException e) {
76              LOG.warn("Could not fulfill request for KEY (" + key + ")", e);
77              return '!' + key + '!';
78          }
79      }
80  
81      /**
82       * Gets a <code>String</code> value corresponding to a particular lookup key
83       * and performs the appropriate substitution based on an additional param.
84       *
85       * @param key the key to be used for looking up a message template.
86       * @param param the value to be substituted into the message template.
87       * @return the result of substituting the param into the message template.
88       */
89      public static String getString(final String key, final Object param) {
90          return getString(key, new Object[] {param});
91      }
92  }