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