Save This Page
Home » mojarra-1.2_09-b02-FCS-source » javax.faces.component » [javadoc | source]
    1   /*
    2    * Copyright 2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   package javax.faces.component;
   17   
   18   import javax.faces.application.FacesMessage;
   19   import javax.faces.context.FacesContext;
   20   import java.text.MessageFormat;
   21   import java.util.Locale;
   22   import java.util.MissingResourceException;
   23   import java.util.ResourceBundle;
   24   
   25   /**
   26    * @author Manfred Geiler (latest modification by $Author: grantsmith $)
   27    * @version $Revision: 169646 $ $Date: 2005-05-11 11:34:57 -0400 (Wed, 11 May 2005) $
   28    */
   29   class _MessageUtils
   30   {
   31       private static final String DETAIL_SUFFIX = "_detail";
   32   
   33   	static void addErrorMessage(FacesContext facesContext,
   34   								UIComponent component,
   35   								String messageId)
   36   	{
   37   		facesContext.addMessage(component.getClientId(facesContext),
   38   								getMessage(facesContext,
   39   										   facesContext.getViewRoot().getLocale(),
   40   										   FacesMessage.SEVERITY_ERROR,
   41   										   messageId,
   42   										   null));
   43   	}
   44   
   45   	static void addErrorMessage(FacesContext facesContext,
   46   								UIComponent component,
   47   								String messageId, Object[] args)
   48   	{
   49   		facesContext.addMessage(component.getClientId(facesContext),
   50   								getMessage(facesContext,
   51   										   facesContext.getViewRoot().getLocale(),
   52   										   FacesMessage.SEVERITY_ERROR,
   53   										   messageId,
   54   										   args));
   55   	}
   56   
   57       static FacesMessage getMessage(FacesContext facesContext,
   58                                      Locale locale,
   59                                      FacesMessage.Severity severity,
   60                                      String messageId,
   61                                      Object args[])
   62       {
   63           ResourceBundle appBundle;
   64           ResourceBundle defBundle;
   65           String summary;
   66           String detail;
   67   
   68           appBundle = getApplicationBundle(facesContext, locale);
   69           summary = getBundleString(appBundle, messageId);
   70           if (summary != null)
   71           {
   72               detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
   73           }
   74           else
   75           {
   76               defBundle = getDefaultBundle(facesContext, locale);
   77               summary = getBundleString(defBundle, messageId);
   78               if (summary != null)
   79               {
   80                   detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
   81               }
   82               else
   83               {
   84                   //Try to find detail alone
   85                   detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
   86                   if (detail != null)
   87                   {
   88                       summary = null;
   89                   }
   90                   else
   91                   {
   92                       detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
   93                       if (detail != null)
   94                       {
   95                           summary = null;
   96                       }
   97                       else
   98                       {
   99                           //Neither detail nor summary found
  100                           facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle");
  101                           return new FacesMessage(severity, messageId, null);
  102                       }
  103                   }
  104               }
  105           }
  106   
  107           if (args != null && args.length > 0)
  108           {
  109               MessageFormat format;
  110   
  111               if (summary != null)
  112               {
  113                   format = new MessageFormat(summary, locale);
  114                   summary = format.format(args);
  115               }
  116   
  117               if (detail != null)
  118               {
  119                   format = new MessageFormat(detail, locale);
  120                   detail = format.format(args);
  121               }
  122           }
  123   
  124           return new FacesMessage(severity, summary, detail);
  125       }
  126   
  127   
  128       private static String getBundleString(ResourceBundle bundle, String key)
  129       {
  130           try
  131           {
  132               return bundle == null ? null : bundle.getString(key);
  133           }
  134           catch (MissingResourceException e)
  135           {
  136               return null;
  137           }
  138       }
  139   
  140   
  141       private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale)
  142       {
  143           String bundleName = facesContext.getApplication().getMessageBundle();
  144           if (bundleName != null)
  145           {
  146               return getBundle(facesContext, locale, bundleName);
  147           }
  148           else
  149           {
  150               return null;
  151           }
  152       }
  153   
  154       private static ResourceBundle getDefaultBundle(FacesContext facesContext,
  155                                                      Locale locale)
  156       {
  157           return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
  158       }
  159   
  160       private static ResourceBundle getBundle(FacesContext facesContext,
  161                                               Locale locale,
  162                                               String bundleName)
  163       {
  164           try
  165           {
  166               //First we try the JSF implementation class loader
  167               return ResourceBundle.getBundle(bundleName,
  168                                               locale,
  169                                               facesContext.getClass().getClassLoader());
  170           }
  171           catch (MissingResourceException ignore1)
  172           {
  173               try
  174               {
  175                   //Next we try the JSF API class loader
  176                   return ResourceBundle.getBundle(bundleName,
  177                                                   locale,
  178                                                   _MessageUtils.class.getClassLoader());
  179               }
  180               catch (MissingResourceException ignore2)
  181               {
  182                   try
  183                   {
  184                       //Last resort is the context class loader
  185                       return ResourceBundle.getBundle(bundleName,
  186                                                       locale,
  187                                                       Thread.currentThread().getContextClassLoader());
  188                   }
  189                   catch (MissingResourceException damned)
  190                   {
  191                       facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
  192                       return null;
  193                   }
  194               }
  195           }
  196       }
  197   
  198   }

Save This Page
Home » mojarra-1.2_09-b02-FCS-source » javax.faces.component » [javadoc | source]