KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > component > _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 javax.faces.component;
17
18 import javax.faces.application.FacesMessage;
19 import javax.faces.context.FacesContext;
20 import java.text.MessageFormat JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.MissingResourceException JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24
25 /**
26  * @author Manfred Geiler (latest modification by $Author: mwessendorf $)
27  * @version $Revision: 1.5 $ $Date: 2004/07/01 22:00:50 $
28  * $Log: _MessageUtils.java,v $
29  * Revision 1.5 2004/07/01 22:00:50 mwessendorf
30  * ASF switch
31  *
32  * Revision 1.4 2004/06/14 12:55:21 manolito
33  * Added missing CVS Log comment
34  *
35  */

36 class _MessageUtils
37 {
38     private static final String JavaDoc DETAIL_SUFFIX = "_detail";
39
40     static void addErrorMessage(FacesContext facesContext,
41                                 UIComponent component,
42                                 String JavaDoc messageId)
43     {
44         facesContext.addMessage(component.getClientId(facesContext),
45                                 getMessage(facesContext,
46                                            facesContext.getViewRoot().getLocale(),
47                                            FacesMessage.SEVERITY_ERROR,
48                                            messageId,
49                                            null));
50     }
51
52     static void addErrorMessage(FacesContext facesContext,
53                                 UIComponent component,
54                                 String JavaDoc messageId, Object JavaDoc[] args)
55     {
56         facesContext.addMessage(component.getClientId(facesContext),
57                                 getMessage(facesContext,
58                                            facesContext.getViewRoot().getLocale(),
59                                            FacesMessage.SEVERITY_ERROR,
60                                            messageId,
61                                            args));
62     }
63
64     static FacesMessage getMessage(FacesContext facesContext,
65                                    Locale JavaDoc locale,
66                                    FacesMessage.Severity severity,
67                                    String JavaDoc messageId,
68                                    Object JavaDoc args[])
69     {
70         ResourceBundle JavaDoc appBundle;
71         ResourceBundle JavaDoc defBundle;
72         String JavaDoc summary;
73         String JavaDoc detail;
74
75         appBundle = getApplicationBundle(facesContext, locale);
76         summary = getBundleString(appBundle, messageId);
77         if (summary != null)
78         {
79             detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
80         }
81         else
82         {
83             defBundle = getDefaultBundle(facesContext, locale);
84             summary = getBundleString(defBundle, messageId);
85             if (summary != null)
86             {
87                 detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
88             }
89             else
90             {
91                 //Try to find detail alone
92
detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
93                 if (detail != null)
94                 {
95                     summary = null;
96                 }
97                 else
98                 {
99                     detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
100                     if (detail != null)
101                     {
102                         summary = null;
103                     }
104                     else
105                     {
106                         //Neither detail nor summary found
107
facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle");
108                         return new FacesMessage(severity, messageId, null);
109                     }
110                 }
111             }
112         }
113
114         if (args != null && args.length > 0)
115         {
116             MessageFormat JavaDoc format;
117
118             if (summary != null)
119             {
120                 format = new MessageFormat JavaDoc(summary, locale);
121                 summary = format.format(args);
122             }
123
124             if (detail != null)
125             {
126                 format = new MessageFormat JavaDoc(detail, locale);
127                 detail = format.format(args);
128             }
129         }
130
131         return new FacesMessage(severity, summary, detail);
132     }
133
134
135     private static String JavaDoc getBundleString(ResourceBundle JavaDoc bundle, String JavaDoc key)
136     {
137         try
138         {
139             return bundle == null ? null : bundle.getString(key);
140         }
141         catch (MissingResourceException JavaDoc e)
142         {
143             return null;
144         }
145     }
146
147
148     private static ResourceBundle JavaDoc getApplicationBundle(FacesContext facesContext, Locale JavaDoc locale)
149     {
150         String JavaDoc bundleName = facesContext.getApplication().getMessageBundle();
151         if (bundleName != null)
152         {
153             return getBundle(facesContext, locale, bundleName);
154         }
155         else
156         {
157             return null;
158         }
159     }
160
161     private static ResourceBundle JavaDoc getDefaultBundle(FacesContext facesContext,
162                                                    Locale JavaDoc locale)
163     {
164         return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
165     }
166
167     private static ResourceBundle JavaDoc getBundle(FacesContext facesContext,
168                                             Locale JavaDoc locale,
169                                             String JavaDoc bundleName)
170     {
171         try
172         {
173             //First we try the JSF implementation class loader
174
return ResourceBundle.getBundle(bundleName,
175                                             locale,
176                                             facesContext.getClass().getClassLoader());
177         }
178         catch (MissingResourceException JavaDoc ignore1)
179         {
180             try
181             {
182                 //Next we try the JSF API class loader
183
return ResourceBundle.getBundle(bundleName,
184                                                 locale,
185                                                 _MessageUtils.class.getClassLoader());
186             }
187             catch (MissingResourceException JavaDoc ignore2)
188             {
189                 try
190                 {
191                     //Last resort is the context class loader
192
return ResourceBundle.getBundle(bundleName,
193                                                     locale,
194                                                     Thread.currentThread().getContextClassLoader());
195                 }
196                 catch (MissingResourceException JavaDoc damned)
197                 {
198                     facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
199                     return null;
200                 }
201             }
202         }
203     }
204
205 }
206
Popular Tags