001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.shared_impl.util;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: import javax.faces.FactoryFinder;
022: import javax.faces.application.Application;
023: import javax.faces.application.ApplicationFactory;
024: import javax.faces.application.FacesMessage;
025: import javax.faces.context.FacesContext;
026: import java.text.MessageFormat;
027: import java.util.Locale;
028: import java.util.MissingResourceException;
029: import java.util.ResourceBundle;
030:
031: /**
032: * Utility class to support multilingual FacesMessages using ResourceBundles.
033: * Standard messages are stored at <code>DEFAULT_BUNDLE</code>.<br>
034: * The summary of the message is stored at the requested key value. The detail
035: * of the message is stored at <messageId>_detail.
036: *
037: * @see FacesMessage
038: * @see java.util.ResourceBundle
039: *
040: * @author Thomas Spiegl (latest modification by $Author: baranda $)
041: * @author Manfred Geiler
042: * @author Sean Schofield
043: * @author Stpehan Strittmatter
044: * @version $Revision: 544646 $ $Date: 2007-06-05 23:51:27 +0200 (Di, 05 Jun 2007) $
045: */
046: public final class MessageUtils {
047: /** Utility class, do not instatiate */
048: private MessageUtils() {
049: // nope
050: }
051:
052: /** Default bundle for messages (<code>javax.faces.Messages</code>) */
053: private static final String DEFAULT_BUNDLE = "javax.faces.Messages";
054:
055: /** Suffix for message details (<code>_detail</code>)*/
056: private static final String DETAIL_SUFFIX = "_detail";
057: private static Log log = LogFactory.getLog(MessageUtils.class);
058:
059: /**
060: * @param severity serverity of message
061: * @param messageId id of message
062: * @param arg arument of message
063: *
064: * @return generated FacesMessage
065: */
066: public static FacesMessage getMessage(
067: FacesMessage.Severity severity, String messageId, Object arg) {
068: return getMessage(severity, messageId, new Object[] { arg },
069: FacesContext.getCurrentInstance());
070: }
071:
072: /**
073: * @param severity serverity of message
074: * @param messageId id of message
075: * @param args aruments of message
076: *
077: * @return generated FacesMessage
078: */
079: public static FacesMessage getMessage(
080: FacesMessage.Severity severity, String messageId,
081: Object[] args) {
082: return getMessage(severity, messageId, args, FacesContext
083: .getCurrentInstance());
084: }
085:
086: public static FacesMessage getMessage(
087: FacesMessage.Severity severity, String messageId,
088: Object[] args, FacesContext facesContext) {
089: FacesMessage message = getMessage(facesContext, messageId, args);
090: message.setSeverity(severity);
091:
092: return message;
093: }
094:
095: public static void addMessage(FacesMessage.Severity severity,
096: String messageId, Object[] args) {
097: addMessage(severity, messageId, args, null, FacesContext
098: .getCurrentInstance());
099: }
100:
101: public static void addMessage(FacesMessage.Severity severity,
102: String messageId, Object[] args, FacesContext facesContext) {
103: addMessage(severity, messageId, args, null, facesContext);
104: }
105:
106: public static void addMessage(FacesMessage.Severity severity,
107: String messageId, Object[] args, String forClientId) {
108: addMessage(severity, messageId, args, forClientId, FacesContext
109: .getCurrentInstance());
110: }
111:
112: public static void addMessage(FacesMessage.Severity severity,
113: String messageId, Object[] args, String forClientId,
114: FacesContext facesContext) {
115: if (log.isTraceEnabled()) {
116: log.trace("adding message " + messageId + " for clientId "
117: + forClientId);
118: }
119: facesContext.addMessage(forClientId, getMessage(severity,
120: messageId, args, facesContext));
121: }
122:
123: /**
124: * Uses <code>MessageFormat</code> and the supplied parameters to fill in the param placeholders in the String.
125: *
126: * @param locale The <code>Locale</code> to use when performing the substitution.
127: * @param msgtext The original parameterized String.
128: * @param params The params to fill in the String with.
129: * @return The updated String.
130: */
131: public static String substituteParams(Locale locale,
132: String msgtext, Object params[]) {
133: String localizedStr = null;
134: if (params == null || msgtext == null)
135: return msgtext;
136: StringBuffer b = new StringBuffer(100);
137: MessageFormat mf = new MessageFormat(msgtext);
138: if (locale != null) {
139: mf.setLocale(locale);
140: b.append(mf.format(params));
141: localizedStr = b.toString();
142: }
143: return localizedStr;
144: }
145:
146: public static FacesMessage getMessage(String messageId,
147: Object params[]) {
148: Locale locale = null;
149: FacesContext context = FacesContext.getCurrentInstance();
150: if (context != null && context.getViewRoot() != null) {
151: locale = context.getViewRoot().getLocale();
152: if (locale == null)
153: locale = Locale.getDefault();
154: } else {
155: locale = Locale.getDefault();
156: }
157: return getMessage(locale, messageId, params);
158: }
159:
160: public static FacesMessage getMessage(Locale locale,
161: String messageId, Object params[]) {
162: String summary = null;
163: String detail = null;
164: String bundleName = getApplication().getMessageBundle();
165: ResourceBundle bundle = null;
166:
167: if (bundleName != null) {
168: try {
169: bundle = ResourceBundle.getBundle(bundleName, locale,
170: org.apache.myfaces.shared_impl.util.ClassUtils
171: .getCurrentLoader(bundleName));
172: summary = bundle.getString(messageId);
173: } catch (MissingResourceException e) {
174: // NoOp
175: }
176: }
177:
178: if (summary == null) {
179: try {
180: bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE,
181: locale,
182: org.apache.myfaces.shared_impl.util.ClassUtils
183: .getCurrentLoader(DEFAULT_BUNDLE));
184: if (bundle == null) {
185: throw new NullPointerException();
186: }
187: summary = bundle.getString(messageId);
188: } catch (MissingResourceException e) {
189: // NoOp
190: }
191: }
192:
193: if (summary == null) {
194: summary = messageId;
195: }
196:
197: if (bundle == null) {
198: throw new NullPointerException(
199: "Unable to locate ResrouceBundle: bundle is null");
200: }
201: summary = substituteParams(locale, summary, params);
202:
203: try {
204: detail = substituteParams(locale, bundle
205: .getString(messageId + DETAIL_SUFFIX), params);
206: } catch (MissingResourceException e) {
207: // NoOp
208: }
209:
210: return new FacesMessage(summary, detail);
211: }
212:
213: /**
214: * @param bundleBaseName baseName of ResourceBundle to load localized messages
215: * @param messageId id of message
216: * @param params parameters to set at localized message
217: * @return generated FacesMessage
218: */
219: public static FacesMessage getMessage(String bundleBaseName,
220: String messageId, Object params[]) {
221: return getMessage(bundleBaseName, getCurrentLocale(),
222: messageId, params);
223: }
224:
225: /**
226: *
227: * @return currently applicable Locale for this request.
228: */
229: public static Locale getCurrentLocale() {
230: Locale locale;
231: FacesContext context = FacesContext.getCurrentInstance();
232: if (context != null && context.getViewRoot() != null) {
233: locale = context.getViewRoot().getLocale();
234: if (locale == null)
235: locale = Locale.getDefault();
236: } else {
237: locale = Locale.getDefault();
238: }
239:
240: return locale;
241: }
242:
243: /**
244: * @param severity severity of message
245: * @param bundleBaseName baseName of ResourceBundle to load localized messages
246: * @param messageId id of message
247: * @param params parameters to set at localized message
248: * @return generated FacesMessage
249: */
250: public static FacesMessage getMessage(
251: FacesMessage.Severity severity, String bundleBaseName,
252: String messageId, Object params[]) {
253: FacesMessage msg = getMessage(bundleBaseName, messageId, params);
254: msg.setSeverity(severity);
255:
256: return msg;
257: }
258:
259: /**
260: * @param bundleBaseName baseName of ResourceBundle to load localized messages
261: * @param locale current locale
262: * @param messageId id of message
263: * @param params parameters to set at localized message
264: * @return generated FacesMessage
265: */
266: public static FacesMessage getMessage(String bundleBaseName,
267: Locale locale, String messageId, Object params[]) {
268: if (bundleBaseName == null) {
269: throw new NullPointerException(
270: "Unable to locate ResrouceBundle: bundle is null");
271: }
272:
273: ResourceBundle bundle = ResourceBundle.getBundle(
274: bundleBaseName, locale);
275:
276: return getMessage(bundle, messageId, params);
277: }
278:
279: /**
280: * @param bundle ResourceBundle to load localized messages
281: * @param messageId id of message
282: * @param params parameters to set at localized message
283: * @return generated FacesMessage
284: */
285: public static FacesMessage getMessage(ResourceBundle bundle,
286: String messageId, Object params[]) {
287:
288: String summary = null;
289: String detail = null;
290:
291: try {
292: summary = bundle.getString(messageId);
293: } catch (MissingResourceException e) {
294: // NoOp
295: }
296:
297: if (summary == null) {
298: summary = messageId;
299: }
300:
301: summary = substituteParams(bundle.getLocale(), summary, params);
302:
303: try {
304: detail = substituteParams(bundle.getLocale(), bundle
305: .getString(messageId + DETAIL_SUFFIX), params);
306: } catch (MissingResourceException e) {
307: // NoOp
308: }
309:
310: return new FacesMessage(summary, detail);
311: }
312:
313: /**
314: *
315: * @param context
316: * @param messageId
317: * @return generated FacesMessage
318: */
319: public static FacesMessage getMessage(FacesContext context,
320: String messageId) {
321: return getMessage(context, messageId, ((Object[]) (null)));
322: }
323:
324: /**
325: *
326: * @param context
327: * @param messageId
328: * @param params
329: * @return generated FacesMessage
330: */
331: public static FacesMessage getMessage(FacesContext context,
332: String messageId, Object params[]) {
333: if (context == null || messageId == null)
334: throw new NullPointerException(" context " + context
335: + " messageId " + messageId);
336: Locale locale = getCurrentLocale();
337: if (null == locale)
338: throw new NullPointerException(" locale " + locale);
339: FacesMessage message = getMessage(locale, messageId, params);
340: if (message != null) {
341: return message;
342: } else {
343: // TODO /FIX: Note that this has fallback behavior to default Locale for message,
344: // but similar behavior above does not. The methods should probably behave
345: locale = Locale.getDefault();
346: return getMessage(locale, messageId, params);
347: }
348: }
349:
350: private static Application getApplication() {
351: FacesContext context = FacesContext.getCurrentInstance();
352: if (context != null) {
353: return FacesContext.getCurrentInstance().getApplication();
354: }
355:
356: ApplicationFactory afactory = (ApplicationFactory) FactoryFinder
357: .getFactory("javax.faces.application.ApplicationFactory");
358: return afactory.getApplication();
359:
360: }
361: }
|