All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.icesoft.faces.utils.MessageUtils Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 * Copyright 2004-2012 ICEsoft Technologies Canada Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS
 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package com.icesoft.faces.utils;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;

/**
 * This class has been designed, so the custom components can get 
 * facesMessages either from the icefaces' ResourceBundle or an 
 * application's resourceBundle. The location of ice's messages.properties
 * is under com.icesoft.faces.resources package. 
 */

public class MessageUtils {
    private static Log log = LogFactory.getLog(MessageUtils.class);
    private static String DETAIL_SUFFIX = "_detail";
    private static int SUMMARY = 0;
    private static int DETAIL = 1;
    private static String ICE_MESSAGES_BUNDLE = "com.icesoft.faces.resources.messages";
    public static FacesMessage getMessage(FacesContext context, 
            String messageId) {
        return getMessage(context, messageId, null);
    }
    
    public static FacesMessage getMessage(FacesContext facesContext, 
            String messageId, Object params[]) {
        String messageInfo[] = new String[2];
        
        Locale locale = facesContext.getViewRoot().getLocale();
        String bundleName = facesContext.getApplication().getMessageBundle();
        //see if the message has been overridden by the application
        if (bundleName != null) {
            try {
                loadMessageInfo(bundleName, locale, messageId, messageInfo);
            } catch (Exception e)  {
                if(log.isWarnEnabled())
                    log.warn(e + ", using " + ICE_MESSAGES_BUNDLE);
            }
        }
        
        //TODO Use defered evaluation of the parameters, like how
        // JSF 1.2's javax.faces.component.MessageFactory.
        // BindingFacesMessage does. ICE-2290.
        
        //if not overridden then check in Icefaces message bundle.
        if (messageInfo[SUMMARY] == null && messageInfo[DETAIL]== null) {
            loadMessageInfo(ICE_MESSAGES_BUNDLE, locale, messageId, messageInfo);
        }
        if (params != null) {
            MessageFormat format;
            for (int i= 0; i 




© 2015 - 2024 Weber Informatics LLC | Privacy Policy