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.context.ResourceManagerUtils;
23  
24  import javax.faces.application.FacesMessage;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import java.text.MessageFormat;
28  import java.util.HashMap;
29  import java.util.Locale;
30  import java.util.Map;
31  import java.util.ResourceBundle;
32  
33  /**
34   * @deprecated since Tobago 1.5.0. Please use {@link MessageUtils} instead
35   */
36  @Deprecated
37  public final class MessageFactory {
38  
39    private MessageFactory() {
40      // utils class
41    }
42  
43    private static final Map<Locale, ResourceBundle> FACES_MESSAGES_MAP = new HashMap<Locale, ResourceBundle>();
44  
45    @Deprecated
46    public static FacesMessage createFacesMessage(
47        final FacesContext facesContext, final String key, final FacesMessage.Severity severity, final Object[] args) {
48      return createFacesMessage(facesContext, "tobago", key, severity, args);
49    }
50  
51    @Deprecated
52    public static FacesMessage createFacesMessage(
53        final FacesContext facesContext, final String key, final FacesMessage.Severity severity) {
54      return createFacesMessage(facesContext, key, severity, new Object[0]);
55    }
56  
57    @Deprecated
58    public static FacesMessage createFacesMessage(
59        final FacesContext facesContext, final String bundle, final String key, final FacesMessage.Severity severity,
60        final Object[] args) {
61      String summary = getMessageText(facesContext, bundle, key);
62      String detail = getMessageText(facesContext, bundle, key + "_detail");
63      if (args != null && args.length > 0) {
64        final Locale locale = getLocale(facesContext);
65        if (summary != null) {
66          final MessageFormat format = new MessageFormat(summary, locale);
67          summary = format.format(args);
68        }
69  
70        if (detail != null) {
71          final MessageFormat format = new MessageFormat(detail, locale);
72          detail = format.format(args);
73        }
74      }
75      return new FacesMessage(severity, summary != null ? summary : key, detail);
76    }
77  
78    @Deprecated
79    public static Locale getLocale(final FacesContext facesContext) {
80      final UIViewRoot root = facesContext.getViewRoot();
81      final Locale locale;
82      if (root != null) {
83        locale = root.getLocale();
84      } else {
85        locale = facesContext.getApplication().getViewHandler().calculateLocale(facesContext);
86      }
87      return locale;
88    }
89  
90    @Deprecated
91    public static FacesMessage createFacesMessage(
92        final FacesContext facesContext, final String bundle, final String key, final FacesMessage.Severity severity) {
93      return createFacesMessage(facesContext, bundle, key, severity, new Object[0]);
94    }
95  
96    @Deprecated
97    public static String getMessageText(
98        final FacesContext facesContext, final String bundle, final String key) {
99      String message = ResourceManagerUtils.getProperty(facesContext, bundle, key);
100     if (message == null || message.length() < 1) {
101       try {
102         final Locale locale = getLocale(facesContext);
103         message = getFacesMessages(locale).getString(key);
104       } catch (final Exception e) {
105         /* ignore at this point */
106       }
107     }
108     return message;
109   }
110 
111   @Deprecated
112   public static ResourceBundle getFacesMessages(final Locale locale) {
113     ResourceBundle facesMessages = FACES_MESSAGES_MAP.get(locale);
114     if (facesMessages == null) {
115       facesMessages
116           = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES, locale);
117       FACES_MESSAGES_MAP.put(locale, facesMessages);
118     }
119     return facesMessages;
120   }
121 }