Coverage Report - javax.faces.convert._MessageUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
_MessageUtils
0%
0/42
0%
0/20
3.714
 
 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.convert;
 20  
 
 21  
 import javax.el.ValueExpression;
 22  
 import javax.faces.application.FacesMessage;
 23  
 import javax.faces.component.UIComponent;
 24  
 import javax.faces.context.FacesContext;
 25  
 
 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
         appBundle = getApplicationBundle(facesContext, locale);
 62  0
         summary = getBundleString(appBundle, messageId);
 63  0
         if (summary != null)
 64  
         {
 65  0
             detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
 66  
         }
 67  
         else
 68  
         {
 69  0
             defBundle = getDefaultBundle(facesContext, locale);
 70  0
             summary = getBundleString(defBundle, messageId);
 71  0
             if (summary != null)
 72  
             {
 73  0
                 detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
 74  
             }
 75  
             else
 76  
             {
 77  
                 //Try to find detail alone
 78  0
                 detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX);
 79  0
                 if (detail != null)
 80  
                 {
 81  0
                     summary = null;
 82  
                 }
 83  
                 else
 84  
                 {
 85  0
                     detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX);
 86  0
                     if (detail != null)
 87  
                     {
 88  0
                         summary = null;
 89  
                     }
 90  
                     else
 91  
                     {
 92  
                         //Neither detail nor summary found
 93  0
                         facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle");
 94  0
                         return new FacesMessage(severity, messageId, null);
 95  
                     }
 96  
                 }
 97  
             }
 98  
         }
 99  
 
 100  0
         if (args != null && args.length > 0)
 101  
         {
 102  0
             return new _ParametrizableFacesMessage(severity, summary, detail, args, locale);
 103  
         }
 104  
         else
 105  
         {
 106  0
             return new FacesMessage(severity, summary, detail);
 107  
         }
 108  
     }
 109  
 
 110  
     private static String getBundleString(ResourceBundle bundle, String key)
 111  
     {
 112  
         try
 113  
         {
 114  0
             return bundle == null ? null : bundle.getString(key);
 115  
         }
 116  0
         catch (MissingResourceException e)
 117  
         {
 118  0
             return null;
 119  
         }
 120  
     }
 121  
 
 122  
 
 123  
     private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale)
 124  
     {
 125  0
         String bundleName = facesContext.getApplication().getMessageBundle();
 126  0
         return bundleName != null ? getBundle(facesContext, locale, bundleName) : null;
 127  
     }
 128  
 
 129  
     private static ResourceBundle getDefaultBundle(FacesContext facesContext,
 130  
                                                    Locale locale)
 131  
     {
 132  0
         return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES);
 133  
     }
 134  
 
 135  
     private static ResourceBundle getBundle(FacesContext facesContext,
 136  
                                             Locale locale,
 137  
                                             String bundleName)
 138  
     {
 139  
         try
 140  
         {
 141  
             //First we try the JSF implementation class loader
 142  0
             return ResourceBundle.getBundle(bundleName,
 143  
                                             locale,
 144  
                                             facesContext.getClass().getClassLoader());
 145  
         }
 146  0
         catch (MissingResourceException ignore1)
 147  
         {
 148  
             try
 149  
             {
 150  
                 //Next we try the JSF API class loader
 151  0
                 return ResourceBundle.getBundle(bundleName,
 152  
                                                 locale,
 153  
                                                 _MessageUtils.class.getClassLoader());
 154  
             }
 155  0
             catch (MissingResourceException ignore2)
 156  
             {
 157  
                 try
 158  
                 {
 159  
                     //Last resort is the context class loader
 160  0
                     return ResourceBundle.getBundle(bundleName,
 161  
                                                     locale,
 162  
                                                     Thread.currentThread().getContextClassLoader());
 163  
                 }
 164  0
                 catch (MissingResourceException damned)
 165  
                 {
 166  0
                     facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found");
 167  0
                     return null;
 168  
                 }
 169  
             }
 170  
         }
 171  
     }
 172  
     
 173  
     static Object getLabel(FacesContext facesContext, UIComponent component) {
 174  0
         Object label = component.getAttributes().get("label");
 175  0
         if(label != null)
 176  0
             return label;
 177  
         
 178  0
         ValueExpression expression = component.getValueExpression("label");
 179  0
         if(expression != null)
 180  0
             return expression;
 181  
         
 182  
         //If no label is not specified, use clientId
 183  0
         return component.getClientId( facesContext );
 184  
     }
 185  
 }