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  
20  package org.apache.myfaces.tobago.util;
21  
22  import org.apache.myfaces.tobago.application.LabelValueExpressionFacesMessage;
23  import org.apache.myfaces.tobago.context.TobagoResourceBundle;
24  
25  import javax.el.ValueExpression;
26  import javax.faces.application.FacesMessage;
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import java.text.MessageFormat;
30  import java.util.Locale;
31  import java.util.MissingResourceException;
32  import java.util.ResourceBundle;
33  
34  /**
35   * Utility to create and add {@link FacesMessage} object to the context.
36   * The message will be internationalized with a bundle in the following order:
37   * <ol>
38   * <li>Application bundle</li>
39   * <li>Tobago bundle</li>
40   * <li>Default JSF bundle</li>
41   * </ol>
42   */
43  public final class MessageUtils {
44  
45    private static final String DETAIL_SUFFIX = "_detail";
46  
47    private MessageUtils() {
48    }
49  
50    public static void addMessage(
51        FacesContext facesContext, UIComponent component, FacesMessage.Severity severity,
52        String messageId, Object[] args) {
53      facesContext.addMessage(component.getClientId(facesContext),
54          getMessage(facesContext, facesContext.getViewRoot().getLocale(), severity, messageId, args));
55    }
56  
57    public static FacesMessage getMessage(
58        FacesContext facesContext, Locale locale,
59        FacesMessage.Severity severity, String messageId, Object... args) {
60  
61      ResourceBundle appBundle = getApplicationBundle(facesContext, locale);
62      String summary = getBundleString(appBundle, messageId);
63      String detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
64  
65      if (summary == null || detail == null) {
66        ResourceBundle tobagoBundle = new TobagoResourceBundle();
67        if (summary == null) {
68          summary = getBundleString(tobagoBundle, messageId);
69        }
70        if (detail == null) {
71          detail = getBundleString(tobagoBundle, messageId + DETAIL_SUFFIX);
72        }
73  
74        if (summary == null || detail == null) {
75          ResourceBundle defBundle = getDefaultBundle(facesContext, locale);
76          if (summary == null) {
77            summary = getBundleString(defBundle, messageId);
78          }
79          if (detail == null) {
80            detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
81          }
82        }
83      }
84  
85      if (summary == null && detail == null) {
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      if (args != null && args.length > 0) {
92        MessageFormat format;
93        if (summary != null) {
94          format = new MessageFormat(summary, locale);
95          summary = format.format(args);
96        }
97  
98        if (detail != null) {
99          format = new MessageFormat(detail, locale);
100         detail = format.format(args);
101       }
102     }
103     return new LabelValueExpressionFacesMessage(severity, summary, detail);
104   }
105 
106   private static String getBundleString(ResourceBundle bundle, String key) {
107     try {
108       return bundle == null ? null : bundle.getString(key);
109     } catch (MissingResourceException e) {
110       return null;
111     }
112   }
113 
114   private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale) {
115     String bundleName = facesContext.getApplication().getMessageBundle();
116     return bundleName != null ? getBundle(facesContext, locale, bundleName) : null;
117   }
118 
119   private static ResourceBundle getDefaultBundle(FacesContext facesContext, Locale locale) {
120     return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
121   }
122 
123   private static ResourceBundle getBundle(FacesContext facesContext, Locale locale, String bundleName) {
124     try {
125       return ResourceBundle.getBundle(bundleName, locale, MessageUtils.class.getClassLoader());
126     } catch (MissingResourceException ignore2) {
127       try {
128         return ResourceBundle.getBundle(bundleName, locale, Thread.currentThread().getContextClassLoader());
129       } catch (MissingResourceException damned) {
130         facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
131         return null;
132       }
133     }
134   }
135 
136   public static String getLabel(FacesContext facesContext, UIComponent component) {
137     Object label = component.getAttributes().get("label");
138     if (label != null) {
139       return label.toString();
140     }
141     final ValueExpression expression = component.getValueExpression("label");
142     if (expression != null) {
143       return expression.getExpressionString();
144     }
145     return component.getClientId(facesContext);
146   }
147 
148   /**
149    * @deprecated Since Tobago 2.0.0
150    */
151   @Deprecated
152   public static String getFormatedMessage(String message, Locale locale, Object... args) {
153     if (args != null && args.length > 0 && message != null) {
154       MessageFormat format = new MessageFormat(message, locale);
155       return format.format(args);
156     }
157     return message;
158   }
159 }