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