Coverage Report - org.apache.commons.ognl.ExpressionNode
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionNode
79%
34/43
64%
36/56
5.143
 
 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  
 
 24  
 /**
 25  
  * @author Luke Blanshard (blanshlu@netscape.net)
 26  
  * @author Drew Davidson (drew@ognl.org)
 27  
  */
 28  
 public abstract class ExpressionNode
 29  
     extends SimpleNode
 30  
 {
 31  
 
 32  
     private static final long serialVersionUID = 9076228016268317598L;
 33  
 
 34  
     public ExpressionNode( int i )
 35  
     {
 36  651
         super( i );
 37  651
     }
 38  
 
 39  
     public ExpressionNode( OgnlParser p, int i )
 40  
     {
 41  0
         super( p, i );
 42  0
     }
 43  
 
 44  
     /**
 45  
      * Returns true iff this node is constant without respect to the children.
 46  
      */
 47  
     @Override
 48  
     public boolean isNodeConstant( OgnlContext context )
 49  
         throws OgnlException
 50  
     {
 51  197
         return false;
 52  
     }
 53  
 
 54  
     @Override
 55  
     public boolean isConstant( OgnlContext context )
 56  
         throws OgnlException
 57  
     {
 58  197
         boolean result = isNodeConstant( context );
 59  
 
 60  197
         if ( ( children != null ) && ( children.length > 0 ) )
 61  
         {
 62  197
             result = true;
 63  480
             for ( int i = 0; result && ( i < children.length ); ++i )
 64  
             {
 65  283
                 if ( children[i] instanceof SimpleNode )
 66  
                 {
 67  283
                     result = ( (SimpleNode) children[i] ).isConstant( context );
 68  
                 }
 69  
                 else
 70  
                 {
 71  0
                     result = false;
 72  
                 }
 73  
             }
 74  
         }
 75  197
         return result;
 76  
     }
 77  
 
 78  
     public String getExpressionOperator( int index )
 79  
     {
 80  0
         throw new RuntimeException( "unknown operator for " + OgnlParserTreeConstants.jjtNodeName[id] );
 81  
     }
 82  
 
 83  
     @Override
 84  
     public String toGetSourceString( OgnlContext context, Object target )
 85  
     {
 86  18
         StringBuilder result =
 87  
             new StringBuilder(
 88  
                 ( parent == null || NumericExpression.class.isAssignableFrom( parent.getClass() ) ) ? "" : "(" );
 89  
 
 90  18
         if ( ( children != null ) && ( children.length > 0 ) )
 91  
         {
 92  36
             for ( int i = 0; i < children.length; ++i )
 93  
             {
 94  18
                 if ( i > 0 )
 95  
                 {
 96  0
                     result.append( " " ).append( getExpressionOperator( i ) ).append( " " );
 97  
                 }
 98  
 
 99  18
                 String value = children[i].toGetSourceString( context, target );
 100  
 
 101  18
                 if ( ( ASTProperty.class.isInstance( children[i] ) || ASTMethod.class.isInstance( children[i] )
 102  
                     || ASTSequence.class.isInstance( children[i] ) || ASTChain.class.isInstance( children[i] ) )
 103  
                     && value != null && value.trim().length() > 0 )
 104  
                 {
 105  
 
 106  6
                     String pre = null;
 107  6
                     if ( ASTMethod.class.isInstance( children[i] ) )
 108  
                     {
 109  1
                         pre = (String) context.get( "_currentChain" );
 110  
                     }
 111  
 
 112  6
                     if ( pre == null )
 113  
                     {
 114  6
                         pre = "";
 115  
                     }
 116  
 
 117  6
                     String cast = (String) context.remove( ExpressionCompiler.PRE_CAST );
 118  6
                     if ( cast == null )
 119  
                     {
 120  6
                         cast = "";
 121  
                     }
 122  
 
 123  6
                     value =
 124  
                         cast + ExpressionCompiler.getRootExpression( children[i], context.getRoot(), context ) + pre
 125  
                             + value;
 126  
                 }
 127  
 
 128  18
                 result.append( value );
 129  
             }
 130  
         }
 131  
 
 132  18
         if ( parent != null && !NumericExpression.class.isAssignableFrom( parent.getClass() ) )
 133  
         {
 134  8
             result.append( ")" );
 135  
         }
 136  
 
 137  18
         return result.toString();
 138  
     }
 139  
 
 140  
     @Override
 141  
     public String toSetSourceString( OgnlContext context, Object target )
 142  
     {
 143  2
         StringBuilder sourceStringBuilder = new StringBuilder( parent == null ? "" : "(" );
 144  
 
 145  2
         if ( ( children != null ) && ( children.length > 0 ) )
 146  
         {
 147  2
             for ( int i = 0; i < children.length; ++i )
 148  
             {
 149  2
                 if ( i > 0 )
 150  
                 {
 151  0
                     sourceStringBuilder.append( " " ).append( getExpressionOperator( i ) ).append( ' ' );
 152  
                 }
 153  
 
 154  2
                 sourceStringBuilder.append( children[i].toSetSourceString( context, target ) );
 155  
             }
 156  
         }
 157  0
         if ( parent != null )
 158  
         {
 159  0
             sourceStringBuilder.append( ")" );
 160  
         }
 161  
 
 162  0
         return sourceStringBuilder.toString();
 163  
     }
 164  
 }