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: package org.apache.pluto.util;
018:
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.commons.logging.Log;
021:
022: import java.net.URLClassLoader;
023: import java.text.MessageFormat;
024: import java.util.Hashtable;
025: import java.util.Locale;
026: import java.util.MissingResourceException;
027: import java.util.ResourceBundle;
028:
029: /**
030: * An internationalization / localization helper class which reduces the bother
031: * of handling ResourceBundles and takes care of the common cases of message
032: * formating which otherwise require the creation of Object arrays and such.
033: * <p/>
034: * <p>The StringManager operates on a package basis. One StringManager per
035: * package can be created and accessed via the getManager method call.
036: * <p/>
037: * <p>The StringManager will look for a ResourceBundle named by the package name
038: * given plus the suffix of "LocalStrings". In practice, this means that the
039: * localized information will be contained in a LocalStrings.properties file
040: * located in the package directory of the classpath.
041: * <p/>
042: * <p>Please see the documentation for java.util.ResourceBundle for more
043: * information.
044: */
045: public class StringManager {
046: private static final Log LOG = LogFactory
047: .getLog(StringManager.class);
048:
049: /**
050: * The ResourceBundle for this StringManager.
051: */
052:
053: private ResourceBundle bundle;
054:
055: /**
056: * The package this StringManager belongs to.
057: */
058: private final String packageName;
059:
060: /**
061: * Creates a new StringManager for a given package. This is a private method
062: * and all access to it is arbitrated by the static getManager method call
063: * so that only one StringManager per package will be created.
064: * @param packageName Name of package to create StringManager for.
065: */
066:
067: private StringManager(String packageName) {
068: if (LOG.isDebugEnabled()) {
069: LOG.debug("String Manager Created for package: "
070: + packageName);
071: }
072: this .packageName = packageName;
073: String bundleName = packageName + ".LocalStrings";
074: try {
075: bundle = ResourceBundle.getBundle(bundleName);
076: return;
077: } catch (MissingResourceException ex) {
078: // Try from the current loader ( that's the case for trusted apps )
079: ClassLoader cl = Thread.currentThread()
080: .getContextClassLoader();
081: if (cl != null) {
082: try {
083: bundle = ResourceBundle.getBundle(bundleName,
084: Locale.getDefault(), cl);
085: return;
086: } catch (MissingResourceException ex2) {
087: }
088: }
089: if (cl == null) {
090: cl = this .getClass().getClassLoader();
091: }
092:
093: System.out.println("Can't find resource " + bundleName
094: + " " + cl);
095: if (cl instanceof URLClassLoader) {
096: System.out.println(((URLClassLoader) cl).getURLs());
097: }
098: }
099: }
100:
101: /**
102: * Get a string from the underlying resource bundle.
103: * @param key The resource name
104: */
105: public String getString(String key) {
106: return MessageFormat.format(getStringInternal(key), null);
107: }
108:
109: protected String getStringInternal(String key) {
110: if (key == null) {
111: String msg = "key is null";
112:
113: throw new NullPointerException(msg);
114: }
115:
116: String str = null;
117:
118: if (bundle == null) {
119: return key;
120: }
121: try {
122: str = bundle.getString(key);
123: } catch (MissingResourceException mre) {
124: String name = null;
125: if (packageName == null) {
126: name = "unknown";
127: } else {
128: name = packageName;
129: }
130: str = "Cannot find message in the ResourceBundle associated with key '"
131: + key + "' " + "(package " + name + ")";
132: LOG.warn(str, mre);
133: }
134:
135: return str;
136: }
137:
138: /**
139: * Get a string from the underlying resource bundle and format it with the
140: * given set of arguments.
141: * @param key The resource name
142: * @param args Formatting directives
143: */
144:
145: public String getString(String key, Object[] args) {
146: String iString = null;
147: String value = getStringInternal(key);
148:
149: // this check for the runtime exception is some pre 1.1.6
150: // VM's don't do an automatic toString() on the passed in
151: // objects and barf out
152:
153: try {
154: // ensure the arguments are not null so pre 1.2 VM's don't barf
155: Object nonNullArgs[] = args;
156: for (int i = 0; i < args.length; i++) {
157: if (args[i] == null) {
158: if (nonNullArgs == args) {
159: nonNullArgs = (Object[]) args.clone();
160: }
161: nonNullArgs[i] = "null";
162: }
163: }
164:
165: iString = MessageFormat.format(value, nonNullArgs);
166: } catch (IllegalArgumentException iae) {
167: StringBuffer buf = new StringBuffer();
168: buf.append(value);
169: for (int i = 0; i < args.length; i++) {
170: buf.append(" arg[").append(i).append("]=").append(
171: args[i]);
172: }
173: iString = buf.toString();
174: }
175: return iString;
176: }
177:
178: /**
179: * Get a string from the underlying resource bundle and format it with the
180: * given object argument. This argument can of course be a String object.
181: * @param key The resource name
182: * @param arg Formatting directive
183: */
184:
185: public String getString(String key, Object arg) {
186: Object[] args = new Object[] { arg };
187: return getString(key, args);
188: }
189:
190: /**
191: * Get a string from the underlying resource bundle and format it with the
192: * given object arguments. These arguments can of course be String objects.
193: * @param key The resource name
194: * @param arg1 Formatting directive
195: * @param arg2 Formatting directive
196: */
197:
198: public String getString(String key, Object arg1, Object arg2) {
199: Object[] args = new Object[] { arg1, arg2 };
200: return getString(key, args);
201: }
202:
203: /**
204: * Get a string from the underlying resource bundle and format it with the
205: * given object arguments. These arguments can of course be String objects.
206: * @param key The resource name
207: * @param arg1 Formatting directive
208: * @param arg2 Formatting directive
209: * @param arg3 Formatting directive
210: */
211:
212: public String getString(String key, Object arg1, Object arg2,
213: Object arg3) {
214: Object[] args = new Object[] { arg1, arg2, arg3 };
215: return getString(key, args);
216: }
217:
218: /**
219: * Get a string from the underlying resource bundle and format it with the
220: * given object arguments. These arguments can of course be String objects.
221: * @param key The resource name
222: * @param arg1 Formatting directive
223: * @param arg2 Formatting directive
224: * @param arg3 Formatting directive
225: * @param arg4 Formatting directive
226: */
227:
228: public String getString(String key, Object arg1, Object arg2,
229: Object arg3, Object arg4) {
230: Object[] args = new Object[] { arg1, arg2, arg3, arg4 };
231: return getString(key, args);
232: }
233:
234: // --------------------------------------------------------------
235: // STATIC SUPPORT METHODS
236: // --------------------------------------------------------------
237:
238: private static Hashtable managers = new Hashtable();
239:
240: /**
241: * Get the StringManager for a particular package. If a manager for a
242: * package already exists, it will be reused, else a new StringManager will
243: * be created and returned.
244: * @param packageName The package name
245: */
246:
247: public synchronized static StringManager getManager(
248: String packageName) {
249: StringManager mgr = (StringManager) managers.get(packageName);
250:
251: if (mgr == null) {
252: mgr = new StringManager(packageName);
253: managers.put(packageName, mgr);
254: }
255: return mgr;
256: }
257: }
|