Save This Page
Home » mojarra-1.2_09-b02-FCS-source » javax.faces.convert » [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.convert;
   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 FacesMessage getErrorMessage(FacesContext facesContext,
   34                                           String messageId,
   35                                           Object args[])
   36       {
   37           return getMessage(facesContext,
   38                             facesContext.getViewRoot().getLocale(),
   39                             FacesMessage.SEVERITY_ERROR,
   40                             messageId,
   41                             args);
   42       }
   43   
   44       static FacesMessage getMessage(FacesContext facesContext,
   45                                      Locale locale,
   46                                      FacesMessage.Severity severity,
   47                                      String messageId,
   48                                      Object args[])
   49       {
   50           ResourceBundle appBundle;
   51           ResourceBundle defBundle;
   52           String summary;
   53           String detail;
   54   
   55           appBundle = getApplicationBundle(facesContext, locale);
   56           summary = getBundleString(appBundle, messageId);
   57           if (summary != null)
   58           {
   59               detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
   60           }
   61           else
   62           {
   63               defBundle = getDefaultBundle(facesContext, locale);
   64               summary = getBundleString(defBundle, messageId);
   65               if (summary != null)
   66               {
   67                   detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
   68               }
   69               else
   70               {
   71                   //Try to find detail alone
   72                   detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
   73                   if (detail != null)
   74                   {
   75                       summary = null;
   76                   }
   77                   else
   78                   {
   79                       detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
   80                       if (detail != null)
   81                       {
   82                           summary = null;
   83                       }
   84                       else
   85                       {
   86                           //Neither detail nor summary found
   87                           return null;
   88                       }
   89                   }
   90               }
   91           }
   92   
   93           if (args != null && args.length > 0)
   94           {
   95               MessageFormat format;
   96   
   97               if (summary != null)
   98               {
   99                   format = new MessageFormat(summary, locale);
  100                   summary = format.format(args);
  101               }
  102   
  103               if (detail != null)
  104               {
  105                   format = new MessageFormat(detail, locale);
  106                   detail = format.format(args);
  107               }
  108           }
  109   
  110           return new FacesMessage(severity, summary, detail);
  111       }
  112   
  113   
  114       private static String getBundleString(ResourceBundle bundle, String key)
  115       {
  116           try
  117           {
  118               return bundle == null ? null : bundle.getString(key);
  119           }
  120           catch (MissingResourceException e)
  121           {
  122               return null;
  123           }
  124       }
  125   
  126   
  127       private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale)
  128       {
  129           String bundleName = facesContext.getApplication().getMessageBundle();
  130           if (bundleName != null)
  131           {
  132               return getBundle(facesContext, locale, bundleName);
  133           }
  134           else
  135           {
  136               return null;
  137           }
  138       }
  139   
  140       private static ResourceBundle getDefaultBundle(FacesContext facesContext,
  141                                                      Locale locale)
  142       {
  143           return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
  144       }
  145   
  146       private static ResourceBundle getBundle(FacesContext facesContext,
  147                                               Locale locale,
  148                                               String bundleName)
  149       {
  150           try
  151           {
  152               //First we try the JSF implementation class loader
  153               return ResourceBundle.getBundle(bundleName,
  154                                               locale,
  155                                               facesContext.getClass().getClassLoader());
  156           }
  157           catch (MissingResourceException ignore1)
  158           {
  159               try
  160               {
  161                   //Next we try the JSF API class loader
  162                   return ResourceBundle.getBundle(bundleName,
  163                                                   locale,
  164                                                   _MessageUtils.class.getClassLoader());
  165               }
  166               catch (MissingResourceException ignore2)
  167               {
  168                   try
  169                   {
  170                       //Last resort is the context class loader
  171                       return ResourceBundle.getBundle(bundleName,
  172                                                       locale,
  173                                                       Thread.currentThread().getContextClassLoader());
  174                   }
  175                   catch (MissingResourceException damned)
  176                   {
  177                       facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
  178                       return null;
  179                   }
  180               }
  181           }
  182       }
  183   
  184   }

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