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 ASTAnd |
31 | |
extends BooleanExpression |
32 | |
{ |
33 | |
|
34 | |
private static final long serialVersionUID = -4585941425250141812L; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public ASTAnd( int id ) |
41 | |
{ |
42 | 28 | super( id ); |
43 | 28 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public ASTAnd( OgnlParser p, int id ) |
51 | |
{ |
52 | 0 | super( p, id ); |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public void jjtClose() |
59 | |
{ |
60 | 28 | flattenTree(); |
61 | 28 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
protected Object getValueBody( OgnlContext context, Object source ) |
67 | |
throws OgnlException |
68 | |
{ |
69 | 8 | Object result = null; |
70 | 8 | int last = children.length - 1; |
71 | 20 | for ( int i = 0; i <= last; ++i ) |
72 | |
{ |
73 | 14 | result = children[i].getValue( context, source ); |
74 | |
|
75 | 14 | if ( i != last && !OgnlOps.booleanValue( result ) ) |
76 | |
{ |
77 | 2 | break; |
78 | |
} |
79 | |
} |
80 | |
|
81 | 8 | return result; |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
protected void setValueBody( OgnlContext context, Object target, Object value ) |
88 | |
throws OgnlException |
89 | |
{ |
90 | 0 | int last = children.length - 1; |
91 | |
|
92 | 0 | for ( int i = 0; i < last; ++i ) |
93 | |
{ |
94 | 0 | Object v = children[i].getValue( context, target ); |
95 | |
|
96 | 0 | if ( !OgnlOps.booleanValue( v ) ) |
97 | |
{ |
98 | 0 | return; |
99 | |
} |
100 | |
} |
101 | |
|
102 | 0 | children[last].setValue( context, target, value ); |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public String getExpressionOperator( int index ) |
109 | |
{ |
110 | 0 | return "&&"; |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public Class getGetterClass() |
117 | |
{ |
118 | 3 | return null; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public String toGetSourceString( OgnlContext context, Object target ) |
125 | |
{ |
126 | 14 | if ( children.length != 2 ) |
127 | |
{ |
128 | 0 | throw new UnsupportedCompilationException( |
129 | |
"Can only compile boolean expressions with two children." ); |
130 | |
} |
131 | |
|
132 | 14 | String result = ""; |
133 | |
|
134 | |
try |
135 | |
{ |
136 | |
|
137 | 14 | String first = OgnlRuntime.getChildSource( context, target, children[0] ); |
138 | 12 | if ( !OgnlOps.booleanValue( context.getCurrentObject() ) ) |
139 | |
{ |
140 | 2 | throw new UnsupportedCompilationException( |
141 | |
"And expression can't be compiled until all conditions are true." ); |
142 | |
} |
143 | |
|
144 | 10 | if ( !OgnlRuntime.isBoolean( first ) && !context.getCurrentType().isPrimitive() ) |
145 | |
{ |
146 | 3 | first = OgnlRuntime.getCompiler( context ).createLocalReference( context, first, context.getCurrentType() ); |
147 | |
} |
148 | |
|
149 | 10 | String second = OgnlRuntime.getChildSource( context, target, children[1] ); |
150 | 9 | if ( !OgnlRuntime.isBoolean( second ) && !context.getCurrentType().isPrimitive() ) |
151 | |
{ |
152 | 2 | second = OgnlRuntime.getCompiler( context ).createLocalReference( context, second, context.getCurrentType() ); |
153 | |
} |
154 | |
|
155 | 9 | result += "(org.apache.commons.ognl.OgnlOps.booleanValue(" + first + ")"; |
156 | |
|
157 | 9 | result += " ? "; |
158 | |
|
159 | 9 | result += " ($w) (" + second + ")"; |
160 | 9 | result += " : "; |
161 | |
|
162 | 9 | result += " ($w) (" + first + ")"; |
163 | |
|
164 | 9 | result += ")"; |
165 | |
|
166 | 9 | context.setCurrentObject( target ); |
167 | 9 | context.setCurrentType( Object.class ); |
168 | |
} |
169 | 0 | catch ( NullPointerException e ) |
170 | |
{ |
171 | |
|
172 | 0 | throw new UnsupportedCompilationException( "evaluation resulted in null expression." ); |
173 | |
} |
174 | 5 | catch ( Throwable t ) |
175 | |
{ |
176 | 5 | throw OgnlOps.castToRuntime( t ); |
177 | 9 | } |
178 | |
|
179 | 9 | return result; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public String toSetSourceString( OgnlContext context, Object target ) |
186 | |
{ |
187 | 2 | if ( children.length != 2 ) |
188 | |
{ |
189 | 0 | throw new UnsupportedCompilationException( "Can only compile boolean expressions with two children." ); |
190 | |
} |
191 | |
|
192 | 2 | String pre = (String) context.get( "_currentChain" ); |
193 | 2 | if ( pre == null ) |
194 | |
{ |
195 | 0 | pre = ""; |
196 | |
} |
197 | |
|
198 | 2 | String result = ""; |
199 | |
|
200 | |
try |
201 | |
{ |
202 | |
|
203 | 2 | if ( !OgnlOps.booleanValue( children[0].getValue( context, target ) ) ) |
204 | |
{ |
205 | 1 | throw new UnsupportedCompilationException( |
206 | |
"And expression can't be compiled until all conditions are true." ); |
207 | |
} |
208 | |
|
209 | 1 | String first = |
210 | |
ExpressionCompiler.getRootExpression( children[0], context.getRoot(), context ) + pre |
211 | |
+ children[0].toGetSourceString( context, target ); |
212 | |
|
213 | 1 | children[1].getValue( context, target ); |
214 | |
|
215 | 1 | String second = |
216 | |
ExpressionCompiler.getRootExpression( children[1], context.getRoot(), context ) + pre |
217 | |
+ children[1].toSetSourceString( context, target ); |
218 | |
|
219 | 1 | if ( !OgnlRuntime.isBoolean( first ) ) |
220 | |
{ |
221 | 1 | result += "if(org.apache.commons.ognl.OgnlOps.booleanValue(" + first + ")){"; |
222 | |
} |
223 | |
else |
224 | |
{ |
225 | 0 | result += "if(" + first + "){"; |
226 | |
} |
227 | |
|
228 | 1 | result += second; |
229 | 1 | result += "; } "; |
230 | |
|
231 | 1 | context.setCurrentObject( target ); |
232 | 1 | context.setCurrentType( Object.class ); |
233 | |
|
234 | |
} |
235 | 1 | catch ( Throwable t ) |
236 | |
{ |
237 | 1 | throw OgnlOps.castToRuntime( t ); |
238 | 1 | } |
239 | |
|
240 | 1 | return result; |
241 | |
} |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data ) |
247 | |
throws OgnlException |
248 | |
{ |
249 | 0 | return visitor.visit( this, data ); |
250 | |
} |
251 | |
} |