Coverage Report - org.apache.myfaces.el.convert.PropertyResolverToELResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyResolverToELResolver
0%
0/85
0%
0/32
0
 
 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.el.convert;
 20  
 
 21  
 import javax.el.ELContext;
 22  
 import javax.el.ELException;
 23  
 import javax.el.ELResolver;
 24  
 import javax.el.ExpressionFactory;
 25  
 import javax.el.PropertyNotFoundException;
 26  
 import javax.el.PropertyNotWritableException;
 27  
 import javax.faces.FactoryFinder;
 28  
 import javax.faces.application.ApplicationFactory;
 29  
 import javax.faces.context.FacesContext;
 30  
 import javax.faces.el.EvaluationException;
 31  
 import javax.faces.el.PropertyResolver;
 32  
 import java.beans.FeatureDescriptor;
 33  
 import java.util.Iterator;
 34  
 import java.util.List;
 35  
 
 36  
 /**
 37  
  * Wrapper that converts a VariableResolver into an ELResolver. See JSF 1.2 spec section 5.6.1.6
 38  
  *
 39  
  * @author Stan Silvert (latest modification by $Author$)
 40  
  * @author Mathias Broekelmann
 41  
  * @version $Revision$ $Date$
 42  
  */
 43  
 @SuppressWarnings("deprecation")
 44  
 public final class PropertyResolverToELResolver extends ELResolver
 45  
 {
 46  
     private PropertyResolver propertyResolver;
 47  
 
 48  
     private ExpressionFactory expressionFactory;
 49  
 
 50  
     /**
 51  
      * Creates a new instance of PropertyResolverToELResolver
 52  
      */
 53  
     public PropertyResolverToELResolver(final PropertyResolver propertyResolver)
 54  0
     {
 55  0
         this.propertyResolver = propertyResolver;
 56  0
     }
 57  
 
 58  
     @Override
 59  
     public void setValue(final ELContext context, final Object base, final Object property, final Object value)
 60  
         throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException
 61  
     {
 62  0
         if (base == null || property == null)
 63  
         {
 64  0
             return;
 65  
         }
 66  
 
 67  
         try
 68  
         {
 69  0
             context.setPropertyResolved(true);
 70  0
             if (needsCoersion(base))
 71  
             {
 72  0
                 propertyResolver.setValue(base, coerceToInt(property), value);
 73  
             } else
 74  
             {
 75  0
                 propertyResolver.setValue(base, property, value);
 76  
             }
 77  
             // see: https://issues.apache.org/jira/browse/MYFACES-1670
 78  0
             context.setPropertyResolved(
 79  
                 FacesContext.getCurrentInstance().getELContext().isPropertyResolved());
 80  
 
 81  
         }
 82  0
         catch (javax.faces.el.PropertyNotFoundException e)
 83  
         {
 84  0
             context.setPropertyResolved(false);
 85  0
             throw new PropertyNotFoundException(e.getMessage(), e);
 86  
         }
 87  0
         catch (EvaluationException e)
 88  
         {
 89  0
             context.setPropertyResolved(false);
 90  0
             throw new ELException(e.getMessage(), e);
 91  
         }
 92  0
         catch (RuntimeException e)
 93  
         {
 94  0
             context.setPropertyResolved(false);
 95  0
             throw e;
 96  0
         }
 97  0
     }
 98  
 
 99  
     @Override
 100  
     public boolean isReadOnly(final ELContext context, final Object base, final Object property) throws NullPointerException,
 101  
         PropertyNotFoundException, ELException
 102  
     {
 103  0
         if (base == null || property == null)
 104  
         {
 105  0
             return true;
 106  
         }
 107  
 
 108  
         try
 109  
         {
 110  
             boolean result;
 111  0
             context.setPropertyResolved(true);
 112  0
             if (needsCoersion(base))
 113  
             {
 114  0
                 result = propertyResolver.isReadOnly(base, coerceToInt(property));
 115  
             } else
 116  
             {
 117  0
                 result = propertyResolver.isReadOnly(base, property);
 118  
             }
 119  
 
 120  
             // see: https://issues.apache.org/jira/browse/MYFACES-1670
 121  0
             context.setPropertyResolved(
 122  
                 FacesContext.getCurrentInstance().getELContext().isPropertyResolved());
 123  0
             return result;
 124  
         }
 125  0
         catch (javax.faces.el.PropertyNotFoundException e)
 126  
         {
 127  0
             context.setPropertyResolved(false);
 128  0
             throw new PropertyNotFoundException(e.getMessage(), e);
 129  
         }
 130  0
         catch (EvaluationException e)
 131  
         {
 132  0
             context.setPropertyResolved(false);
 133  0
             throw new ELException(e.getMessage(), e);
 134  
         }
 135  0
         catch (RuntimeException e)
 136  
         {
 137  0
             context.setPropertyResolved(false);
 138  0
             throw e;
 139  
         }
 140  
     }
 141  
 
 142  
     @Override
 143  
     public Object getValue(final ELContext context, final Object base, final Object property) throws NullPointerException,
 144  
         PropertyNotFoundException, ELException
 145  
     {
 146  0
         if (base == null || property == null)
 147  
         {
 148  0
             return null;
 149  
         }
 150  
 
 151  
         try
 152  
         {
 153  0
             context.setPropertyResolved(true);
 154  
             Object value;
 155  0
             if (needsCoersion(base))
 156  
             {
 157  0
                 value = propertyResolver.getValue(base, coerceToInt(property));
 158  
             } else
 159  
             {
 160  0
                 value = propertyResolver.getValue(base, property);
 161  
             }
 162  
 
 163  
             // see: https://issues.apache.org/jira/browse/MYFACES-1670
 164  0
             context.setPropertyResolved(
 165  
                 FacesContext.getCurrentInstance().getELContext().isPropertyResolved());
 166  
 
 167  0
             return value;
 168  
         }
 169  0
         catch (javax.faces.el.PropertyNotFoundException e)
 170  
         {
 171  0
             context.setPropertyResolved(false);
 172  0
             throw new PropertyNotFoundException(e.getMessage(), e);
 173  
         }
 174  0
         catch (EvaluationException e)
 175  
         {
 176  0
             context.setPropertyResolved(false);
 177  0
             throw new ELException(e.getMessage(), e);
 178  
         }
 179  0
         catch (RuntimeException e)
 180  
         {
 181  0
             context.setPropertyResolved(false);
 182  0
             throw e;
 183  
         }
 184  
     }
 185  
 
 186  
     @Override
 187  
     public Class<?> getType(final ELContext context, final Object base, final Object property) throws NullPointerException,
 188  
         PropertyNotFoundException, ELException
 189  
     {
 190  0
         if (base == null || property == null)
 191  
         {
 192  0
             return null;
 193  
         }
 194  
 
 195  
         try
 196  
         {
 197  0
             context.setPropertyResolved(true);
 198  
             Class<?> value;
 199  0
             if (needsCoersion(base))
 200  
             {
 201  0
                 value = propertyResolver.getType(base, coerceToInt(property));
 202  
             } else
 203  
             {
 204  0
                 value = propertyResolver.getType(base, property);
 205  
             }
 206  
 
 207  
             // see: https://issues.apache.org/jira/browse/MYFACES-1670
 208  0
             context.setPropertyResolved(
 209  
                 FacesContext.getCurrentInstance().getELContext().isPropertyResolved());
 210  
 
 211  0
             return value;
 212  
         }
 213  0
         catch (javax.faces.el.PropertyNotFoundException e)
 214  
         {
 215  0
             context.setPropertyResolved(false);
 216  0
             throw new PropertyNotFoundException(e.getMessage(), e);
 217  
         }
 218  0
         catch (EvaluationException e)
 219  
         {
 220  0
             context.setPropertyResolved(false);
 221  0
             throw new ELException(e.getMessage(), e);
 222  
         }
 223  0
         catch (RuntimeException e)
 224  
         {
 225  0
             context.setPropertyResolved(false);
 226  0
             throw e;
 227  
         }
 228  
     }
 229  
 
 230  
     @Override
 231  
     public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base)
 232  
     {
 233  0
         return null;
 234  
     }
 235  
 
 236  
     @Override
 237  
     public Class<?> getCommonPropertyType(ELContext context, Object base)
 238  
     {
 239  
 
 240  0
         if (base == null)
 241  
         {
 242  0
             return null;
 243  
         }
 244  
 
 245  0
         return Object.class;
 246  
     }
 247  
 
 248  
     private static boolean needsCoersion(Object base)
 249  
     {
 250  0
         return (base instanceof List) || base.getClass().isArray();
 251  
     }
 252  
 
 253  
     protected ExpressionFactory getExpressionFactory()
 254  
     {
 255  0
         if (expressionFactory == null)
 256  
         {
 257  0
             ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder
 258  
                 .getFactory(FactoryFinder.APPLICATION_FACTORY);
 259  0
             expressionFactory = appFactory.getApplication().getExpressionFactory();
 260  
         }
 261  0
         return expressionFactory;
 262  
     }
 263  
 
 264  
     public void setExpressionFactory(ExpressionFactory expressionFactory)
 265  
     {
 266  0
         this.expressionFactory = expressionFactory;
 267  0
     }
 268  
 
 269  
     private int coerceToInt(Object property)
 270  
     {
 271  0
         return (Integer) getExpressionFactory().coerceToType(property, Integer.class);
 272  
     }
 273  
 
 274  
 
 275  
     public PropertyResolver getPropertyResolver()
 276  
     {
 277  0
         return propertyResolver;
 278  
     }
 279  
 }