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