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        final FacesContext facesContext, final UIComponent component, final FacesMessage.Severity severity,
52        final String messageId, final 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        final FacesContext facesContext, final Locale locale,
59        final FacesMessage.Severity severity, final String messageId, final Object... args) {
60  
61      final 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        final 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          final 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(final ResourceBundle bundle, final String key) {
107     try {
108       return bundle == null ? null : bundle.getString(key);
109     } catch (final MissingResourceException e) {
110       return null;
111     }
112   }
113 
114   private static ResourceBundle getApplicationBundle(final FacesContext facesContext, final Locale locale) {
115     final String bundleName = facesContext.getApplication().getMessageBundle();
116     return bundleName != null ? getBundle(facesContext, locale, bundleName) : null;
117   }
118 
119   private static ResourceBundle getDefaultBundle(final FacesContext facesContext, final Locale locale) {
120     return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
121   }
122 
123   private static ResourceBundle getBundle(
124       final FacesContext facesContext, final Locale locale, final String bundleName) {
125     try {
126       return ResourceBundle.getBundle(bundleName, locale, MessageUtils.class.getClassLoader());
127     } catch (final MissingResourceException ignore2) {
128       try {
129         return ResourceBundle.getBundle(bundleName, locale, Thread.currentThread().getContextClassLoader());
130       } catch (final MissingResourceException damned) {
131         facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
132         return null;
133       }
134     }
135   }
136 
137   public static String getLabel(final FacesContext facesContext, final UIComponent component) {
138     final Object label = component.getAttributes().get("label");
139     if (label != null) {
140       return label.toString();
141     }
142     final ValueExpression expression = component.getValueExpression("label");
143     if (expression != null) {
144       return expression.getExpressionString();
145     }
146     return component.getClientId(facesContext);
147   }
148 
149   /**
150    * @deprecated Since Tobago 2.0.0
151    */
152   @Deprecated
153   public static String getFormatedMessage(final String message, final Locale locale, final Object... args) {
154     if (args != null && args.length > 0 && message != null) {
155       final MessageFormat format = new MessageFormat(message, locale);
156       return format.format(args);
157     }
158     return message;
159   }
160 }