View Javadoc

1   /***
2    * Licensed under the Artistic License; you may not use this file
3    * except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://displaytag.sourceforge.net/license.html
7    *
8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11   */
12  package org.displaytag;
13  
14  import java.text.MessageFormat;
15  import java.util.MissingResourceException;
16  import java.util.ResourceBundle;
17  
18  
19  /***
20   * Helper class for message bundle access.
21   * @author Fabrizio Giustina
22   * @version $Revision: 1.3 $ ($Author: fgiust $)
23   */
24  public final class Messages
25  {
26  
27      /***
28       * Base name for the bundle.
29       */
30      private static final String BUNDLE_NAME = "org.displaytag.messages"; //$NON-NLS-1$
31  
32      /***
33       * Loaded ResourceBundle.
34       */
35      private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
36  
37      /***
38       * Don't instantiate.
39       */
40      private Messages()
41      {
42          // unused
43      }
44  
45      /***
46       * Returns a message from the resource bundle.
47       * @param key Message key.
48       * @return message String.
49       */
50      public static String getString(String key)
51      {
52          try
53          {
54              return RESOURCE_BUNDLE.getString(key);
55          }
56          catch (MissingResourceException e)
57          {
58              return '!' + key + '!';
59          }
60      }
61  
62      /***
63       * Reads a message from the resource bundle and format it using java MessageFormat.
64       * @param key Message key.
65       * @param parameters Parameters to pass to MessageFormat.format()
66       * @return message String.
67       */
68      public static String getString(String key, Object[] parameters)
69      {
70          String baseMsg;
71          try
72          {
73              baseMsg = RESOURCE_BUNDLE.getString(key);
74          }
75          catch (MissingResourceException e)
76          {
77              return '!' + key + '!';
78          }
79  
80          return MessageFormat.format(baseMsg, parameters);
81      }
82  
83      /***
84       * Reads a message from the resource bundle and format it using java MessageFormat.
85       * @param key Message key.
86       * @param parameter single parameter to pass to MessageFormat.format()
87       * @return message String.
88       */
89      public static String getString(String key, Object parameter)
90      {
91          return getString(key, new Object[]{parameter});
92      }
93  }