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.rave.web.ui.util;
042:
043: import java.text.*;
044: import java.util.*;
045:
046: import javax.faces.context.FacesContext;
047:
048: /**
049: * Factory class for retrieving server-side i18n messages within the JSF
050: * framework. Note that the ServletResponse locale, content type, and character
051: * encoding are not set here. Since tags may be used outside the Sun Web
052: * Console, that task will most likely be done in the console's session filter.
053: * <p>
054: * Example:
055: * </p><code>
056: * ResponseWriter w = FacesContext.getCurrentInstance().getResponseWriter();
057: * w.write(MessageUtil.getMessage("com.sun.rave.web.ui.Resources", "key"));
058: * </code>
059: *
060: * @author Dan Labrecque
061: */
062: public class MessageUtil extends Object {
063: // Default constructor.
064: protected MessageUtil() {
065: }
066:
067: /**
068: * Get a message from a desired resource bundle.
069: *
070: * @param context The FacesContext object used to obtain locale.
071: * @param baseName The fully qualified name of the resource bundle.
072: * @param key The key for the desired string.
073: * @throws NullPointerException if context or baseName is null.
074: */
075: public static String getMessage(FacesContext context,
076: String baseName, String key) {
077: return getMessage(context, baseName, key, null);
078: }
079:
080: /**
081: * Get a formatted message from a desired resource bundle.
082: *
083: * @param context The FacesContext object used to obtain locale.
084: * @param baseName The fully qualified name of the resource bundle.
085: * @param key The key for the desired string.
086: * @param args The arguments to be inserted into the string.
087: * @throws NullPointerException if context or baseName is null.
088: */
089: public static String getMessage(FacesContext context,
090: String baseName, String key, Object args[]) {
091: return getMessage(getLocale(context), baseName, key, args);
092: }
093:
094: /**
095: * Get a message from a desired resource bundle.
096: *
097: * @param baseName The fully qualified name of the resource bundle.
098: * @param key The key for the desired string.
099: * @throws NullPointerException if baseName is null.
100: */
101: public static String getMessage(String baseName, String key) {
102: return getMessage(baseName, key, null);
103: }
104:
105: /**
106: * Get a formatted message from a desired resource bundle.
107: *
108: * @param baseName The fully qualified name of the resource bundle.
109: * @param key The key for the desired string.
110: * @param args The arguments to be inserted into the string.
111: * @throws NullPointerException if baseName is null.
112: */
113: public static String getMessage(String baseName, String key,
114: Object args[]) {
115: return getMessage(getLocale(), baseName, key, args);
116: }
117:
118: /**
119: * Get a formatted message from a desired resource bundle.
120: *
121: * @param locale The locale for which a resource bundle is desired.
122: * @param baseName The fully qualified name of the resource bundle.
123: * @param key The key for the desired string.
124: * @param args The arguments to be inserted into the string.
125: * @throws NullPointerException if locale or baseName is null.
126: */
127: public static String getMessage(Locale locale, String baseName,
128: String key, Object args[]) {
129: ClassLoader loader = ClassLoaderFinder
130: .getCurrentLoader(MessageUtil.class);
131: // First try the context CL
132: return getMessage(locale, baseName, key, args, loader);
133: }
134:
135: /**
136: * Get a formatted message from a desired resource bundle.
137: *
138: * @param locale The locale for which a resource bundle is desired.
139: * @param baseName The fully qualified name of the resource bundle.
140: * @param key The key for the desired string.
141: * @param args The arguments to be inserted into the string.
142: * @param loader The class loader used to load the resource bundle.
143: * @throws NullPointerException if locale, baseName, or loader is null.
144: */
145: public static String getMessage(Locale locale, String baseName,
146: String key, Object args[], ClassLoader loader) {
147: if (key == null)
148: return key;
149: else if (locale == null || baseName == null || loader == null)
150: throw new NullPointerException(
151: "One or more parameters is null");
152:
153: ResourceBundle bundle = ResourceBundleManager.getInstance()
154: .getBundle(baseName, locale, loader);
155:
156: if (null == bundle)
157: throw new NullPointerException(
158: "Could not obtain resource bundle");
159:
160: String message = null;
161:
162: try {
163: message = bundle.getString(key);
164: } catch (MissingResourceException e) {
165: }
166:
167: return getFormattedMessage((message != null) ? message : key,
168: args);
169: }
170:
171: /**
172: * Format message using given arguments.
173: *
174: * @param message The string used as a pattern for inserting arguments.
175: * @param args The arguments to be inserted into the string.
176: */
177: protected static String getFormattedMessage(String message,
178: Object args[]) {
179: if ((args == null) || (args.length == 0)) {
180: return message;
181: }
182:
183: String result = null;
184:
185: try {
186: MessageFormat mf = new MessageFormat(message);
187: result = mf.format(args);
188: } catch (NullPointerException e) {
189: }
190:
191: return (result != null) ? result : message;
192: }
193:
194: /**
195: * Get locale from current FacesContext instance.
196: */
197: protected static Locale getLocale() {
198: return getLocale(FacesContext.getCurrentInstance());
199: }
200:
201: /**
202: * Get locale from given FacesContext object.
203: *
204: * @param context The FacesContext object used to obtain locale.
205: */
206: protected static Locale getLocale(FacesContext context) {
207: if (context == null) {
208: return Locale.getDefault();
209: }
210:
211: Locale locale = null;
212:
213: // context.getViewRoot() may not have been initialized at this point.
214: if (context.getViewRoot() != null)
215: locale = context.getViewRoot().getLocale();
216:
217: return (locale != null) ? locale : Locale.getDefault();
218: }
219:
220: /**
221: * Get current class loader from given object.
222: *
223: * @param o Object used to obtain fallback class loader.
224: */
225: public static ClassLoader getCurrentLoader(Object o) {
226: ClassLoader loader = Thread.currentThread()
227: .getContextClassLoader();
228: return (loader != null) ? loader : o.getClass()
229: .getClassLoader();
230: }
231: }
|