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: package org.netbeans.modules.sql.framework.common.utils;
042:
043: import java.text.MessageFormat;
044: import java.util.HashMap;
045: import java.util.MissingResourceException;
046: import java.util.ResourceBundle;
047:
048: /**
049: * An internationalization / localization helper class which reduces the bother of
050: * handling ResourceBundles and takes care of the sql framework cases of message formating
051: * which otherwise require the creation of Object arrays and such.
052: * <p>
053: * The MessageManager operates on a package basis. One MessageManager per package can be
054: * created and accessed via the getManager method call.
055: * <p>
056: * The MessageManager will look for a ResourceBundle named by the package name given plus
057: * the suffix of "LocalStrings". In practice, this means that the localized information
058: * will be contained in a LocalStrings.properties file located in the package directory of
059: * the classpath.
060: * <p>
061: * Please see the documentation for java.util.ResourceBundle for more information.
062: *
063: * @author sseshachala
064: * @version :$Revision$
065: */
066:
067: public class MessageManager {
068:
069: private static HashMap managers = new HashMap();
070:
071: public static synchronized MessageManager getManager(
072: java.lang.Class theClass) {
073: return getManager(theClass.getPackage().getName());
074: }
075:
076: /**
077: * Get the MessageManager for a particular package. If a manager for a package already
078: * exists, it will be reused, else a new MessageManager will be created and returned.
079: *
080: * @param packageName for which the MessageManager needs to be picked
081: * @return MessageManager For the given packageName
082: */
083:
084: public static synchronized MessageManager getManager(
085: String packageName) {
086: if (managers != null) {
087: MessageManager mgr = (MessageManager) managers
088: .get(packageName);
089: if (mgr == null) {
090: mgr = new MessageManager(packageName);
091: managers.put(packageName, mgr);
092: }
093: return mgr;
094: }
095: managers = new HashMap();
096: MessageManager mgr = new MessageManager(packageName);
097: managers.put(packageName, mgr);
098: return mgr;
099: }
100:
101: /**
102: * The ResourceBundle for this MessageManager.
103: */
104:
105: private ResourceBundle bundle;
106:
107: /**
108: * Creates a new MessageManager for a given package. This is a private method and all
109: * access to it is arbitrated by the static getManager method call so that only one
110: * MessageManager per package will be created.
111: *
112: * @param packageName Name of package to create MessageManager for.
113: */
114:
115: private MessageManager(String packageName) {
116: String bundleName = packageName + ".";
117: bundleName = bundleName + "LocalStrings";
118: bundle = ResourceBundle.getBundle(bundleName);
119: }
120:
121: /**
122: * Get a string from the underlying resource bundle.
123: *
124: * @param key for which msg needs to retrieved from HashMap
125: * @return String The MessageFormat for given Key
126: */
127:
128: public synchronized String getString(String key) {
129: if (key == null) {
130: String msg = "key is null";
131: throw new NullPointerException(msg);
132: }
133:
134: if (bundle == null) {
135: String message = " Could load Resources ";
136: throw new NullPointerException(message);
137: }
138:
139: String str;
140:
141: try {
142: str = bundle.getString(key);
143: } catch (MissingResourceException mre) {
144: str = "Cannot find message associated with key '" + key
145: + "'";
146: }
147:
148: return str;
149: }
150:
151: /**
152: * Get a string from the underlying resource bundle and format it with the given
153: * object argument. This argument can of course be a String object.
154: *
155: * @param key For which String needs to be displayed
156: * @param arg That is the result for Info, ERR or Exception
157: * @return String MessageString for given Key
158: */
159:
160: public synchronized String getString(String key, Object arg) {
161: Object[] args = new Object[] { arg };
162: return getString(key, args);
163: }
164:
165: /**
166: * Get a string from the underlying resource bundle and format it with the given
167: * object arguments. These arguments can of course be String objects.
168: *
169: * @param key For which Message needs to be constructed
170: * @param arg1 used for message construction
171: * @param arg2 used for message construction
172: * @return String Message String for the given key
173: */
174:
175: public String getString(String key, Object arg1, Object arg2) {
176: Object[] args = new Object[] { arg1, arg2 };
177: return getString(key, args);
178: }
179:
180: /**
181: * Get a string from the underlying resource bundle and format it with the given
182: * object arguments. These arguments can of course be String objects.
183: *
184: * @param key For which Message needs to be constructed
185: * @param arg1 Used for messageConstruction
186: * @param arg2 Used for message construction
187: * @param arg3 used for message construction
188: * @return String for given key and args.
189: */
190:
191: public String getString(String key, Object arg1, Object arg2,
192: Object arg3) {
193: Object[] args = new Object[] { arg1, arg2, arg3 };
194: return getString(key, args);
195: }
196:
197: /**
198: * Get a string from the underlying resource bundle and format it with the given
199: * object arguments. These arguments can of course be String objects.
200: *
201: * @param key For which Message needs to be constructed
202: * @param arg1 Used for messageConstruction
203: * @param arg2 Used for messageConstruction
204: * @param arg3 Used for messageConstruction
205: * @param arg4 Used for messageConstruction
206: * @return String MessageString for the given String And Args
207: */
208:
209: public String getString(String key, Object arg1, Object arg2,
210: Object arg3, Object arg4) {
211: Object[] args = new Object[] { arg1, arg2, arg3, arg4 };
212: return getString(key, args);
213: }
214:
215: /**
216: * Get a string from the underlying resource bundle and format it with the given set
217: * of arguments.
218: *
219: * @param key For which MessageFormat needs to be picked for a key
220: * @param args This contains the arguments
221: * @return String Message Format for given key and arguments
222: */
223: public String getString(String key, Object[] args) {
224: String iString;
225: String value = getString(key);
226:
227: try {
228: Object nonNullArgs[] = args;
229: for (int i = 0; i < args.length; i++) {
230: if (args[i] == null) {
231: if (nonNullArgs == args) {
232: nonNullArgs = (Object[]) args.clone();
233: }
234: nonNullArgs[i] = "null";
235: }
236: }
237:
238: iString = MessageFormat.format(value, nonNullArgs);
239: } catch (IllegalArgumentException iae) {
240: StringBuilder buf = new StringBuilder();
241: buf.append(value);
242: for (int i = 0; i < args.length; i++) {
243: buf.append(" arg[" + i + "]=" + args[i]);
244: }
245: iString = buf.toString();
246: }
247:
248: return iString;
249: }
250:
251: }
|