View Javadoc

1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.resource;
4   
5   import java.text.MessageFormat;
6   import java.util.Locale;
7   import java.util.MissingResourceException;
8   import java.util.ResourceBundle;
9   
10  
11  /***
12   * Class to manage access to resource files.
13   *
14   * @author Chad Woolley
15   * @version $Revision: 1.1 $
16   *
17   * @todo i18n.
18   */
19  public class ResourceManager {
20      private Locale locale = null;
21      private ResourceBundle resourceBundle = null;
22      private String country = null;
23      private String language = null;
24      private String resourceFileName = null;
25      private boolean initialized = false;
26  
27      /***
28       * Constructor which takes resource file name, language, and country.
29       *
30       * @param resourceFileName the name of the resource file
31       * @param language the language of the locale
32       * @param country the country of the locale
33       */
34      public ResourceManager(String resourceFileName, String language,
35          String country) {
36          super();
37          this.language = language;
38          this.country = country;
39          this.resourceFileName = resourceFileName;
40          locale = new Locale(language, country);
41      }
42  
43      /***
44       * Gets a string from the resource file.
45       *
46       * @param key the key to look up in the resource file
47       *
48       * @return The value that was found
49       *
50       * @throws RuntimeException if the key isn't in the resource file
51       */
52      public String getString(String key) {
53          init();
54  
55          String value = null;
56  
57          try {
58              value = resourceBundle.getString(key);
59          } catch (MissingResourceException missingResourceException) {
60              throw new RuntimeException("The resource for " + key
61                  + "was not found in the " + resourceFileName + " resource file");
62          }
63  
64          return value;
65      }
66  
67      /***
68       * Gets a string from the resource file, substituting arguments.
69       *
70       * @param key the key to look up in the resource file
71       * @param arguments the arguments to substitute
72       *
73       * @return The value that was found, with arguments substituted
74       */
75      public String getString(String key, String[] arguments) {
76          String value = getString(key);
77  
78          return MessageFormat.format(value, arguments);
79      }
80  
81      /***
82       * Gets a string from the resource file, substituting arguments.
83       *
84       * @param key the key to look up in the resource file
85       * @param argument1 the first argument to substitute
86       * @param argument2 the second argument to substitute
87       *
88       * @return The value that was found, with arguments substituted
89       */
90      public String getString(String key, String argument1, String argument2) {
91          return getString(key, new String[] {argument1, argument2});
92      }
93  
94      /***
95       * Gets a string from the resource file, substituting arguments.
96       *
97       * @param key the key to look up in the resource file
98       * @param argument1 the first argument to substitute
99       * @param argument2 the second argument to substitute
100      * @param argument3 the third argument to substitute
101      *
102      * @return The value that was found, with arguments substituted
103      */
104     public String getString(String key, String argument1, String argument2,
105         String argument3) {
106         return getString(key, new String[] {argument1, argument2, argument3});
107     }
108 
109     /***
110      * Gets a string from the resource file, substituting arguments.
111      *
112      * @param key the key to look up in the resource file
113      * @param argument1 the first argument to substitute
114      * @param argument2 the second argument to substitute
115      * @param argument3 the third argument to substitute
116      * @param argument4 the fourth argument to substitute
117      *
118      * @return The value that was found, with arguments substituted
119      */
120     public String getString(String key, String argument1, String argument2,
121         String argument3, String argument4) {
122         return getString(key,
123             new String[] {argument1, argument2, argument3, argument4});
124     }
125 
126     /***
127      * Gets a string from the resource file, substituting an argument.
128      *
129      * @param key the key to look up in the resource file
130      * @param argument1 the single argument to substitute
131      *
132      * @return The value that was found, with argument substituted
133      */
134     public String getString(String key, String argument1) {
135         return getString(key, new String[] {argument1});
136     }
137 
138     /***
139      * Initialize by reading the bundle.
140      */
141     private void init() {
142         if (!initialized) {
143             ClassLoader classLoader = this.getClass().getClassLoader();
144 
145             resourceBundle =
146                 ResourceBundle.getBundle(resourceFileName, locale, classLoader);
147 
148             initialized = true;
149         }
150     }
151 }