Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you 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 javax.faces.component;
 

Author(s):
Manfred Geiler (latest modification by $Author: grantsmith $)
Version:
$Revision: 472555 $ $Date: 2006-11-08 18:30:58 +0100 (Mi, 08 Nov 2006) $
 
 {
     private static final String DETAIL_SUFFIX = "_detail";
 
 	static void addErrorMessage(FacesContext facesContext,
 								UIComponent component,
 								String messageId)
 	{
 		facesContext.addMessage(component.getClientId(facesContext),
 								getMessage(facesContext,
 										   facesContext.getViewRoot().getLocale(),
 										   messageId,
 										   null));
 	}
 
 	static void addErrorMessage(FacesContext facesContext,
 								UIComponent component,
 								String messageIdObject[] args)
 	{
 		facesContext.addMessage(component.getClientId(facesContext),
 								getMessage(facesContext,
 										   facesContext.getViewRoot().getLocale(),
 										   messageId,
 										   args));
 	}
 
     static void addErrorMessage(FacesContext facesContext,
             UIComponent componentThrowable cause)
     {
         StringBuffer buf = new StringBuffer();
 
         while(cause != null && cause.getCause()!=cause)
         {
             if(buf.length()>0)
                 buf.append(", ");
             
             buf.append(cause.getLocalizedMessage());
 
             cause = cause.getCause();
         }
 
         facesContext.addMessage(component.getClientId(facesContext),
                 new FacesMessage(.buf.toString(), buf.toString()));
     }
 
     
     static FacesMessage getMessage(FacesContext facesContext,
                                    Locale locale,
                                    FacesMessage.Severity severity,
                                    String messageId,
                                    Object args[])
     {
         ResourceBundle appBundle;
         ResourceBundle defBundle;
         String summary;
         String detail;
 
         appBundle = getApplicationBundle(facesContextlocale);
         summary = getBundleString(appBundlemessageId);
         if (summary != null)
         {
             detail = getBundleString(appBundlemessageId + );
         }
         else
         {
             defBundle = getDefaultBundle(facesContextlocale);
            summary = getBundleString(defBundlemessageId);
            if (summary != null)
            {
                detail = getBundleString(defBundlemessageId + );
            }
            else
            {
                //Try to find detail alone
                detail = getBundleString(appBundlemessageId + );
                if (detail != null)
                {
                    summary = null;
                }
                else
                {
                    detail = getBundleString(defBundlemessageId + );
                    if (detail != null)
                    {
                        summary = null;
                    }
                    else
                    {
                        //Neither detail nor summary found
                        facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle");
                        return new FacesMessage(severitymessageIdnull);
                    }
                }
            }
        }
        if (args != null && args.length > 0)
        {
            MessageFormat format;
            if (summary != null)
            {
                format = new MessageFormat(summarylocale);
                summary = format.format(args);
            }
            if (detail != null)
            {
                format = new MessageFormat(detaillocale);
                detail = format.format(args);
            }
        }
        return new FacesMessage(severitysummarydetail);
    }
    private static String getBundleString(ResourceBundle bundleString key)
    {
        try
        {
            return bundle == null ? null : bundle.getString(key);
        }
        catch (MissingResourceException e)
        {
            return null;
        }
    }
    private static ResourceBundle getApplicationBundle(FacesContext facesContextLocale locale)
    {
        String bundleName = facesContext.getApplication().getMessageBundle();
        if (bundleName != null)
        {
            return getBundle(facesContextlocalebundleName);
        }
        else
        {
            return null;
        }
    }
    private static ResourceBundle getDefaultBundle(FacesContext facesContext,
                                                   Locale locale)
    {
        return getBundle(facesContextlocale.);
    }
    private static ResourceBundle getBundle(FacesContext facesContext,
                                            Locale locale,
                                            String bundleName)
    {
        try
        {
            //First we try the JSF implementation class loader
            return ResourceBundle.getBundle(bundleName,
                                            locale,
                                            facesContext.getClass().getClassLoader());
        }
        catch (MissingResourceException ignore1)
        {
            try
            {
                //Next we try the JSF API class loader
                return ResourceBundle.getBundle(bundleName,
                                                locale,
                                                _MessageUtils.class.getClassLoader());
            }
            catch (MissingResourceException ignore2)
            {
                try
                {
                    //Last resort is the context class loader
                    return ResourceBundle.getBundle(bundleName,
                                                    locale,
                                                    Thread.currentThread().getContextClassLoader());
                }
                catch (MissingResourceException damned)
                {
                    facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
                    return null;
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X