001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.xpath.common;
043:
044: import java.text.MessageFormat;
045: import java.util.Hashtable;
046: import java.util.Locale;
047: import java.util.MissingResourceException;
048: import java.util.ResourceBundle;
049:
050: /**
051: * An internationalization / localization helper class which reduces the bother
052: * of handling ResourceBundles and takes care of the common cases of message
053: * formating which otherwise require the creation of Object arrays and such. <p>
054: *
055: * The MessageManager operates on a package basis. One MessageManager per
056: * package can be created and accessed via the getManager method call. <p>
057: *
058: * The MessageManager will look for a ResourceBundle named by the package name
059: * given plus the suffix of "LocalStrings". In practice, this means that the
060: * localized information will be contained in a LocalStrings.properties file
061: * located in the package directory of the classpath. <p>
062: *
063: * Please see the documentation for java.util.ResourceBundle for more
064: * information.
065: *
066: * @author Tom Carter
067: * @version
068: */
069: public class MessageManager {
070:
071: // --------------------------------------------------------------
072: // STATIC SUPPORT METHODS
073: // --------------------------------------------------------------
074:
075: /** Message managers. */
076: private static Hashtable managers = new Hashtable();
077:
078: /** The ResourceBundle for this MessageManager. */
079: private ResourceBundle bundle;
080:
081: /**
082: * Creates a new MessageManager for a given package. This is a private
083: * method and all access to it is arbitrated by the static getManager
084: * method call so that only one MessageManager per package will be created.
085: *
086: * @param packageName Name of package to create MessageManager for.
087: */
088: private MessageManager(String packageName) {
089: String bundleName = packageName + ".Bundle";
090: bundle = ResourceBundle.getBundle(bundleName);
091: }
092:
093: /**
094: * Creates a new MessageManager for a given package. This is a private
095: * method and all access to it is arbitrated by the static getManager
096: * method call so that only one MessageManager per package will be created.
097: *
098: * @param baseName Base name of resource bundle file.
099: * @param clazz Class object for locating package.
100: */
101: private MessageManager(String baseName, Class clazz) {
102: bundle = ResourceBundle.getBundle(baseName,
103: Locale.getDefault(), clazz.getClassLoader());
104: }
105:
106: /** Finds package name for given class.
107: * @param clazz Class object.
108: */
109: private static String findName(Class clazz) {
110: String pref = clazz.getName();
111: int last = pref.lastIndexOf('.');
112: if (last >= 0) {
113: return pref.substring(0, last);
114: } else {
115: // base package, search for bundle
116: return ""; // NOI18N
117: }
118: }
119:
120: /**
121: * Get the MessageManager for a particular package. If a manager for a
122: * package already exists, it will be reused, else a new MessageManager will
123: * be created and returned.
124: *
125: * @param clazz A class object in the package
126: * @return The message manager for the package
127: */
128: public static MessageManager getManager(Class clazz) {
129: String packageName = findName(clazz);
130: MessageManager mgr = (MessageManager) managers.get(packageName);
131: if (mgr == null) {
132: String bundleName = (packageName.length() == 0) ? "Bundle"
133: : packageName + ".Bundle";
134: mgr = new MessageManager(bundleName, clazz);
135: managers.put(packageName, mgr);
136: }
137: return mgr;
138: }
139:
140: /**
141: * Get the MessageManager for a particular package. If a manager for a
142: * package already exists, it will be reused, else a new MessageManager will
143: * be created and returned.
144: *
145: * @param packageName The name of the package
146: * @return The message manager for the package
147: */
148: public static synchronized MessageManager getManager(
149: String packageName) {
150: MessageManager mgr = (MessageManager) managers.get(packageName);
151: if (mgr == null) {
152: mgr = new MessageManager(packageName);
153: managers.put(packageName, mgr);
154: }
155: return mgr;
156: }
157:
158: /**
159: * Get a string from the underlying resource bundle.
160: *
161: * @param key The name of the string
162: * @return The string
163: */
164: public String getString(String key) {
165: if (key == null) {
166: String msg = "key is null";
167: throw new NullPointerException(msg);
168: }
169:
170: String str = null;
171:
172: try {
173: str = bundle.getString(key);
174: } catch (MissingResourceException mre) {
175: str = "Cannot find message associated with key '" + key
176: + "'";
177: }
178:
179: return str;
180: }
181:
182: /**
183: * Get a string from the underlying resource bundle and format it with the
184: * given set of arguments.
185: *
186: * @param key The name of the string
187: * @param args Argument to interpolate into the format string
188: * @return The formatted string
189: */
190: public String getString(String key, Object[] args) {
191: String iString = null;
192: String value = getString(key);
193:
194: try {
195: Object nonNullArgs[] = args;
196: for (int i = 0; i < args.length; i++) {
197: if (args[i] == null) {
198: if (nonNullArgs == args) {
199: nonNullArgs = (Object[]) args.clone();
200: }
201: nonNullArgs[i] = "null";
202: }
203: }
204:
205: iString = MessageFormat.format(value, nonNullArgs);
206: } catch (IllegalArgumentException iae) {
207: StringBuffer buf = new StringBuffer();
208: buf.append(value);
209: for (int i = 0; i < args.length; i++) {
210: buf.append(" arg[" + i + "]=" + args[i]);
211: }
212: iString = buf.toString();
213: }
214:
215: return iString;
216: }
217:
218: /**
219: * Get a string from the underlying resource bundle and format it with the
220: * given object argument. This argument can of course be a String object.
221: *
222: * @param key The name of the string
223: * @param arg The string to format with
224: * @return The formatted string
225: */
226: public String getString(String key, Object arg) {
227: Object[] args = new Object[] { arg };
228: return getString(key, args);
229: }
230:
231: /**
232: * Get a string from the underlying resource bundle and format it with the
233: * given object arguments. These arguments can of course be String objects.
234: *
235: * @param key The name of the string
236: * @param arg1 The first argument to interpolate into the string
237: * @param arg2 The second argument to interpolate into the string
238: * @return The formatted string
239: */
240: public String getString(String key, Object arg1, Object arg2) {
241: Object[] args = new Object[] { arg1, arg2 };
242: return getString(key, args);
243: }
244:
245: /**
246: * Get a string from the underlying resource bundle and format it with the
247: * given object arguments. These arguments can of course be String objects.
248: *
249: * @param key The name of the string
250: * @param arg1 The first argument to interpolate into the string
251: * @param arg2 The second argument to interpolate into the string
252: * @param arg3 The third argument to interpolate into the string
253: * @return The formatted string
254: */
255:
256: public String getString(String key, Object arg1, Object arg2,
257: Object arg3) {
258: Object[] args = new Object[] { arg1, arg2, arg3 };
259: return getString(key, args);
260: }
261:
262: /**
263: * Get a string from the underlying resource bundle and format it with the
264: * given object arguments. These arguments can of course be String objects.
265: *
266: * @param key The name of the string
267: * @param arg1 The first argument to interpolate into the string
268: * @param arg2 The second argument to interpolate into the string
269: * @param arg3 The third argument to interpolate into the string
270: * @param arg4 The fourth argument to interpolate into the string
271: * @return The formatted string
272: */
273: public String getString(String key, Object arg1, Object arg2,
274: Object arg3, Object arg4) {
275: Object[] args = new Object[] { arg1, arg2, arg3, arg4 };
276: return getString(key, args);
277: }
278:
279: }
|