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