001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/StringManager.java,v 1.1 2000/11/02 06:14:16 remm Exp $
003: * $Revision: 1.1 $
004: * $Date: 2000/11/02 06:14:16 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.naming;
065:
066: import java.text.MessageFormat;
067: import java.util.Hashtable;
068: import java.util.Locale;
069: import java.util.MissingResourceException;
070: import java.util.ResourceBundle;
071:
072: /**
073: * An internationalization / localization helper class which reduces
074: * the bother of handling ResourceBundles and takes care of the
075: * common cases of message formating which otherwise require the
076: * creation of Object arrays and such.
077: *
078: * <p>The StringManager operates on a package basis. One StringManager
079: * per package can be created and accessed via the getManager method
080: * call.
081: *
082: * <p>The StringManager will look for a ResourceBundle named by
083: * the package name given plus the suffix of "LocalStrings". In
084: * practice, this means that the localized information will be contained
085: * in a LocalStrings.properties file located in the package
086: * directory of the classpath.
087: *
088: * <p>Please see the documentation for java.util.ResourceBundle for
089: * more information.
090: *
091: * @author James Duncan Davidson [duncan@eng.sun.com]
092: * @author James Todd [gonzo@eng.sun.com]
093: */
094:
095: public class StringManager {
096:
097: /**
098: * The ResourceBundle for this StringManager.
099: */
100:
101: private ResourceBundle bundle;
102:
103: /**
104: * Creates a new StringManager for a given package. This is a
105: * private method and all access to it is arbitrated by the
106: * static getManager method call so that only one StringManager
107: * per package will be created.
108: *
109: * @param packageName Name of package to create StringManager for.
110: */
111:
112: private StringManager(String packageName) {
113: String bundleName = packageName + ".LocalStrings";
114: bundle = ResourceBundle.getBundle(bundleName);
115: }
116:
117: /**
118: * Get a string from the underlying resource bundle.
119: *
120: * @param key
121: */
122:
123: public String getString(String key) {
124: if (key == null) {
125: String msg = "key is null";
126:
127: throw new NullPointerException(msg);
128: }
129:
130: String str = null;
131:
132: try {
133: str = bundle.getString(key);
134: } catch (MissingResourceException mre) {
135: str = "Cannot find message associated with key '" + key
136: + "'";
137: }
138:
139: return str;
140: }
141:
142: /**
143: * Get a string from the underlying resource bundle and format
144: * it with the given set of arguments.
145: *
146: * @param key
147: * @param args
148: */
149:
150: public String getString(String key, Object[] args) {
151: String iString = null;
152: String value = getString(key);
153:
154: // this check for the runtime exception is some pre 1.1.6
155: // VM's don't do an automatic toString() on the passed in
156: // objects and barf out
157:
158: try {
159: // ensure the arguments are not null so pre 1.2 VM's don't barf
160: Object nonNullArgs[] = args;
161: for (int i = 0; i < args.length; i++) {
162: if (args[i] == null) {
163: if (nonNullArgs == args)
164: nonNullArgs = (Object[]) args.clone();
165: nonNullArgs[i] = "null";
166: }
167: }
168:
169: iString = MessageFormat.format(value, nonNullArgs);
170: } catch (IllegalArgumentException iae) {
171: StringBuffer buf = new StringBuffer();
172: buf.append(value);
173: for (int i = 0; i < args.length; i++) {
174: buf.append(" arg[" + i + "]=" + args[i]);
175: }
176: iString = buf.toString();
177: }
178: return iString;
179: }
180:
181: /**
182: * Get a string from the underlying resource bundle and format it
183: * with the given object argument. This argument can of course be
184: * a String object.
185: *
186: * @param key
187: * @param arg
188: */
189:
190: public String getString(String key, Object arg) {
191: Object[] args = new Object[] { arg };
192: return getString(key, args);
193: }
194:
195: /**
196: * Get a string from the underlying resource bundle and format it
197: * with the given object arguments. These arguments can of course
198: * be String objects.
199: *
200: * @param key
201: * @param arg1
202: * @param arg2
203: */
204:
205: public String getString(String key, Object arg1, Object arg2) {
206: Object[] args = new Object[] { arg1, arg2 };
207: return getString(key, args);
208: }
209:
210: /**
211: * Get a string from the underlying resource bundle and format it
212: * with the given object arguments. These arguments can of course
213: * be String objects.
214: *
215: * @param key
216: * @param arg1
217: * @param arg2
218: * @param arg3
219: */
220:
221: public String getString(String key, Object arg1, Object arg2,
222: Object arg3) {
223: Object[] args = new Object[] { arg1, arg2, arg3 };
224: return getString(key, args);
225: }
226:
227: /**
228: * Get a string from the underlying resource bundle and format it
229: * with the given object arguments. These arguments can of course
230: * be String objects.
231: *
232: * @param key
233: * @param arg1
234: * @param arg2
235: * @param arg3
236: * @param arg4
237: */
238:
239: public String getString(String key, Object arg1, Object arg2,
240: Object arg3, Object arg4) {
241: Object[] args = new Object[] { arg1, arg2, arg3, arg4 };
242: return getString(key, args);
243: }
244:
245: // --------------------------------------------------------------
246: // STATIC SUPPORT METHODS
247: // --------------------------------------------------------------
248:
249: private static Hashtable managers = new Hashtable();
250:
251: /**
252: * Get the StringManager for a particular package. If a manager for
253: * a package already exists, it will be reused, else a new
254: * StringManager will be created and returned.
255: *
256: * @param packageName
257: */
258:
259: public synchronized static StringManager getManager(
260: String packageName) {
261: StringManager mgr = (StringManager) managers.get(packageName);
262: if (mgr == null) {
263: mgr = new StringManager(packageName);
264: managers.put(packageName, mgr);
265: }
266: return mgr;
267: }
268: }
|