Clover coverage report - Code Coverage for tapestry release 4.0-beta-8
Coverage timestamp: Sat Sep 24 2005 11:50:34 EDT
file stats: LOC: 156   Methods: 10
NCLOC: 102   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExpressionEvaluatorImpl.java 100% 100% 100% 100%
coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.services.impl;
 16   
 17    import java.util.Iterator;
 18    import java.util.List;
 19    import java.util.Map;
 20   
 21    import ognl.ClassResolver;
 22    import ognl.Ognl;
 23    import ognl.OgnlRuntime;
 24    import ognl.TypeConverter;
 25   
 26    import org.apache.hivemind.ApplicationRuntimeException;
 27    import org.apache.tapestry.Tapestry;
 28    import org.apache.tapestry.services.ExpressionCache;
 29    import org.apache.tapestry.services.ExpressionEvaluator;
 30    import org.apache.tapestry.spec.IApplicationSpecification;
 31   
 32    /**
 33    * @author Howard M. Lewis Ship
 34    * @since 4.0
 35    */
 36    public class ExpressionEvaluatorImpl implements ExpressionEvaluator
 37    {
 38    // Uses Thread's context class loader
 39   
 40    private final ClassResolver _ognlResolver = new OgnlClassResolver();
 41   
 42    private ExpressionCache _expressionCache;
 43   
 44    private IApplicationSpecification _applicationSpecification;
 45   
 46    private TypeConverter _typeConverter;
 47   
 48    private List _contributions;
 49   
 50    // Context, with a root of null, used when evaluating an expression
 51    // to see if it is a constant.
 52   
 53    private Map _defaultContext;
 54   
 55  42 public void setApplicationSpecification(IApplicationSpecification applicationSpecification)
 56    {
 57  42 _applicationSpecification = applicationSpecification;
 58    }
 59   
 60  42 public void initializeService()
 61    {
 62  42 if (_applicationSpecification.checkExtension(Tapestry.OGNL_TYPE_CONVERTER))
 63  1 _typeConverter = (TypeConverter) _applicationSpecification.getExtension(
 64    Tapestry.OGNL_TYPE_CONVERTER,
 65    TypeConverter.class);
 66   
 67  42 Iterator i = _contributions.iterator();
 68   
 69  42 while (i.hasNext())
 70    {
 71  80 PropertyAccessorContribution c = (PropertyAccessorContribution) i.next();
 72   
 73  80 OgnlRuntime.setPropertyAccessor(c.getSubjectClass(), c.getAccessor());
 74    }
 75   
 76  42 _defaultContext = Ognl.createDefaultContext(null, _ognlResolver, _typeConverter);
 77   
 78    }
 79   
 80  58 public Object read(Object target, String expression)
 81    {
 82  58 return readCompiled(target, _expressionCache.getCompiledExpression(expression));
 83    }
 84   
 85  2406 public Object readCompiled(Object target, Object expression)
 86    {
 87  2406 try
 88    {
 89  2406 Map context = createContext(target);
 90   
 91  2406 return Ognl.getValue(expression, context, target);
 92    }
 93    catch (Exception ex)
 94    {
 95  2 throw new ApplicationRuntimeException(ImplMessages.unableToReadExpression(ImplMessages
 96    .parsedExpression(), target, ex), target, null, ex);
 97    }
 98    }
 99   
 100  3825 private Map createContext(Object target)
 101    {
 102  3825 Map result = Ognl.createDefaultContext(target, _ognlResolver);
 103   
 104  3825 if (_typeConverter != null)
 105  1 Ognl.setTypeConverter(result, _typeConverter);
 106   
 107  3825 return result;
 108    }
 109   
 110  3 public void write(Object target, String expression, Object value)
 111    {
 112  3 writeCompiled(target, _expressionCache.getCompiledExpression(expression), value);
 113    }
 114   
 115  1419 public void writeCompiled(Object target, Object expression, Object value)
 116    {
 117  1419 try
 118    {
 119  1419 Map context = createContext(target);
 120   
 121  1419 Ognl.setValue(expression, context, target, value);
 122    }
 123    catch (Exception ex)
 124    {
 125  1 throw new ApplicationRuntimeException(ImplMessages.unableToWriteExpression(ImplMessages
 126    .parsedExpression(), target, value, ex), target, null, ex);
 127    }
 128   
 129    }
 130   
 131  938 public boolean isConstant(String expression)
 132    {
 133  938 Object compiled = _expressionCache.getCompiledExpression(expression);
 134   
 135  938 try
 136    {
 137  938 return Ognl.isConstant(compiled, _defaultContext);
 138    }
 139    catch (Exception ex)
 140    {
 141  1 throw new ApplicationRuntimeException(ImplMessages.isConstantExpressionError(
 142    expression,
 143    ex), ex);
 144    }
 145    }
 146   
 147  64 public void setExpressionCache(ExpressionCache expressionCache)
 148    {
 149  64 _expressionCache = expressionCache;
 150    }
 151   
 152  42 public void setContributions(List contributions)
 153    {
 154  42 _contributions = contributions;
 155    }
 156    }