KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > validator > _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.validator;
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.2 $ $Date: 2004/07/01 22:01:10 $
28  */

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