KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > util > MessageUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.util;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import javax.faces.application.FacesMessage;
22 import javax.faces.context.FacesContext;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28 /**
29  * @author Thomas Spiegl (latest modification by $Author: matze $)
30  * @author Manfred Geiler
31  * @version $Revision: 1.4 $ $Date: 2004/10/13 11:51:01 $
32  * $Log: MessageUtils.java,v $
33  * Revision 1.4 2004/10/13 11:51:01 matze
34  * renamed packages to org.apache
35  *
36  * Revision 1.3 2004/08/17 12:15:34 manolito
37  * NPE when there was no ViewRoot
38  *
39  * Revision 1.2 2004/07/01 22:01:13 mwessendorf
40  * ASF switch
41  *
42  * Revision 1.1 2004/03/30 16:59:47 manolito
43  * MessageFactory removed, MessageUtils moved to util in src/share
44  *
45  */

46 public class MessageUtils
47 {
48     private static final Log log = LogFactory.getLog(MessageUtils.class);
49
50     private static final String JavaDoc DEFAULT_BUNDLE = "javax.faces.Messages";
51     private static final String JavaDoc DETAIL_SUFFIX = "_detail";
52
53     public static FacesMessage getMessage(FacesMessage.Severity severity,
54                                           String JavaDoc messageId,
55                                           Object JavaDoc arg1)
56     {
57         return getMessage(severity,
58                           messageId,
59                           new Object JavaDoc[]{arg1},
60                           FacesContext.getCurrentInstance());
61     }
62
63     public static FacesMessage getMessage(FacesMessage.Severity severity,
64                                           String JavaDoc messageId,
65                                           Object JavaDoc[] args)
66     {
67         return getMessage(severity,
68                           messageId,
69                           args,
70                           FacesContext.getCurrentInstance());
71     }
72
73     public static FacesMessage getMessage(FacesMessage.Severity severity,
74                                           String JavaDoc messageId,
75                                           Object JavaDoc[] args,
76                                           FacesContext facesContext)
77     {
78         Locale JavaDoc locale;
79         if (facesContext.getViewRoot() != null)
80         {
81             locale = facesContext.getViewRoot().getLocale();
82         }
83         else
84         {
85             locale = facesContext.getApplication().getDefaultLocale();
86         }
87         return internalGetMessage(facesContext,
88                                   locale,
89                                   severity,
90                                   messageId,
91                                   args);
92     }
93
94     public static void addMessage(FacesMessage.Severity severity,
95                                   String JavaDoc messageId,
96                                   Object JavaDoc[] args)
97     {
98         addMessage(severity, messageId, args, null, FacesContext.getCurrentInstance());
99     }
100
101     public static void addMessage(FacesMessage.Severity severity,
102                                   String JavaDoc messageId,
103                                   Object JavaDoc[] args,
104                                   FacesContext facesContext)
105     {
106         addMessage(severity, messageId, args, null, facesContext);
107     }
108
109     public static void addMessage(FacesMessage.Severity severity,
110                                   String JavaDoc messageId,
111                                   Object JavaDoc[] args,
112                                   String JavaDoc forClientId)
113     {
114         addMessage(severity, messageId, args, forClientId, FacesContext.getCurrentInstance());
115     }
116
117     public static void addMessage(FacesMessage.Severity severity,
118                                   String JavaDoc messageId,
119                                   Object JavaDoc[] args,
120                                   String JavaDoc forClientId,
121                                   FacesContext facesContext)
122     {
123         facesContext.addMessage(forClientId,
124                                 getMessage(severity, messageId, args, facesContext));
125     }
126
127
128
129
130     private static FacesMessage internalGetMessage(FacesContext facesContext,
131                                                    Locale JavaDoc locale,
132                                                    FacesMessage.Severity severity,
133                                                    String JavaDoc messageId,
134                                                    Object JavaDoc args[])
135     {
136         ResourceBundle JavaDoc appBundle;
137         ResourceBundle JavaDoc defBundle;
138         String JavaDoc summary;
139         String JavaDoc detail;
140
141         appBundle = getApplicationBundle(facesContext, locale);
142         summary = getBundleString(appBundle, messageId);
143         if (summary != null)
144         {
145             detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
146         }
147         else
148         {
149             defBundle = getDefaultBundle(locale);
150             summary = getBundleString(defBundle, messageId);
151             if (summary != null)
152             {
153                 detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
154             }
155             else
156             {
157                 //Try to find detail alone
158
detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
159                 if (detail != null)
160                 {
161                     summary = null;
162                 }
163                 else
164                 {
165                     detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
166                     if (detail != null)
167                     {
168                         summary = null;
169                     }
170                     else
171                     {
172                         //Neither detail nor summary found
173
if (log.isWarnEnabled()) log.warn("No message with id " + messageId + " found in any bundle");
174                         return new FacesMessage(severity, messageId, null);
175                     }
176                 }
177             }
178         }
179
180         if (args != null && args.length > 0)
181         {
182             MessageFormat JavaDoc format;
183
184             if (summary != null)
185             {
186                 format = new MessageFormat JavaDoc(summary, locale);
187                 summary = format.format(args);
188             }
189
190             if (detail != null)
191             {
192                 format = new MessageFormat JavaDoc(detail, locale);
193                 detail = format.format(args);
194             }
195         }
196
197         return new FacesMessage(severity, summary, detail);
198     }
199
200
201     private static String JavaDoc getBundleString(ResourceBundle JavaDoc bundle, String JavaDoc key)
202     {
203         try
204         {
205             return bundle == null ? null : bundle.getString(key);
206         }
207         catch (MissingResourceException JavaDoc e)
208         {
209             return null;
210         }
211     }
212
213
214     private static ResourceBundle JavaDoc getApplicationBundle(FacesContext facesContext, Locale JavaDoc locale)
215     {
216         String JavaDoc bundleName = facesContext.getApplication().getMessageBundle();
217         if (bundleName != null)
218         {
219             try
220             {
221                 return ResourceBundle.getBundle(bundleName, locale, Thread.currentThread().getContextClassLoader());
222             }
223             catch (MissingResourceException JavaDoc e)
224             {
225                 log.error("Resource bundle " + bundleName + " could not be found.");
226                 return null;
227             }
228         }
229         else
230         {
231             return null;
232         }
233     }
234
235     private static ResourceBundle JavaDoc getDefaultBundle(Locale JavaDoc locale)
236     {
237         try
238         {
239             return ResourceBundle.getBundle(DEFAULT_BUNDLE,
240                                             locale,
241                                             FacesContext.class.getClassLoader());
242         }
243         catch (MissingResourceException JavaDoc e)
244         {
245             log.error("Resource bundle " + DEFAULT_BUNDLE + " could not be found.");
246             return null;
247         }
248     }
249
250 }
251
Popular Tags