KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xpath > common > MessageManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xpath.common;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28
29 /**
30  * An internationalization / localization helper class which reduces the bother
31  * of handling ResourceBundles and takes care of the common cases of message
32  * formating which otherwise require the creation of Object arrays and such. <p>
33  *
34  * The MessageManager operates on a package basis. One MessageManager per
35  * package can be created and accessed via the getManager method call. <p>
36  *
37  * The MessageManager will look for a ResourceBundle named by the package name
38  * given plus the suffix of "LocalStrings". In practice, this means that the
39  * localized information will be contained in a LocalStrings.properties file
40  * located in the package directory of the classpath. <p>
41  *
42  * Please see the documentation for java.util.ResourceBundle for more
43  * information.
44  *
45  * @author Tom Carter
46  * @version $Revision: 1.4 $
47  */

48 public class MessageManager {
49
50     // --------------------------------------------------------------
51
// STATIC SUPPORT METHODS
52
// --------------------------------------------------------------
53

54     /** Message managers. */
55     private static Hashtable JavaDoc managers = new Hashtable JavaDoc();
56
57     /** The ResourceBundle for this MessageManager. */
58     private ResourceBundle JavaDoc bundle;
59
60
61     /**
62      * Creates a new MessageManager for a given package. This is a private
63      * method and all access to it is arbitrated by the static getManager
64      * method call so that only one MessageManager per package will be created.
65      *
66      * @param packageName Name of package to create MessageManager for.
67      */

68     private MessageManager(String JavaDoc packageName) {
69         String JavaDoc bundleName = packageName + ".Bundle";
70         bundle = ResourceBundle.getBundle(bundleName);
71     }
72
73     /**
74      * Creates a new MessageManager for a given package. This is a private
75      * method and all access to it is arbitrated by the static getManager
76      * method call so that only one MessageManager per package will be created.
77      *
78      * @param baseName Base name of resource bundle file.
79      * @param clazz Class object for locating package.
80      */

81     private MessageManager(String JavaDoc baseName, Class JavaDoc clazz) {
82         bundle = ResourceBundle.getBundle(baseName, Locale.getDefault(), clazz.getClassLoader());
83     }
84
85     /** Finds package name for given class.
86      * @param clazz Class object.
87      */

88     private static String JavaDoc findName(Class JavaDoc clazz) {
89         String JavaDoc pref = clazz.getName();
90         int last = pref.lastIndexOf('.');
91         if (last >= 0) {
92             return pref.substring(0, last);
93         } else {
94             // base package, search for bundle
95
return ""; // NOI18N
96
}
97     }
98
99     /**
100      * Get the MessageManager for a particular package. If a manager for a
101      * package already exists, it will be reused, else a new MessageManager will
102      * be created and returned.
103      *
104      * @param clazz A class object in the package
105      * @return The message manager for the package
106      */

107     public static MessageManager getManager(Class JavaDoc clazz) {
108         String JavaDoc packageName = findName(clazz);
109         MessageManager mgr = (MessageManager) managers.get(packageName);
110         if (mgr == null) {
111             String JavaDoc bundleName = (packageName.length() == 0) ? "Bundle" : packageName + ".Bundle";
112             mgr = new MessageManager(bundleName, clazz);
113             managers.put(packageName, mgr);
114         }
115         return mgr;
116     }
117
118     /**
119      * Get the MessageManager for a particular package. If a manager for a
120      * package already exists, it will be reused, else a new MessageManager will
121      * be created and returned.
122      *
123      * @param packageName The name of the package
124      * @return The message manager for the package
125      */

126     public static synchronized MessageManager getManager(String JavaDoc packageName) {
127         MessageManager mgr = (MessageManager) managers.get(packageName);
128         if (mgr == null) {
129             mgr = new MessageManager(packageName);
130             managers.put(packageName, mgr);
131         }
132         return mgr;
133     }
134
135
136     /**
137      * Get a string from the underlying resource bundle.
138      *
139      * @param key The name of the string
140      * @return The string
141      */

142     public String JavaDoc getString(String JavaDoc key) {
143         if (key == null) {
144             String JavaDoc msg = "key is null";
145             throw new NullPointerException JavaDoc(msg);
146         }
147
148         String JavaDoc str = null;
149
150         try {
151             str = bundle.getString(key);
152         } catch (MissingResourceException JavaDoc mre) {
153             str = "Cannot find message associated with key '" + key + "'";
154         }
155
156         return str;
157     }
158
159
160     /**
161      * Get a string from the underlying resource bundle and format it with the
162      * given set of arguments.
163      *
164      * @param key The name of the string
165      * @param args Argument to interpolate into the format string
166      * @return The formatted string
167      */

168     public String JavaDoc getString(String JavaDoc key, Object JavaDoc[] args) {
169         String JavaDoc iString = null;
170         String JavaDoc value = getString(key);
171
172         try {
173             Object JavaDoc nonNullArgs[] = args;
174             for (int i = 0; i < args.length; i++) {
175                 if (args[i] == null) {
176                     if (nonNullArgs == args) {
177                         nonNullArgs = (Object JavaDoc[]) args.clone();
178                     }
179                     nonNullArgs[i] = "null";
180                 }
181             }
182
183             iString = MessageFormat.format(value, nonNullArgs);
184         } catch (IllegalArgumentException JavaDoc iae) {
185             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
186             buf.append(value);
187             for (int i = 0; i < args.length; i++) {
188                 buf.append(" arg[" + i + "]=" + args[i]);
189             }
190             iString = buf.toString();
191         }
192
193         return iString;
194     }
195
196
197     /**
198      * Get a string from the underlying resource bundle and format it with the
199      * given object argument. This argument can of course be a String object.
200      *
201      * @param key The name of the string
202      * @param arg The string to format with
203      * @return The formatted string
204      */

205     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg) {
206         Object JavaDoc[] args = new Object JavaDoc[]{arg};
207         return getString(key, args);
208     }
209
210
211     /**
212      * Get a string from the underlying resource bundle and format it with the
213      * given object arguments. These arguments can of course be String objects.
214      *
215      * @param key The name of the string
216      * @param arg1 The first argument to interpolate into the string
217      * @param arg2 The second argument to interpolate into the string
218      * @return The formatted string
219      */

220     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2) {
221         Object JavaDoc[] args = new Object JavaDoc[]{arg1, arg2};
222         return getString(key, args);
223     }
224
225
226     /**
227      * Get a string from the underlying resource bundle and format it with the
228      * given object arguments. These arguments can of course be String objects.
229      *
230      * @param key The name of the string
231      * @param arg1 The first argument to interpolate into the string
232      * @param arg2 The second argument to interpolate into the string
233      * @param arg3 The third argument to interpolate into the string
234      * @return The formatted string
235      */

236
237     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2,
238                                 Object JavaDoc arg3) {
239         Object JavaDoc[] args = new Object JavaDoc[]{arg1, arg2, arg3};
240         return getString(key, args);
241     }
242
243
244     /**
245      * Get a string from the underlying resource bundle and format it with the
246      * given object arguments. These arguments can of course be String objects.
247      *
248      * @param key The name of the string
249      * @param arg1 The first argument to interpolate into the string
250      * @param arg2 The second argument to interpolate into the string
251      * @param arg3 The third argument to interpolate into the string
252      * @param arg4 The fourth argument to interpolate into the string
253      * @return The formatted string
254      */

255     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3,
256                                 Object JavaDoc arg4) {
257         Object JavaDoc[] args = new Object JavaDoc[]{arg1, arg2, arg3, arg4};
258         return getString(key, args);
259     }
260
261 }
262
Popular Tags