001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.naming;
019:
020: import java.text.MessageFormat;
021: import java.util.Hashtable;
022: import java.util.MissingResourceException;
023: import java.util.ResourceBundle;
024:
025: /**
026: * An internationalization / localization helper class which reduces
027: * the bother of handling ResourceBundles and takes care of the
028: * common cases of message formating which otherwise require the
029: * creation of Object arrays and such.
030: *
031: * <p>The StringManager operates on a package basis. One StringManager
032: * per package can be created and accessed via the getManager method
033: * call.
034: *
035: * <p>The StringManager will look for a ResourceBundle named by
036: * the package name given plus the suffix of "LocalStrings". In
037: * practice, this means that the localized information will be contained
038: * in a LocalStrings.properties file located in the package
039: * directory of the classpath.
040: *
041: * <p>Please see the documentation for java.util.ResourceBundle for
042: * more information.
043: *
044: * @author James Duncan Davidson [duncan@eng.sun.com]
045: * @author James Todd [gonzo@eng.sun.com]
046: */
047:
048: public class StringManager {
049:
050: /**
051: * The ResourceBundle for this StringManager.
052: */
053:
054: private ResourceBundle bundle;
055:
056: /**
057: * Creates a new StringManager for a given package. This is a
058: * private method and all access to it is arbitrated by the
059: * static getManager method call so that only one StringManager
060: * per package will be created.
061: *
062: * @param packageName Name of package to create StringManager for.
063: */
064:
065: private StringManager(String packageName) {
066: String bundleName = packageName + ".LocalStrings";
067: bundle = ResourceBundle.getBundle(bundleName);
068: }
069:
070: /**
071: * Get a string from the underlying resource bundle.
072: *
073: * @param key
074: */
075:
076: public String getString(String key) {
077: if (key == null) {
078: String msg = "key is null";
079:
080: throw new NullPointerException(msg);
081: }
082:
083: String str = null;
084:
085: try {
086: str = bundle.getString(key);
087: } catch (MissingResourceException mre) {
088: str = "Cannot find message associated with key '" + key
089: + "'";
090: }
091:
092: return str;
093: }
094:
095: /**
096: * Get a string from the underlying resource bundle and format
097: * it with the given set of arguments.
098: *
099: * @param key
100: * @param args
101: */
102:
103: public String getString(String key, Object[] args) {
104: String iString = null;
105: String 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 nonNullArgs[] = args;
114: for (int i = 0; i < args.length; i++) {
115: if (args[i] == null) {
116: if (nonNullArgs == args)
117: nonNullArgs = (Object[]) args.clone();
118: nonNullArgs[i] = "null";
119: }
120: }
121:
122: iString = MessageFormat.format(value, nonNullArgs);
123: } catch (IllegalArgumentException iae) {
124: StringBuffer buf = new StringBuffer();
125: buf.append(value);
126: for (int i = 0; i < args.length; i++) {
127: buf.append(" arg[" + i + "]=" + args[i]);
128: }
129: iString = buf.toString();
130: }
131: return iString;
132: }
133:
134: /**
135: * Get a string from the underlying resource bundle and format it
136: * with the given object argument. This argument can of course be
137: * a String object.
138: *
139: * @param key
140: * @param arg
141: */
142:
143: public String getString(String key, Object arg) {
144: Object[] args = new Object[] { arg };
145: return getString(key, args);
146: }
147:
148: /**
149: * Get a string from the underlying resource bundle and format it
150: * with the given object arguments. These arguments can of course
151: * be String objects.
152: *
153: * @param key
154: * @param arg1
155: * @param arg2
156: */
157:
158: public String getString(String key, Object arg1, Object arg2) {
159: Object[] args = new Object[] { arg1, arg2 };
160: return getString(key, args);
161: }
162:
163: /**
164: * Get a string from the underlying resource bundle and format it
165: * with the given object arguments. These arguments can of course
166: * be String objects.
167: *
168: * @param key
169: * @param arg1
170: * @param arg2
171: * @param arg3
172: */
173:
174: public String getString(String key, Object arg1, Object arg2,
175: Object arg3) {
176: Object[] args = new Object[] { arg1, arg2, arg3 };
177: return getString(key, args);
178: }
179:
180: /**
181: * Get a string from the underlying resource bundle and format it
182: * with the given object arguments. These arguments can of course
183: * be String objects.
184: *
185: * @param key
186: * @param arg1
187: * @param arg2
188: * @param arg3
189: * @param arg4
190: */
191:
192: public String getString(String key, Object arg1, Object arg2,
193: Object arg3, Object arg4) {
194: Object[] args = new Object[] { arg1, arg2, arg3, arg4 };
195: return getString(key, args);
196: }
197:
198: // --------------------------------------------------------------
199: // STATIC SUPPORT METHODS
200: // --------------------------------------------------------------
201:
202: private static Hashtable managers = new Hashtable();
203:
204: /**
205: * Get the StringManager for a particular package. If a manager for
206: * a package already exists, it will be reused, else a new
207: * StringManager will be created and returned.
208: *
209: * @param packageName
210: */
211:
212: public synchronized static StringManager getManager(
213: String packageName) {
214: StringManager mgr = (StringManager) managers.get(packageName);
215: if (mgr == null) {
216: mgr = new StringManager(packageName);
217: managers.put(packageName, mgr);
218: }
219: return mgr;
220: }
221: }
|