Save This Page
Home » mojarra-1.2_09-b02-FCS-source » javax.faces.validator » [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.validator;
   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: mwessendorf $)
   27    * @version $Revision: 166994 $ $Date: 2004-07-01 18:12:40 -0400 (Thu, 01 Jul 2004) $
   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                           facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle");
   88                           return new FacesMessage(severity, messageId, null);
   89                       }
   90                   }
   91               }
   92           }
   93   
   94           if (args != null && args.length > 0)
   95           {
   96               MessageFormat format;
   97   
   98               if (summary != null)
   99               {
  100                   format = new MessageFormat(summary, locale);
  101                   summary = format.format(args);
  102               }
  103   
  104               if (detail != null)
  105               {
  106                   format = new MessageFormat(detail, locale);
  107                   detail = format.format(args);
  108               }
  109           }
  110   
  111           return new FacesMessage(severity, summary, detail);
  112       }
  113   
  114   
  115       private static String getBundleString(ResourceBundle bundle, String key)
  116       {
  117           try
  118           {
  119               return bundle == null ? null : bundle.getString(key);
  120           }
  121           catch (MissingResourceException e)
  122           {
  123               return null;
  124           }
  125       }
  126   
  127   
  128       private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale)
  129       {
  130           String bundleName = facesContext.getApplication().getMessageBundle();
  131           if (bundleName != null)
  132           {
  133               return getBundle(facesContext, locale, bundleName);
  134           }
  135           else
  136           {
  137               return null;
  138           }
  139       }
  140   
  141       private static ResourceBundle getDefaultBundle(FacesContext facesContext,
  142                                                      Locale locale)
  143       {
  144           return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
  145       }
  146   
  147       private static ResourceBundle getBundle(FacesContext facesContext,
  148                                               Locale locale,
  149                                               String bundleName)
  150       {
  151           try
  152           {
  153               //First we try the JSF implementation class loader
  154               return ResourceBundle.getBundle(bundleName,
  155                                               locale,
  156                                               facesContext.getClass().getClassLoader());
  157           }
  158           catch (MissingResourceException ignore1)
  159           {
  160               try
  161               {
  162                   //Next we try the JSF API class loader
  163                   return ResourceBundle.getBundle(bundleName,
  164                                                   locale,
  165                                                   _MessageUtils.class.getClassLoader());
  166               }
  167               catch (MissingResourceException ignore2)
  168               {
  169                   try
  170                   {
  171                       //Last resort is the context class loader
  172                       return ResourceBundle.getBundle(bundleName,
  173                                                       locale,
  174                                                       Thread.currentThread().getContextClassLoader());
  175                   }
  176                   catch (MissingResourceException damned)
  177                   {
  178                       facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
  179                       return null;
  180                   }
  181               }
  182           }
  183       }
  184   
  185   }

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