| 1 | |
package org.apache.commons.ognl; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
import org.apache.commons.ognl.enhance.UnsupportedCompilationException; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
class ASTTest |
| 30 | |
extends ExpressionNode |
| 31 | |
{ |
| 32 | |
public ASTTest( int id ) |
| 33 | |
{ |
| 34 | 60 | super( id ); |
| 35 | 60 | } |
| 36 | |
|
| 37 | |
public ASTTest( OgnlParser p, int id ) |
| 38 | |
{ |
| 39 | 0 | super( p, id ); |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
protected Object getValueBody( OgnlContext context, Object source ) |
| 43 | |
throws OgnlException |
| 44 | |
{ |
| 45 | 114 | Object test = children[0].getValue( context, source ); |
| 46 | 114 | int branch = OgnlOps.booleanValue( test ) ? 1 : 2; |
| 47 | 114 | return children[branch].getValue( context, source ); |
| 48 | |
} |
| 49 | |
|
| 50 | |
protected void setValueBody( OgnlContext context, Object target, Object value ) |
| 51 | |
throws OgnlException |
| 52 | |
{ |
| 53 | 0 | Object test = children[0].getValue( context, target ); |
| 54 | 0 | int branch = OgnlOps.booleanValue( test ) ? 1 : 2; |
| 55 | 0 | children[branch].setValue( context, target, value ); |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
public String getExpressionOperator( int index ) |
| 59 | |
{ |
| 60 | 0 | return ( index == 1 ) ? "?" : ":"; |
| 61 | |
} |
| 62 | |
|
| 63 | |
public String toGetSourceString( OgnlContext context, Object target ) |
| 64 | |
{ |
| 65 | 31 | if ( target == null ) |
| 66 | |
{ |
| 67 | 4 | throw new UnsupportedCompilationException( "evaluation resulted in null expression." ); |
| 68 | |
} |
| 69 | |
|
| 70 | 27 | if ( children.length != 3 ) |
| 71 | |
{ |
| 72 | 0 | throw new UnsupportedCompilationException( "Can only compile test expressions with two children." |
| 73 | |
+ children.length ); |
| 74 | |
} |
| 75 | |
|
| 76 | 27 | String result = ""; |
| 77 | |
|
| 78 | |
try |
| 79 | |
{ |
| 80 | |
|
| 81 | 27 | String first = OgnlRuntime.getChildSource( context, target, children[0] ); |
| 82 | 26 | if ( !OgnlRuntime.isBoolean( first ) && !context.getCurrentType().isPrimitive() ) |
| 83 | |
{ |
| 84 | 7 | first = OgnlRuntime.getCompiler( context ).createLocalReference( context, first, context.getCurrentType() ); |
| 85 | |
} |
| 86 | |
|
| 87 | 26 | if ( ExpressionNode.class.isInstance( children[0] ) ) |
| 88 | |
{ |
| 89 | 5 | first = "(" + first + ")"; |
| 90 | |
} |
| 91 | |
|
| 92 | 26 | String second = OgnlRuntime.getChildSource( context, target, children[1] ); |
| 93 | 26 | Class secondType = context.getCurrentType(); |
| 94 | |
|
| 95 | 26 | if ( !OgnlRuntime.isBoolean( second ) && !context.getCurrentType().isPrimitive() ) |
| 96 | |
{ |
| 97 | 22 | second = OgnlRuntime.getCompiler( context ).createLocalReference( context, second, context.getCurrentType() ); |
| 98 | |
} |
| 99 | |
|
| 100 | 25 | if ( ExpressionNode.class.isInstance( children[1] ) ) |
| 101 | |
{ |
| 102 | 1 | second = "(" + second + ")"; |
| 103 | |
} |
| 104 | |
|
| 105 | 25 | String third = OgnlRuntime.getChildSource( context, target, children[2] ); |
| 106 | 24 | Class thirdType = context.getCurrentType(); |
| 107 | |
|
| 108 | 24 | if ( !OgnlRuntime.isBoolean( third ) && !context.getCurrentType().isPrimitive() ) |
| 109 | |
{ |
| 110 | 20 | third = OgnlRuntime.getCompiler( context ).createLocalReference( context, third, context.getCurrentType() ); |
| 111 | |
|
| 112 | |
} |
| 113 | |
|
| 114 | 22 | if ( ExpressionNode.class.isInstance( children[2] ) ) |
| 115 | |
{ |
| 116 | 0 | third = "(" + third + ")"; |
| 117 | |
} |
| 118 | |
|
| 119 | 22 | boolean mismatched = |
| 120 | |
( secondType.isPrimitive( ) && !thirdType.isPrimitive( ) ) || ( !secondType.isPrimitive( ) |
| 121 | |
&& thirdType.isPrimitive( ) ); |
| 122 | |
|
| 123 | 22 | result += "org.apache.commons.ognl.OgnlOps.booleanValue(" + first + ")"; |
| 124 | |
|
| 125 | 22 | result += " ? "; |
| 126 | |
|
| 127 | 22 | result += ( mismatched ? " ($w) " : "" ) + second; |
| 128 | 22 | result += " : "; |
| 129 | |
|
| 130 | 22 | result += ( mismatched ? " ($w) " : "" ) + third; |
| 131 | |
|
| 132 | 22 | context.setCurrentObject( target ); |
| 133 | 22 | context.setCurrentType( mismatched ? Object.class : secondType ); |
| 134 | |
|
| 135 | 22 | return result; |
| 136 | |
|
| 137 | |
} |
| 138 | 3 | catch ( NullPointerException e ) |
| 139 | |
{ |
| 140 | |
|
| 141 | |
|
| 142 | 3 | throw new UnsupportedCompilationException( "evaluation resulted in null expression." ); |
| 143 | |
} |
| 144 | 2 | catch ( Throwable t ) |
| 145 | |
{ |
| 146 | 2 | throw OgnlOps.castToRuntime( t ); |
| 147 | |
} |
| 148 | |
} |
| 149 | |
|
| 150 | |
public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data ) |
| 151 | |
throws OgnlException |
| 152 | |
{ |
| 153 | 0 | return visitor.visit( this, data ); |
| 154 | |
} |
| 155 | |
} |