KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > convert > _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.convert;
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  * $Log: _MessageUtils.java,v $
27  * Revision 1.3 2004/07/01 22:00:51 mwessendorf
28  * ASF switch
29  *
30  * Revision 1.2 2004/03/26 12:08:42 manolito
31  * Exceptions in getAsString now catched and
32  * more relaxed Number casting in all number converters
33  *
34  * @author Manfred Geiler (latest modification by $Author: mwessendorf $)
35  * @version $Revision: 1.3 $ $Date: 2004/07/01 22:00:51 $
36  */

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