KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > StringManager


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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

48
49 public class StringManager {
50
51     /**
52      * The ResourceBundle for this StringManager.
53      */

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

65
66     private StringManager(String JavaDoc packageName) {
67     String JavaDoc bundleName = packageName + ".LocalStrings";
68     bundle = ResourceBundle.getBundle(bundleName);
69     }
70
71     /**
72      * Get a string from the underlying resource bundle.
73      *
74      * @param key
75      */

76     
77     public String JavaDoc getString(String JavaDoc key) {
78         if (key == null) {
79             String JavaDoc msg = "key is null";
80
81             throw new NullPointerException JavaDoc(msg);
82         }
83
84         String JavaDoc str = null;
85
86         try {
87         str = bundle.getString(key);
88         } catch (MissingResourceException JavaDoc mre) {
89             str = "Cannot find message associated with key '" + key + "'";
90         }
91
92         return str;
93     }
94
95     /**
96      * Get a string from the underlying resource bundle and format
97      * it with the given set of arguments.
98      *
99      * @param key
100      * @param args
101      */

102
103     public String JavaDoc getString(String JavaDoc key, Object JavaDoc[] args) {
104     String JavaDoc iString = null;
105         String JavaDoc value = getString(key);
106
107     // this check for the runtime exception is some pre 1.1.6
108
// VM's don't do an automatic toString() on the passed in
109
// objects and barf out
110

111     try {
112             // ensure the arguments are not null so pre 1.2 VM's don't barf
113
Object JavaDoc nonNullArgs[] = args;
114             for (int i=0; i<args.length; i++) {
115         if (args[i] == null) {
116             if (nonNullArgs==args) nonNullArgs=(Object JavaDoc[])args.clone();
117             nonNullArgs[i] = "null";
118         }
119         }
120  
121             iString = MessageFormat.format(value, nonNullArgs);
122     } catch (IllegalArgumentException JavaDoc iae) {
123         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
124         buf.append(value);
125         for (int i = 0; i < args.length; i++) {
126         buf.append(" arg[" + i + "]=" + args[i]);
127         }
128         iString = buf.toString();
129     }
130     return iString;
131     }
132
133     /**
134      * Get a string from the underlying resource bundle and format it
135      * with the given object argument. This argument can of course be
136      * a String object.
137      *
138      * @param key
139      * @param arg
140      */

141
142     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg) {
143     Object JavaDoc[] args = new Object JavaDoc[] {arg};
144     return getString(key, args);
145     }
146
147     /**
148      * Get a string from the underlying resource bundle and format it
149      * with the given object arguments. These arguments can of course
150      * be String objects.
151      *
152      * @param key
153      * @param arg1
154      * @param arg2
155      */

156
157     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2) {
158     Object JavaDoc[] args = new Object JavaDoc[] {arg1, arg2};
159     return getString(key, args);
160     }
161     
162     /**
163      * Get a string from the underlying resource bundle and format it
164      * with the given object arguments. These arguments can of course
165      * be String objects.
166      *
167      * @param key
168      * @param arg1
169      * @param arg2
170      * @param arg3
171      */

172
173     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2,
174                 Object JavaDoc arg3) {
175     Object JavaDoc[] args = new Object JavaDoc[] {arg1, arg2, arg3};
176     return getString(key, args);
177     }
178     
179     /**
180      * Get a string from the underlying resource bundle and format it
181      * with the given object arguments. These arguments can of course
182      * be String objects.
183      *
184      * @param key
185      * @param arg1
186      * @param arg2
187      * @param arg3
188      * @param arg4
189      */

190
191     public String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2,
192                 Object JavaDoc arg3, Object JavaDoc arg4) {
193     Object JavaDoc[] args = new Object JavaDoc[] {arg1, arg2, arg3, arg4};
194     return getString(key, args);
195     }
196     // --------------------------------------------------------------
197
// STATIC SUPPORT METHODS
198
// --------------------------------------------------------------
199

200     private static Hashtable JavaDoc managers = new Hashtable JavaDoc();
201
202     /**
203      * Get the StringManager for a particular package. If a manager for
204      * a package already exists, it will be reused, else a new
205      * StringManager will be created and returned.
206      *
207      * @param packageName
208      */

209
210     public synchronized static StringManager getManager(String JavaDoc packageName) {
211     StringManager mgr = (StringManager)managers.get(packageName);
212     if (mgr == null) {
213         mgr = new StringManager(packageName);
214         managers.put(packageName, mgr);
215     }
216     return mgr;
217     }
218 }
219
Popular Tags