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.ExpressionCompiler; |
23 | |
import org.apache.commons.ognl.enhance.UnsupportedCompilationException; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class ASTOr |
31 | |
extends BooleanExpression |
32 | |
{ |
33 | |
public ASTOr( int id ) |
34 | |
{ |
35 | 26 | super( id ); |
36 | 26 | } |
37 | |
|
38 | |
public ASTOr( OgnlParser p, int id ) |
39 | |
{ |
40 | 0 | super( p, id ); |
41 | 0 | } |
42 | |
|
43 | |
public void jjtClose() |
44 | |
{ |
45 | 26 | flattenTree(); |
46 | 26 | } |
47 | |
|
48 | |
protected Object getValueBody( OgnlContext context, Object source ) |
49 | |
throws OgnlException |
50 | |
{ |
51 | 5 | Object result = null; |
52 | 5 | int last = children.length - 1; |
53 | 7 | for ( int i = 0; i <= last; ++i ) |
54 | |
{ |
55 | 6 | result = children[i].getValue( context, source ); |
56 | 6 | if ( i != last && OgnlOps.booleanValue( result ) ) |
57 | |
{ |
58 | 4 | break; |
59 | |
} |
60 | |
} |
61 | 5 | return result; |
62 | |
} |
63 | |
|
64 | |
protected void setValueBody( OgnlContext context, Object target, Object value ) |
65 | |
throws OgnlException |
66 | |
{ |
67 | 1 | int last = children.length - 1; |
68 | 1 | for ( int i = 0; i < last; ++i ) |
69 | |
{ |
70 | 1 | Object v = children[i].getValue( context, target ); |
71 | 1 | if ( OgnlOps.booleanValue( v ) ) |
72 | |
{ |
73 | 1 | return; |
74 | |
} |
75 | |
} |
76 | 0 | children[last].setValue( context, target, value ); |
77 | 0 | } |
78 | |
|
79 | |
public String getExpressionOperator( int index ) |
80 | |
{ |
81 | 0 | return "||"; |
82 | |
} |
83 | |
|
84 | |
public Class getGetterClass() |
85 | |
{ |
86 | 3 | return null; |
87 | |
} |
88 | |
|
89 | |
public String toGetSourceString( OgnlContext context, Object target ) |
90 | |
{ |
91 | 13 | if ( children.length != 2 ) |
92 | |
{ |
93 | 0 | throw new UnsupportedCompilationException( "Can only compile boolean expressions with two children." ); |
94 | |
} |
95 | |
|
96 | 13 | String result = "("; |
97 | |
|
98 | |
try |
99 | |
{ |
100 | |
|
101 | 13 | String first = OgnlRuntime.getChildSource( context, target, children[0] ); |
102 | 10 | if ( !OgnlRuntime.isBoolean( first ) ) |
103 | |
{ |
104 | 10 | first = OgnlRuntime.getCompiler( context ).createLocalReference( context, first, context.getCurrentType() ); |
105 | |
} |
106 | |
|
107 | 10 | Class firstType = context.getCurrentType(); |
108 | |
|
109 | 10 | String second = OgnlRuntime.getChildSource( context, target, children[1] ); |
110 | 10 | if ( !OgnlRuntime.isBoolean( second ) ) |
111 | |
{ |
112 | 8 | second = OgnlRuntime.getCompiler( context ).createLocalReference( context, second, context.getCurrentType() ); |
113 | |
} |
114 | |
|
115 | 10 | Class secondType = context.getCurrentType(); |
116 | |
|
117 | 10 | boolean mismatched = |
118 | |
( firstType.isPrimitive( ) && !secondType.isPrimitive( ) ) || ( !firstType.isPrimitive( ) |
119 | |
&& secondType.isPrimitive( ) ); |
120 | |
|
121 | 10 | result += "org.apache.commons.ognl.OgnlOps.booleanValue(" + first + ")"; |
122 | |
|
123 | 10 | result += " ? "; |
124 | |
|
125 | 10 | result += ( mismatched ? " ($w) " : "" ) + first; |
126 | |
|
127 | 10 | result += " : "; |
128 | |
|
129 | 10 | result += ( mismatched ? " ($w) " : "" ) + second; |
130 | |
|
131 | 10 | result += ")"; |
132 | |
|
133 | 10 | context.setCurrentObject( target ); |
134 | 10 | context.setCurrentType( Boolean.TYPE ); |
135 | |
|
136 | |
} |
137 | 3 | catch ( Throwable t ) |
138 | |
{ |
139 | 3 | throw OgnlOps.castToRuntime( t ); |
140 | 10 | } |
141 | |
|
142 | 10 | return result; |
143 | |
} |
144 | |
|
145 | |
public String toSetSourceString( OgnlContext context, Object target ) |
146 | |
{ |
147 | 1 | if ( children.length != 2 ) |
148 | |
{ |
149 | 0 | throw new UnsupportedCompilationException( "Can only compile boolean expressions with two children." ); |
150 | |
} |
151 | |
|
152 | 1 | String pre = (String) context.get( "_currentChain" ); |
153 | 1 | if ( pre == null ) |
154 | |
{ |
155 | 0 | pre = ""; |
156 | |
} |
157 | |
|
158 | 1 | String result = ""; |
159 | |
|
160 | |
try |
161 | |
{ |
162 | |
|
163 | 1 | children[0].getValue( context, target ); |
164 | |
|
165 | 1 | String first = |
166 | |
ExpressionCompiler.getRootExpression( children[0], context.getRoot(), context ) + pre |
167 | |
+ children[0].toGetSourceString( context, target ); |
168 | 1 | if ( !OgnlRuntime.isBoolean( first ) ) |
169 | |
{ |
170 | 1 | first = OgnlRuntime.getCompiler( context ).createLocalReference( context, first, Object.class ); |
171 | |
} |
172 | 1 | children[1].getValue( context, target ); |
173 | |
|
174 | 1 | String second = |
175 | |
ExpressionCompiler.getRootExpression( children[1], context.getRoot(), context ) + pre |
176 | |
+ children[1].toSetSourceString( context, target ); |
177 | 1 | if ( !OgnlRuntime.isBoolean( second ) ) |
178 | |
{ |
179 | 1 | second = OgnlRuntime.getCompiler( context ).createLocalReference( context, second, context.getCurrentType() ); |
180 | |
} |
181 | 1 | result += "org.apache.commons.ognl.OgnlOps.booleanValue(" + first + ")"; |
182 | |
|
183 | 1 | result += " ? "; |
184 | |
|
185 | 1 | result += first; |
186 | 1 | result += " : "; |
187 | |
|
188 | 1 | result += second; |
189 | |
|
190 | 1 | context.setCurrentObject( target ); |
191 | |
|
192 | 1 | context.setCurrentType( Boolean.TYPE ); |
193 | |
|
194 | |
} |
195 | 0 | catch ( Throwable t ) |
196 | |
{ |
197 | 0 | throw OgnlOps.castToRuntime( t ); |
198 | 1 | } |
199 | |
|
200 | 1 | return result; |
201 | |
} |
202 | |
|
203 | |
public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data ) |
204 | |
throws OgnlException |
205 | |
{ |
206 | 0 | return visitor.visit( this, data ); |
207 | |
} |
208 | |
} |