Coverage Report - org.apache.commons.ognl.ASTSequence
 
Classes in this File Line Coverage Branch Coverage Complexity
ASTSequence
93%
42/45
84%
32/38
2.583
 
 1  
 package org.apache.commons.ognl;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.commons.ognl.enhance.ExpressionCompiler;
 23  
 import org.apache.commons.ognl.enhance.OrderedReturn;
 24  
 
 25  
 /**
 26  
  * $Id: ASTSequence.java 1194869 2011-10-29 11:10:16Z mcucchiara $
 27  
  * @author Luke Blanshard (blanshlu@netscape.net)
 28  
  * @author Drew Davidson (drew@ognl.org)
 29  
  */
 30  
 public class ASTSequence
 31  
     extends SimpleNode
 32  
     implements NodeType, OrderedReturn
 33  
 {
 34  
     private Class getterClass;
 35  
 
 36  
     private String lastExpression;
 37  
 
 38  
     private String coreExpression;
 39  
 
 40  
     public ASTSequence( int id )
 41  
     {
 42  40
         super( id );
 43  40
     }
 44  
 
 45  
     public ASTSequence( OgnlParser p, int id )
 46  
     {
 47  0
         super( p, id );
 48  0
     }
 49  
 
 50  
     public void jjtClose()
 51  
     {
 52  40
         flattenTree();
 53  40
     }
 54  
 
 55  
     protected Object getValueBody( OgnlContext context, Object source )
 56  
         throws OgnlException
 57  
     {
 58  43
         Object result = null;
 59  134
         for ( int i = 0; i < children.length; ++i )
 60  
         {
 61  91
             result = children[i].getValue( context, source );
 62  
         }
 63  
 
 64  43
         return result; // The result is just the last one we saw.
 65  
     }
 66  
 
 67  
     protected void setValueBody( OgnlContext context, Object target, Object value )
 68  
         throws OgnlException
 69  
     {
 70  2
         int last = children.length - 1;
 71  4
         for ( int i = 0; i < last; ++i )
 72  
         {
 73  2
             children[i].getValue( context, target );
 74  
         }
 75  2
         children[last].setValue( context, target, value );
 76  2
     }
 77  
 
 78  
     public Class getGetterClass()
 79  
     {
 80  3
         return getterClass;
 81  
     }
 82  
 
 83  
     public Class getSetterClass()
 84  
     {
 85  2
         return null;
 86  
     }
 87  
 
 88  
     public String getLastExpression()
 89  
     {
 90  6
         return lastExpression;
 91  
     }
 92  
 
 93  
     public String getCoreExpression()
 94  
     {
 95  3
         return coreExpression;
 96  
     }
 97  
 
 98  
     public String toSetSourceString( OgnlContext context, Object target )
 99  
     {
 100  13
         return "";
 101  
     }
 102  
 
 103  
     public String toGetSourceString( OgnlContext context, Object target )
 104  
     {
 105  13
         String result = "";
 106  
 
 107  13
         NodeType lastType = null;
 108  
 
 109  18
         for ( int i = 0; i < children.length; ++i )
 110  
         {
 111  
             // System.out.println("astsequence child : " + _children[i].getClass().getName());
 112  16
             String seqValue = children[i].toGetSourceString( context, target );
 113  
 
 114  5
             if ( ( i + 1 ) < children.length && ASTOr.class.isInstance( children[i] ) )
 115  
             {
 116  1
                 seqValue = "(" + seqValue + ")";
 117  
             }
 118  
 
 119  5
             if ( i > 0 && ASTProperty.class.isInstance( children[i] ) && seqValue != null
 120  
                 && seqValue.trim().length() > 0 )
 121  
             {
 122  2
                 String pre = (String) context.get( "_currentChain" );
 123  2
                 if ( pre == null )
 124  
                 {
 125  1
                     pre = "";
 126  
                 }
 127  
                 
 128  2
                 seqValue =
 129  
                     ExpressionCompiler.getRootExpression( children[i], context.getRoot(), context ) + pre + seqValue;
 130  2
                 context.setCurrentAccessor( context.getRoot().getClass() );
 131  
             }
 132  
 
 133  5
             if ( ( i + 1 ) >= children.length )
 134  
             {
 135  2
                 coreExpression = result;
 136  2
                 lastExpression = seqValue;
 137  
             }
 138  
 
 139  5
             if ( seqValue != null && seqValue.trim().length() > 0 && ( i + 1 ) < children.length )
 140  
             {
 141  1
                 result += seqValue + ";";
 142  
             }
 143  4
             else if ( seqValue != null && seqValue.trim().length() > 0 )
 144  
             {
 145  2
                 result += seqValue;
 146  
             }
 147  
             // set last known type from last child with a type
 148  
 
 149  5
             if ( NodeType.class.isInstance( children[i] ) && ( (NodeType) children[i] ).getGetterClass() != null )
 150  
             {
 151  2
                 lastType = (NodeType) children[i];
 152  
             }
 153  
         }
 154  
 
 155  2
         if ( lastType != null )
 156  
         {
 157  2
             getterClass = lastType.getGetterClass();
 158  
         }
 159  
 
 160  2
         return result;
 161  
     }
 162  
     
 163  
     public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
 164  
         throws OgnlException
 165  
     {
 166  0
         return visitor.visit( this, data );
 167  
     }
 168  
 }