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 com.sun.jsfcl.util;
042:
043: import java.beans.Beans;
044: import java.text.MessageFormat;
045: import java.util.Hashtable;
046: import java.util.Map;
047: import java.util.ResourceBundle;
048:
049: /**
050: * Wraps a java.util.ResourceBundle and utilizes a java.text.MessageFormat for message formatting.
051: * @see java.util.ResourceBundle
052: * @see java.text.MessageFormat
053: */
054: public class ComponentBundle {
055:
056: private static MessageFormat mf = new MessageFormat(""); // NOI18N
057: private ResourceBundle rb;
058: private static final String DESIGN_TIME_IMPL_CLASS = "com.sun.jsfcl.util.DesignTimeComponentBundle"; //NOI18N
059:
060: protected ComponentBundle() {
061: }
062:
063: /**
064: * If a class loader is not passed in, I will only be able to fetch ResourceBundles that
065: * are reachable from MY class loader. This is due to a bug in ResourceBundle that
066: * goes up the stack only 2 levels in order to find the appropriate class loader.
067: * Since I provide a level of indirection, I will ALWAYS be the callee, and therefore
068: * at the 2nd level.
069: * See source for ResourceBundle.getBundle() ResourceBundle.getLoader().
070: *
071: * @param baseName
072: */
073: public void init(String baseName, ClassLoader classLoader) {
074: rb = ResourceBundle.getBundle(baseName, mf.getLocale(),
075: classLoader);
076: }
077:
078: public String getMessage(String key) {
079:
080: String string = rb.getString(key);
081: return string;
082: }
083:
084: public String getMessage(String key, Object arg1) {
085: return getMessage(key, new Object[] { arg1 });
086: }
087:
088: public String getMessage(String key, Object arg1, Object arg2) {
089: return getMessage(key, new Object[] { arg1, arg2 });
090: }
091:
092: public String getMessage(String key, Object arg1, Object arg2,
093: Object arg3) {
094: return getMessage(key, new Object[] { arg1, arg2, arg3 });
095: }
096:
097: public String getMessage(String key, Object[] args) {
098: return getMessage(key, args, true);
099: }
100:
101: public String getMessage(String key, Object[] args,
102: boolean escapeSingleQuotes) {
103: String pattern = getMessage(key);
104: if (escapeSingleQuotes)
105: pattern = pattern.replaceAll("'", "''"); // NOI18N
106: String message;
107: synchronized (mf) {
108: mf.applyPattern(pattern);
109: message = mf.format(args);
110: }
111: return message;
112: }
113:
114: private static Map componentBundleMap = new Hashtable(); //use Hashtable instead of HashMap since we are lazily populating the map
115:
116: public static ComponentBundle getBundle(Class c) {
117:
118: return getBundle(c, null);
119: }
120:
121: /**
122: * Return the component bundle found in class c's package.
123: * The name of the bundle file is Bundle.properties. If suffix
124: * is not null and not empty, then the bundle file will be
125: * Bundle-<suffix>.properties.
126: *
127: * @param c
128: * @param suffix
129: * @return
130: */
131: public static ComponentBundle getBundle(Class c, String suffix) {
132:
133: String className = c.getName();
134: int lastDotIndex = className.lastIndexOf('.');
135: String packageName = ""; // NOI18N
136: if (lastDotIndex > -1) {
137: packageName = className.substring(0, lastDotIndex + 1);
138: }
139: String baseName = packageName + "Bundle"; // NOI18N
140: if (suffix != null && suffix.length() > 0) {
141: baseName += suffix;
142: }
143: ComponentBundle cb = (ComponentBundle) componentBundleMap
144: .get(baseName);
145: if (cb == null) {
146: if (Beans.isDesignTime()) {
147: try {
148: cb = (ComponentBundle) Class.forName(
149: DESIGN_TIME_IMPL_CLASS).newInstance();
150: } catch (Exception e) {
151: throw new RuntimeException(
152: "Could not create instance of "
153: + DESIGN_TIME_IMPL_CLASS, e);
154: }
155: } else {
156: cb = new ComponentBundle();
157: }
158: cb.init(baseName, c.getClassLoader());
159: componentBundleMap.put(baseName, cb);
160: }
161: return cb;
162: }
163: }
|