KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > catalog > validator > MessageFactory


1 /* Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: http://developer.sun.com/berkeley_license.html
2 $Id: MessageFactory.java,v 1.2 2005/03/23 14:49:22 jenniferb Exp $ */

3
4 package com.sun.j2ee.blueprints.catalog.validator;
5
6 import java.io.FileInputStream JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 import java.util.Locale JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15 import java.util.MissingResourceException JavaDoc;
16 import javax.faces.FacesException;
17 import javax.faces.application.Application;
18 import javax.faces.application.FacesMessage;
19 import javax.faces.context.FacesContext;
20 import java.text.MessageFormat JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * This class examines a resource bundle and generates
25  * <code>FacesMessage</code> instances, which represent
26  * single localized messages.
27  */

28
29 public class MessageFactory extends Object JavaDoc
30 {
31
32
33     private MessageFactory() {
34     }
35
36     /**
37      * Creates a <code>MessageFormat</code> pattern given the specified locale, message,
38      * and parameters; converts the pattern to a string; and returns the string.
39      * @param locale The locale being used by the application
40      * @param msgtext The message as it appears in the resource bundle
41      * @param params The set of parameters provided by the user
42      * @return a string converted from the message format pattern
43      */

44     public static String JavaDoc substituteParams(Locale JavaDoc locale, String JavaDoc msgtext, Object JavaDoc params[]) {
45         String JavaDoc localizedStr = null;
46         
47         if (params == null || msgtext == null ) {
48             return msgtext;
49         }
50         StringBuffer JavaDoc b = new StringBuffer JavaDoc(100);
51         MessageFormat JavaDoc mf = new MessageFormat JavaDoc(msgtext);
52         if (locale != null) {
53             mf.setLocale(locale);
54             b.append(mf.format(params));
55             localizedStr = b.toString();
56         }
57         return localizedStr;
58     }
59
60     /**
61     * Returns a <code>FacesMessage</code> object given a message ID and a set of parameters
62     * provided by the user.
63     * @param messageId The ID used to locate the message in the resource bundle
64     * @param params The set of parameters provided by the user
65     * @return a <code>FacesMessage</code> instance representing the message
66     */

67
68     public static FacesMessage getMessage(String JavaDoc messageId, Object JavaDoc params[]) {
69         Locale JavaDoc locale = null;
70         FacesContext context = FacesContext.getCurrentInstance();
71         // context.getViewRoot() may not have been initialized at this point.
72
if (context != null && context.getViewRoot() != null) {
73             locale = context.getViewRoot().getLocale();
74             if (locale == null) {
75                 locale = Locale.getDefault();
76             }
77         } else {
78             locale = Locale.getDefault();
79         }
80         
81     return getMessage(locale, messageId, params);
82     }
83
84    /**
85     * Returns a <code>FacesMessage</code> object given a locale, message ID, and a set of
86     * parameters provided by the user.
87     * @param locale The locale used by the application
88     * @param messageId The ID used to locate the message in the resource bundle
89     * @param params The set of parameters provided by the user
90     * @return a <code>FacesMessage</code> instance representing the message
91     */

92     public static FacesMessage getMessage(Locale JavaDoc locale, String JavaDoc messageId,
93                        Object JavaDoc params[]) {
94     FacesMessage result = null;
95     String JavaDoc
96         summary = null,
97         detail = null,
98         bundleName = null;
99     ResourceBundle JavaDoc bundle = null;
100
101     // see if we have a user-provided bundle
102
if (null != (bundleName = getApplication().getMessageBundle())) {
103         if (null !=
104         (bundle =
105          ResourceBundle.getBundle(bundleName, locale,
106                       getCurrentLoader(bundleName)))) {
107         // see if we have a hit
108
try {
109             summary = bundle.getString(messageId);
110         }
111         catch (MissingResourceException JavaDoc e) {
112         }
113         }
114     }
115     
116     // we couldn't find a summary in the user-provided bundle
117
if (null == summary) {
118         // see if we have a summary in the app provided bundle
119
bundle = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES,
120                           locale,
121                           getCurrentLoader(bundleName));
122         if (null == bundle) {
123         throw new NullPointerException JavaDoc();
124         }
125         // see if we have a hit
126
try {
127         summary = bundle.getString(messageId);
128         }
129         catch (MissingResourceException JavaDoc e) {
130         }
131     }
132     
133     // we couldn't find a summary anywhere! Return null
134
if (null == summary) {
135         return null;
136     }
137
138     // At this point, we have a summary and a bundle.
139
if (null == summary || null == bundle) {
140         throw new NullPointerException JavaDoc();
141     }
142     summary = substituteParams(locale, summary, params);
143
144     try {
145         detail = substituteParams(locale,
146                       bundle.getString(messageId + "_detail"),
147                       params);
148     }
149     catch (MissingResourceException JavaDoc e) {
150     }
151
152         return (new FacesMessage(summary, detail));
153     }
154
155
156     //
157
// Methods from MessageFactory
158
//
159
public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId) {
160         return getMessage(context, messageId, null);
161     }
162
163    /**
164     * Returns a <code>FacesMessage</code> object given the current <code>FacesContext</code>
165     * instance, a message ID and a set of parameters
166     * provided by the user.
167     * @param context The current <code>FacesContext</code> instance
168     * @param messageId The ID used to locate the message in the resource bundle
169     * @param params The set of parameters provided by the user
170     * @return a <code>FacesMessage</code> instance representing the message
171     */

172     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
173                        Object JavaDoc params[]) {
174         if (context == null || messageId == null ) {
175             throw new NullPointerException JavaDoc("One or more parameters could be null");
176         }
177         Locale JavaDoc locale = null;
178         // viewRoot may not have been initialized at this point.
179
if (context != null && context.getViewRoot() != null) {
180             locale = context.getViewRoot().getLocale();
181         } else {
182             locale = Locale.getDefault();
183         }
184     if (null == locale) {
185         throw new NullPointerException JavaDoc();
186     }
187         FacesMessage message = getMessage(locale, messageId, params);
188         if (message != null) {
189             return message;
190         }
191         locale = Locale.getDefault();
192         return (getMessage(locale, messageId, params));
193     }
194     
195    /**
196     * Returns a <code>FacesMessage</code> object given the current <code>FacesContext</code>,
197     * a message ID and a parameter provided by the user.
198     * @param context The current <code>FacesContext</code> instance
199     * @param messageId The ID used to locate the message in the resource bundle
200     * @param param0 A parameter provided by the user
201     * @return a <code>FacesMessage</code> instance representing the message
202     */

203     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
204                                        Object JavaDoc param0) {
205         return getMessage(context, messageId, new Object JavaDoc[]{param0});
206     }
207  
208    /**
209     * Returns a <code>FacesMessage</code> object given the current <code>FacesContext</code>,
210     * a message ID and a set of parameters provided by the user.
211     * @param context The current <code>FacesContext</code> instance
212     * @param messageId The ID used to locate the message in the resource bundle
213     * @param param0 A parameter provided by the user
214     * @param param1 A parameter provided by the user
215     * @return a <code>FacesMessage</code> instance representing the message
216     */

217     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
218                                        Object JavaDoc param0, Object JavaDoc param1) {
219          return getMessage(context, messageId, new Object JavaDoc[]{param0, param1});
220     }
221
222    /**
223     * Returns a <code>FacesMessage</code> object given the current <code>FacesContext</code>,
224     * a message ID and a set of parameters provided by the user.
225     * @param context The current <code>FacesContext</code> instance
226     * @param messageId The ID used to locate the message in the resource bundle
227     * @param param0 A parameter provided by the user
228     * @param param1 A parameter provided by the user
229     * @param param2 A parameter provided by the user
230     * @return a <code>FacesMessage</code> instance representing the message
231     */

232     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
233                                        Object JavaDoc param0, Object JavaDoc param1,
234                                        Object JavaDoc param2) {
235          return getMessage(context, messageId,
236              new Object JavaDoc[]{param0, param1, param2});
237     }
238
239    /**
240     * Returns a <code>FacesMessage</code> object given the current <code>FacesContext</code>
241     * instance, a message ID and a set of parameters provided by the user.
242     * @param context The current <code>FacesContext</code> instance
243     * @param messageId The ID used to locate the message in the resource bundle
244     * @param param0 A parameter provided by the user
245     * @param param1 A parameter provided by the user
246     * @param param2 A parameter provided by the user
247     * @param param3 A parameter provided by the user
248     * @return a <code>FacesMessage</code> instance representing the message
249     */

250     public static FacesMessage getMessage(FacesContext context, String JavaDoc messageId,
251                                        Object JavaDoc param0, Object JavaDoc param1,
252                                        Object JavaDoc param2, Object JavaDoc param3) {
253          return getMessage(context, messageId,
254                  new Object JavaDoc[]{param0, param1, param2, param3});
255     }
256
257     protected static Application getApplication() {
258         return (FacesContext.getCurrentInstance().getApplication());
259     }
260
261     protected static ClassLoader JavaDoc getCurrentLoader(Object JavaDoc fallbackClass) {
262         ClassLoader JavaDoc loader =
263         Thread.currentThread().getContextClassLoader();
264     if (loader == null) {
265         loader = fallbackClass.getClass().getClassLoader();
266     }
267     return loader;
268     }
269
270
271 } // end of class MessageFactory
272
Popular Tags