Coverage Report - org.apache.myfaces.shared_impl.renderkit._SharedRendererUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
_SharedRendererUtils
0%
0/70
0%
0/58
14.667
 
 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 org.apache.myfaces.shared_impl.renderkit;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 
 24  
 import javax.el.ValueExpression;
 25  
 import javax.faces.FacesException;
 26  
 import javax.faces.component.UIOutput;
 27  
 import javax.faces.component.UISelectMany;
 28  
 import javax.faces.context.FacesContext;
 29  
 import javax.faces.convert.Converter;
 30  
 import javax.faces.convert.ConverterException;
 31  
 import java.lang.reflect.Array;
 32  
 import java.util.ArrayList;
 33  
 import java.util.List;
 34  
 
 35  
 /**
 36  
  * The util methods in this class are shared between the javax.faces.component package and
 37  
  * the org.apache.myfaces.renderkit package.
 38  
  * Please note: Any changes here must also apply to the class in the other package!
 39  
  *
 40  
  * @author Manfred Geiler (latest modification by $Author: skitching $)
 41  
  * @version $Revision: 673827 $ $Date: 2008-07-03 16:46:23 -0500 (Thu, 03 Jul 2008) $
 42  
  */
 43  0
 class _SharedRendererUtils
 44  
 {
 45  
     static Converter findUIOutputConverter(FacesContext facesContext, UIOutput component)
 46  
     {
 47  
         // Attention!
 48  
         // This code is duplicated in jsfapi component package.
 49  
         // If you change something here please do the same in the other class!
 50  
 
 51  0
         Converter converter = component.getConverter();
 52  0
         if (converter != null) return converter;
 53  
 
 54  
         //Try to find out by value expression
 55  0
         ValueExpression expression = component.getValueExpression("value");
 56  0
         if (expression == null) return null;
 57  
 
 58  0
         Class valueType = expression.getType(facesContext.getELContext());
 59  0
         if (valueType == null) return null;
 60  
 
 61  0
         if (Object.class.equals(valueType)) return null;    //There is no converter for Object class
 62  
 
 63  
         try
 64  
         {
 65  0
             return facesContext.getApplication().createConverter(valueType);
 66  
         }
 67  0
         catch (FacesException e)
 68  
         {
 69  0
             log(facesContext, "No Converter for type " + valueType.getName() + " found", e);
 70  0
             return null;
 71  
         }
 72  
     }
 73  
 
 74  
     static Object getConvertedUISelectManyValue(FacesContext facesContext,
 75  
                                                 UISelectMany component,
 76  
                                                 String[] submittedValue)
 77  
             throws ConverterException
 78  
     {
 79  
         // Attention!
 80  
         // This code is duplicated in jsfapi component package.
 81  
         // If you change something here please do the same in the other class!
 82  
 
 83  0
         if (submittedValue == null) throw new NullPointerException("submittedValue");
 84  
 
 85  0
         ValueExpression expression = component.getValueExpression("value");
 86  0
         Class valueType = null;
 87  0
         Class arrayComponentType = null;
 88  0
         if (expression != null)
 89  
         {
 90  
             //By some strange reason vb.getType(facesContext.getELContext());
 91  
             //does not return the same as vb.getValue(facesContext.getELContext()).getClass(),
 92  
             //so we need to use this instead.
 93  0
             Object value = expression.getValue(facesContext.getELContext()); 
 94  0
             valueType = (value != null) ? value.getClass() :
 95  
                 expression.getType(facesContext.getELContext()) ;
 96  
             
 97  0
             if (valueType != null && valueType.isArray())
 98  
             {
 99  0
                 arrayComponentType = valueType.getComponentType();
 100  
             }
 101  
         }
 102  
         
 103  0
         Converter converter = component.getConverter();
 104  0
         if (converter == null)
 105  
         {
 106  0
             if (valueType == null)
 107  
             {
 108  
                 // No converter, and no idea of expected type
 109  
                 // --> return the submitted String array
 110  0
                 return submittedValue;
 111  
             }
 112  
 
 113  0
             if (List.class.isAssignableFrom(valueType))
 114  
             {
 115  
                 // expected type is a List
 116  
                 // --> according to javadoc of UISelectMany we assume that the element type
 117  
                 //     is java.lang.String, and copy the String array to a new List
 118  0
                 int len = submittedValue.length;
 119  0
                 List lst = new ArrayList(len);
 120  0
                 for (int i = 0; i < len; i++)
 121  
                 {
 122  0
                     lst.add(submittedValue[i]);
 123  
                 }
 124  0
                 return lst;
 125  
             }
 126  
 
 127  0
             if (arrayComponentType == null)
 128  
             {
 129  0
                 throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array");
 130  
             }
 131  
 
 132  0
             if (Object.class.equals(arrayComponentType)) return submittedValue; //No conversion for Object class
 133  
 
 134  0
             converter = facesContext.getApplication().createConverter(arrayComponentType);
 135  
 
 136  0
             if (converter == null)
 137  
             {
 138  0
                 return submittedValue;
 139  
             }
 140  
         }
 141  
 
 142  
         // Now, we have a converter...
 143  
         // We determine the type of the component array after converting one of it's elements
 144  0
         if (expression != null)
 145  
         {
 146  0
             valueType = expression.getType(facesContext.getELContext());
 147  0
             if (valueType != null && valueType.isArray())
 148  
             {
 149  0
                 if (submittedValue.length > 0) 
 150  
                 {
 151  0
                     arrayComponentType = converter.getAsObject(facesContext, component, submittedValue[0]).getClass();
 152  
                 }
 153  
             }
 154  
         }
 155  
         
 156  0
         if (valueType == null)
 157  
         {
 158  
             // ...but have no idea of expected type
 159  
             // --> so let's convert it to an Object array
 160  0
             int len = submittedValue.length;
 161  0
             Object [] convertedValues = (Object []) Array.newInstance(
 162  
                     arrayComponentType==null?Object.class:arrayComponentType,len);
 163  0
             for (int i = 0; i < len; i++)
 164  
             {
 165  0
                 convertedValues[i]
 166  
                     = converter.getAsObject(facesContext, component, submittedValue[i]);
 167  
             }
 168  0
             return convertedValues;
 169  
         }
 170  
 
 171  0
         if (List.class.isAssignableFrom(valueType))
 172  
         {
 173  
             // Curious case: According to specs we should assume, that the element type
 174  
             // of this List is java.lang.String. But there is a Converter set for this
 175  
             // component. Because the user must know what he is doing, we will convert the values.
 176  0
             int len = submittedValue.length;
 177  0
             List lst = new ArrayList(len);
 178  0
             for (int i = 0; i < len; i++)
 179  
             {
 180  0
                 lst.add(converter.getAsObject(facesContext, component, submittedValue[i]));
 181  
             }
 182  0
             return lst;
 183  
         }
 184  
 
 185  0
         if (arrayComponentType == null)
 186  
         {
 187  0
             throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array");
 188  
         }
 189  
 
 190  0
         if (arrayComponentType.isPrimitive())
 191  
         {
 192  
             //primitive array
 193  0
             int len = submittedValue.length;
 194  0
             Object convertedValues = Array.newInstance(arrayComponentType, len);
 195  0
             for (int i = 0; i < len; i++)
 196  
             {
 197  0
                 Array.set(convertedValues, i,
 198  
                           converter.getAsObject(facesContext, component, submittedValue[i]));
 199  
             }
 200  0
             return convertedValues;
 201  
         }
 202  
 
 203  
         //Object array
 204  0
         int len = submittedValue.length;
 205  0
         ArrayList convertedValues = new ArrayList(len); 
 206  0
         for (int i = 0; i < len; i++)
 207  
         {
 208  0
             convertedValues.add(i, converter.getAsObject(facesContext, component, submittedValue[i])); 
 209  
         }
 210  0
         return convertedValues.toArray((Object[]) Array.newInstance(arrayComponentType, len));
 211  
         
 212  
     }
 213  
 
 214  
 
 215  
 
 216  0
     private static final Log log = LogFactory.getLog(_SharedRendererUtils.class);
 217  
 
 218  
     /**
 219  
      * This method is different in the two versions of _SharedRendererUtils.
 220  
      */
 221  
     private static void log(FacesContext context, String msg, Exception e)
 222  
     {
 223  0
         log.error(msg, e);
 224  0
     }
 225  
 }