KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > test > MessageFactory


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 /*
14  *
15  */

16
17 /*
18  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
19  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
20  */

21
22 package com.tonbeller.wcf.test;
23
24 import javax.faces.application.Application;
25 import javax.faces.application.FacesMessage;
26 import javax.faces.context.FacesContext;
27
28 import java.text.MessageFormat JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.MissingResourceException JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32
33 /**
34  * <p>supported filters: <code>package</code> and
35  * <code>protection</code>.</p>
36  */

37
38 public class MessageFactory extends Object JavaDoc {
39
40     //
41
// Protected Constants
42
//
43

44     //
45
// Class Variables
46
//
47

48     //
49
// Instance Variables
50
//
51

52     // Attribute Instance Variables
53

54     // Relationship Instance Variables
55

56     //
57
// Constructors and Initializers
58
//
59

60     private MessageFactory() {
61     }
62
63     //
64
// Class methods
65
//
66

67     //
68
// General Methods
69
//
70

71     public static String JavaDoc substituteParams(Locale JavaDoc locale, String JavaDoc msgtext, Object JavaDoc params[]) {
72         String JavaDoc localizedStr = null;
73
74         if (params == null || msgtext == null) {
75             return msgtext;
76         }
77         StringBuffer JavaDoc b = new StringBuffer JavaDoc(100);
78         MessageFormat JavaDoc mf = new MessageFormat JavaDoc(msgtext);
79         if (locale != null) {
80             mf.setLocale(locale);
81             b.append(mf.format(params));
82             localizedStr = b.toString();
83         }
84         return localizedStr;
85     }
86
87
88     /**
89      * This version of getMessage() is used in the RI for localizing RI
90      * specific messages.
91      */

92
93     public static FacesMessage getMessage(String JavaDoc messageId, Object JavaDoc params[]) {
94         Locale JavaDoc locale = null;
95         FacesContext context = FacesContext.getCurrentInstance();
96         // context.getViewRoot() may not have been initialized at this point.
97
if (context != null && context.getViewRoot() != null) {
98             locale = context.getViewRoot().getLocale();
99             if (locale == null) {
100                 locale = Locale.getDefault();
101             }
102         } else {
103             locale = Locale.getDefault();
104         }
105
106         return getMessage(locale, messageId, params);
107     }
108
109
110     public static FacesMessage getMessage(Locale JavaDoc locale, String JavaDoc messageId,
111                                           Object JavaDoc params[]) {
112         FacesMessage result = null;
113         String JavaDoc
114             summary = null,
115             detail = null,
116             bundleName = null;
117         ResourceBundle JavaDoc bundle = null;
118
119         // see if we have a user-provided bundle
120
if (null != (bundleName = getApplication().getMessageBundle())) {
121             if (null !=
122                 (bundle =
123                 ResourceBundle.getBundle(bundleName, locale,
124                                          getCurrentLoader(bundleName)))) {
125                 // see if we have a hit
126
try {
127                     summary = bundle.getString(messageId);
128                 } catch (MissingResourceException JavaDoc e) {
129                 }
130             }
131         }
132
133         // we couldn't find a summary in the user-provided bundle
134
if (null == summary) {
135             // see if we have a summary in the app provided bundle
136
bundle = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES,
137                                               locale,
138                                               getCurrentLoader(bundleName));
139             if (null == bundle) {
140                 throw new NullPointerException JavaDoc();
141             }
142             // see if we have a hit
143
try {
144                 summary = bundle.getString(messageId);
145             } catch (MissingResourceException JavaDoc e) {
146             }
147         }
148
149         // we couldn't find a summary anywhere! Return null
150
if (null == summary) {
151             return null;
152         }
153
154         // At this point, we have a summary and a bundle.
155
if (null == summary || null == bundle) {
156             throw new NullPointerException JavaDoc();
157         }
158         summary = substituteParams(locale, summary, params);
159
160         try {
161             detail = substituteParams(locale,
162                                       bundle.getString(messageId + "_detail"),
163                                       params);
164         } catch (MissingResourceException JavaDoc e) {
165         }
166
167         return (new FacesMessage(summary, detail));
168     }
169
170
171     //
172
// Methods from MessageFactory
173
//
174
public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId) {
175         return getMessage(context, messageId, null);
176     }
177
178
179     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
180                                           Object JavaDoc params[]) {
181         if (context == null || messageId == null) {
182             throw new NullPointerException JavaDoc(
183                 "One or more parameters could be null");
184         }
185         Locale JavaDoc locale = null;
186         // viewRoot may not have been initialized at this point.
187
if (context != null && context.getViewRoot() != null) {
188             locale = context.getViewRoot().getLocale();
189         } else {
190             locale = Locale.getDefault();
191         }
192         if (null == locale) {
193             throw new NullPointerException JavaDoc();
194         }
195         FacesMessage message = getMessage(locale, messageId, params);
196         if (message != null) {
197             return message;
198         }
199         locale = Locale.getDefault();
200         return (getMessage(locale, messageId, params));
201     }
202
203
204     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
205                                           Object JavaDoc param0) {
206         return getMessage(context, messageId, new Object JavaDoc[]{param0});
207     }
208
209
210     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
211                                           Object JavaDoc param0, Object JavaDoc param1) {
212         return getMessage(context, messageId, new Object JavaDoc[]{param0, param1});
213     }
214
215
216     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
217                                           Object JavaDoc param0, Object JavaDoc param1,
218                                           Object JavaDoc param2) {
219         return getMessage(context, messageId,
220                           new Object JavaDoc[]{param0, param1, param2});
221     }
222
223
224     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
225                                           Object JavaDoc param0, Object JavaDoc param1,
226                                           Object JavaDoc param2, Object JavaDoc param3) {
227         return getMessage(context, messageId,
228                           new Object JavaDoc[]{param0, param1, param2, param3});
229     }
230
231
232     protected static Application getApplication() {
233         return (FacesContext.getCurrentInstance().getApplication());
234     }
235
236
237     protected static ClassLoader JavaDoc getCurrentLoader(Object JavaDoc fallbackClass) {
238         ClassLoader JavaDoc loader =
239             Thread.currentThread().getContextClassLoader();
240         if (loader == null) {
241             loader = fallbackClass.getClass().getClassLoader();
242         }
243         return loader;
244     }
245
246
247 } // end of class MessageFactory
248
Popular Tags