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.util.Locale;
26  import java.util.MissingResourceException;
27  import java.util.ResourceBundle;
28  
29  /**
30   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
31   * @version $Revision: 1378434 $ $Date: 2012-08-29 03:19:55 +0000 (Wed, 29 Aug 2012) $
32   */
33  class _MessageUtils
34  {
35      private static final String DETAIL_SUFFIX = "_detail";
36  
37      static void addErrorMessage(FacesContext facesContext,
38                                  UIComponent component,
39                                  String messageId)
40      {
41          facesContext.addMessage(component.getClientId(facesContext),
42                                  getMessage(facesContext,
43                                             facesContext.getViewRoot().getLocale(),
44                                             FacesMessage.SEVERITY_ERROR,
45                                             messageId,
46                                             null));
47      }
48  
49      static void addErrorMessage(FacesContext facesContext,
50                                  UIComponent component,
51                                  String messageId, Object[] args)
52      {
53          facesContext.addMessage(component.getClientId(facesContext),
54                                  getMessage(facesContext,
55                                             facesContext.getViewRoot().getLocale(),
56                                             FacesMessage.SEVERITY_ERROR,
57                                             messageId,
58                                             args));
59      }
60  
61      static void addErrorMessage(FacesContext facesContext,
62              UIComponent component, Throwable cause)
63      {
64          facesContext.addMessage(component.getClientId(facesContext),
65                  new FacesMessage(FacesMessage.SEVERITY_ERROR, cause
66                          .getLocalizedMessage(), cause.getLocalizedMessage()));
67      }
68  
69      
70      static FacesMessage getMessage(FacesContext facesContext,
71                                     Locale locale,
72                                     FacesMessage.Severity severity,
73                                     String messageId,
74                                     Object args[])
75      {
76          ResourceBundle appBundle;
77          ResourceBundle defBundle;
78          String summary;
79          String detail;
80  
81          appBundle = getApplicationBundle(facesContext, locale);
82          summary = getBundleString(appBundle, messageId);
83          if (summary != null)
84          {
85              detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
86          }
87          else
88          {
89              defBundle = getDefaultBundle(facesContext, locale);
90              summary = getBundleString(defBundle, messageId);
91              if (summary != null)
92              {
93                  detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
94              }
95              else
96              {
97                  //Try to find detail alone
98                  detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
99                  if (detail != null)
100                 {
101                     summary = null;
102                 }
103                 else
104                 {
105                     detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
106                     if (detail != null)
107                     {
108                         summary = null;
109                     }
110                     else
111                     {
112                         //Neither detail nor summary found
113                         facesContext.getExternalContext().log("No message with id " + messageId
114                                                               + " 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                                                     _ClassUtils.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     {
196         Object label = component.getAttributes().get("label");
197         ValueExpression expression = null;
198         if (label != null && 
199             label instanceof String && ((String)label).length() == 0 )
200         {
201             // Note component.getAttributes().get("label") internally try to 
202             // evaluate the EL expression for the label, but in some cases, 
203             // when PSS is disabled and f:loadBundle is used, when the view is 
204             // restored the bundle is not set to the EL expression returns an 
205             // empty String. It is not possible to check if there is a 
206             // hardcoded label, but we can check if there is
207             // an EL expression set, so the best in this case is use that, and if
208             // there is an EL expression set, use it, otherwise use the hardcoded
209             // value. See MYFACES-3591 for details.
210             expression = component.getValueExpression("label");
211             if (expression != null)
212             {
213                 // Set the label to null and use the EL expression instead.
214                 label = null;
215             }
216         }
217             
218         if(label != null)
219         {
220             return label;
221         }
222         
223         expression = (expression == null) ? component.getValueExpression("label") : expression;
224         if(expression != null)
225         {
226             return expression;
227         }
228         
229         //If no label is not specified, use clientId
230         return component.getClientId( facesContext );
231     }
232 }