001    package org.apache.myfaces.tobago.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.myfaces.tobago.context.ResourceManagerUtil;
023    
024    import javax.faces.application.FacesMessage;
025    import javax.faces.context.FacesContext;
026    import java.util.HashMap;
027    import java.util.Locale;
028    import java.util.Map;
029    import java.util.ResourceBundle;
030    import java.text.MessageFormat;
031    
032    /*
033     * User: weber
034     * Date: Jun 14, 2005
035     * Time: 5:40:04 PM
036     */
037    public class MessageFactory {
038    
039      private static final Log LOG = LogFactory.getLog(MessageFactory.class);
040    
041      private static Map<Locale, ResourceBundle> facesMessagesMap
042          = new HashMap<Locale, ResourceBundle>();
043    
044      public static FacesMessage createFacesMessage(FacesContext facesContext,
045          String key, FacesMessage.Severity severity, Object[] args) {
046        return createFacesMessage(facesContext, "tobago", key, severity, args);
047      }
048    
049      public static FacesMessage createFacesMessage(FacesContext facesContext,
050          String key, FacesMessage.Severity severity) {
051        return createFacesMessage(facesContext, key, severity, new Object[0]);
052      }
053    
054      public static FacesMessage createFacesMessage(FacesContext facesContext,
055          String bundle, String key, FacesMessage.Severity severity, Object[] args) {
056        String summary = getMessageText(facesContext, bundle, key);
057        String detail = getMessageText(facesContext, bundle, key + "_detail");
058        if (args != null && args.length > 0) {
059          if (summary != null) {
060            MessageFormat format = new MessageFormat(summary, facesContext.getViewRoot().getLocale());
061            summary = format.format(args);
062          }
063    
064          if (detail != null) {
065            MessageFormat format = new MessageFormat(detail, facesContext.getViewRoot().getLocale());
066            detail = format.format(args);
067          }
068        }
069        return new FacesMessage(severity, summary != null ? summary : key, detail);
070      }
071    
072      public static FacesMessage createFacesMessage(FacesContext facesContext,
073          String bundle, String key, FacesMessage.Severity severity) {
074        return createFacesMessage(facesContext, bundle, key, severity, new Object[0]);
075      }
076    
077      private static String getMessageText(
078          FacesContext facesContext, String bundle, String key) {
079        String message = ResourceManagerUtil.getProperty(facesContext, bundle, key);
080        if (message == null || message.length() < 1) {
081          try {
082            Locale locale = facesContext.getViewRoot().getLocale();
083            message = getFacesMessages(locale).getString(key);
084          } catch (Exception e) {
085            /* ignore at this point */
086          }
087        }
088        return message;
089      }
090    
091      private static ResourceBundle getFacesMessages(Locale locale) {
092        ResourceBundle facesMessages = facesMessagesMap.get(locale);
093        if (facesMessages == null) {
094          facesMessages
095              = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES, locale);
096          facesMessagesMap.put(locale, facesMessages);
097        }
098        return facesMessages;
099      }
100    }