001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/StringManager.java,v 1.2 2001/07/22 20:25:14 pier Exp $
003: * $Revision: 1.2 $
004: * $Date: 2001/07/22 20:25:14 $
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.catalina.util;
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: private StringManager(String packageName) {
112: String bundleName = packageName + ".LocalStrings";
113: bundle = ResourceBundle.getBundle(bundleName);
114: }
115:
116: /**
117: * Get a string from the underlying resource bundle.
118: *
119: * @param key
120: */
121:
122: public String getString(String key) {
123: if (key == null) {
124: String msg = "key is null";
125:
126: throw new NullPointerException(msg);
127: }
128:
129: String str = null;
130:
131: try {
132: str = bundle.getString(key);
133: } catch (MissingResourceException mre) {
134: str = "Cannot find message associated with key '" + key
135: + "'";
136: }
137:
138: return str;
139: }
140:
141: /**
142: * Get a string from the underlying resource bundle and format
143: * it with the given set of arguments.
144: *
145: * @param key
146: * @param args
147: */
148:
149: public String getString(String key, Object[] args) {
150: String iString = null;
151: String value = getString(key);
152:
153: // this check for the runtime exception is some pre 1.1.6
154: // VM's don't do an automatic toString() on the passed in
155: // objects and barf out
156:
157: try {
158: // ensure the arguments are not null so pre 1.2 VM's don't barf
159: Object nonNullArgs[] = args;
160: for (int i = 0; i < args.length; i++) {
161: if (args[i] == null) {
162: if (nonNullArgs == args)
163: nonNullArgs = (Object[]) args.clone();
164: nonNullArgs[i] = "null";
165: }
166: }
167:
168: iString = MessageFormat.format(value, nonNullArgs);
169: } catch (IllegalArgumentException iae) {
170: StringBuffer buf = new StringBuffer();
171: buf.append(value);
172: for (int i = 0; i < args.length; i++) {
173: buf.append(" arg[" + i + "]=" + args[i]);
174: }
175: iString = buf.toString();
176: }
177: return iString;
178: }
179:
180: /**
181: * Get a string from the underlying resource bundle and format it
182: * with the given object argument. This argument can of course be
183: * a String object.
184: *
185: * @param key
186: * @param arg
187: */
188:
189: public String getString(String key, Object arg) {
190: Object[] args = new Object[] { arg };
191: return getString(key, args);
192: }
193:
194: /**
195: * Get a string from the underlying resource bundle and format it
196: * with the given object arguments. These arguments can of course
197: * be String objects.
198: *
199: * @param key
200: * @param arg1
201: * @param arg2
202: */
203:
204: public String getString(String key, Object arg1, Object arg2) {
205: Object[] args = new Object[] { arg1, arg2 };
206: return getString(key, args);
207: }
208:
209: /**
210: * Get a string from the underlying resource bundle and format it
211: * with the given object arguments. These arguments can of course
212: * be String objects.
213: *
214: * @param key
215: * @param arg1
216: * @param arg2
217: * @param arg3
218: */
219:
220: public String getString(String key, Object arg1, Object arg2,
221: Object arg3) {
222: Object[] args = new Object[] { arg1, arg2, arg3 };
223: return getString(key, args);
224: }
225:
226: /**
227: * Get a string from the underlying resource bundle and format it
228: * with the given object arguments. These arguments can of course
229: * be String objects.
230: *
231: * @param key
232: * @param arg1
233: * @param arg2
234: * @param arg3
235: * @param arg4
236: */
237:
238: public String getString(String key, Object arg1, Object arg2,
239: Object arg3, Object arg4) {
240: Object[] args = new Object[] { arg1, arg2, arg3, arg4 };
241: return getString(key, args);
242: }
243:
244: // --------------------------------------------------------------
245: // STATIC SUPPORT METHODS
246: // --------------------------------------------------------------
247:
248: private static Hashtable managers = new Hashtable();
249:
250: /**
251: * Get the StringManager for a particular package. If a manager for
252: * a package already exists, it will be reused, else a new
253: * StringManager will be created and returned.
254: *
255: * @param packageName
256: */
257:
258: public synchronized static StringManager getManager(
259: String packageName) {
260: StringManager mgr = (StringManager) managers.get(packageName);
261: if (mgr == null) {
262: mgr = new StringManager(packageName);
263: managers.put(packageName, mgr);
264: }
265: return mgr;
266: }
267: }
|