View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package javax.faces.component;
20  
21  import javax.el.ValueExpression;
22  import javax.faces.application.FacesMessage;
23  import javax.faces.context.FacesContext;
24  
25  import java.text.MessageFormat;
26  import java.util.Locale;
27  import java.util.MissingResourceException;
28  import java.util.ResourceBundle;
29  
30  /**
31   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
32   * @version $Revision: 940134 $ $Date: 2010-05-01 20:31:07 -0500 (Sat, 01 May 2010) $
33   */
34  class _MessageUtils
35  {
36      private static final String DETAIL_SUFFIX = "_detail";
37  
38      static void addErrorMessage(FacesContext facesContext,
39                                  UIComponent component,
40                                  String messageId)
41      {
42          facesContext.addMessage(component.getClientId(facesContext),
43                                  getMessage(facesContext,
44                                             facesContext.getViewRoot().getLocale(),
45                                             FacesMessage.SEVERITY_ERROR,
46                                             messageId,
47                                             null));
48      }
49  
50      static void addErrorMessage(FacesContext facesContext,
51                                  UIComponent component,
52                                  String messageId, Object[] args)
53      {
54          facesContext.addMessage(component.getClientId(facesContext),
55                                  getMessage(facesContext,
56                                             facesContext.getViewRoot().getLocale(),
57                                             FacesMessage.SEVERITY_ERROR,
58                                             messageId,
59                                             args));
60      }
61  
62      static void addErrorMessage(FacesContext facesContext,
63              UIComponent component, Throwable cause)
64      {
65          facesContext.addMessage(component.getClientId(facesContext),
66                  new FacesMessage(FacesMessage.SEVERITY_ERROR, cause
67                          .getLocalizedMessage(), cause.getLocalizedMessage()));
68      }
69  
70      
71      static FacesMessage getMessage(FacesContext facesContext,
72                                     Locale locale,
73                                     FacesMessage.Severity severity,
74                                     String messageId,
75                                     Object args[])
76      {
77          ResourceBundle appBundle;
78          ResourceBundle defBundle;
79          String summary;
80          String detail;
81  
82          appBundle = getApplicationBundle(facesContext, locale);
83          summary = getBundleString(appBundle, messageId);
84          if (summary != null)
85          {
86              detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
87          }
88          else
89          {
90              defBundle = getDefaultBundle(facesContext, locale);
91              summary = getBundleString(defBundle, messageId);
92              if (summary != null)
93              {
94                  detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
95              }
96              else
97              {
98                  //Try to find detail alone
99                  detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
100                 if (detail != null)
101                 {
102                     summary = null;
103                 }
104                 else
105                 {
106                     detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
107                     if (detail != null)
108                     {
109                         summary = null;
110                     }
111                     else
112                     {
113                         //Neither detail nor summary found
114                         facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle");
115                         return new FacesMessage(severity, messageId, null);
116                     }
117                 }
118             }
119         }
120 
121         if (args != null && args.length > 0)
122         {
123             return new _ParametrizableFacesMessage(severity, summary, detail, args, locale);
124         }
125         else
126         {
127             return new FacesMessage(severity, summary, detail);
128         }
129     }
130 
131     private static String getBundleString(ResourceBundle bundle, String key)
132     {
133         try
134         {
135             return bundle == null ? null : bundle.getString(key);
136         }
137         catch (MissingResourceException e)
138         {
139             return null;
140         }
141     }
142 
143 
144     private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale)
145     {
146         String bundleName = facesContext.getApplication().getMessageBundle();
147         return bundleName != null ? getBundle(facesContext, locale, bundleName) : null;
148     }
149 
150     private static ResourceBundle getDefaultBundle(FacesContext facesContext,
151                                                    Locale locale)
152     {
153         return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
154     }
155 
156     private static ResourceBundle getBundle(FacesContext facesContext,
157                                             Locale locale,
158                                             String bundleName)
159     {
160         try
161         {
162             //First we try the JSF implementation class loader
163             return ResourceBundle.getBundle(bundleName,
164                                             locale,
165                                             facesContext.getClass().getClassLoader());
166         }
167         catch (MissingResourceException ignore1)
168         {
169             try
170             {
171                 //Next we try the JSF API class loader
172                 return ResourceBundle.getBundle(bundleName,
173                                                 locale,
174                                                 _MessageUtils.class.getClassLoader());
175             }
176             catch (MissingResourceException ignore2)
177             {
178                 try
179                 {
180                     //Last resort is the context class loader
181                     return ResourceBundle.getBundle(bundleName,
182                                                     locale,
183                                                     Thread.currentThread().getContextClassLoader());
184                 }
185                 catch (MissingResourceException damned)
186                 {
187                     facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
188                     return null;
189                 }
190             }
191         }
192     }
193     
194     static Object getLabel(FacesContext facesContext, UIComponent component) {
195         Object label = component.getAttributes().get("label");
196         if(label != null)
197             return label;
198         
199         ValueExpression expression = component.getValueExpression("label");
200         if(expression != null)
201             return expression;
202         
203         //If no label is not specified, use clientId
204         return component.getClientId( facesContext );
205     }
206 }