01: /**
02: * MVEL (The MVFLEX Expression Language)
03: *
04: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: */package org.mvel.ast;
19:
20: import static org.mvel.MVEL.eval;
21: import org.mvel.compiler.ExecutableStatement;
22: import org.mvel.integration.VariableResolverFactory;
23: import static org.mvel.util.ParseTools.subCompileExpression;
24:
25: /**
26: * @author Christopher Brock
27: */
28: public class IfNode extends ASTNode implements NestedStatement {
29: protected char[] block;
30: protected ExecutableStatement condition;
31: protected ExecutableStatement nestedStatement;
32:
33: protected IfNode elseIf;
34: protected ExecutableStatement elseBlock;
35:
36: public IfNode(char[] condition, char[] block, int fields) {
37: // super(condition, fields);
38: this .name = condition;
39: this .block = block;
40:
41: if ((fields & COMPILE_IMMEDIATE) != 0) {
42: this .condition = (ExecutableStatement) subCompileExpression(condition);
43: this .nestedStatement = (ExecutableStatement) subCompileExpression(block);
44: }
45: }
46:
47: public Object getReducedValueAccelerated(Object ctx,
48: Object this Value, VariableResolverFactory factory) {
49: if ((Boolean) condition.getValue(ctx, this Value, factory)) {
50: return nestedStatement.getValue(ctx, this Value, factory);
51: } else if (elseIf != null) {
52: return elseIf.getReducedValueAccelerated(ctx, this Value,
53: factory);
54: } else if (elseBlock != null) {
55: return elseBlock.getValue(ctx, this Value, factory);
56: } else {
57: return Void.class;
58: }
59: }
60:
61: public Object getReducedValue(Object ctx, Object this Value,
62: VariableResolverFactory factory) {
63: if ((Boolean) eval(name, ctx, factory)) {
64: return eval(block, ctx, factory);
65: } else if (elseIf != null) {
66: return elseIf.getReducedValue(ctx, this Value, factory);
67: } else if (elseBlock != null) {
68: return elseBlock.getValue(ctx, this Value, factory);
69: } else {
70: return Void.class;
71: }
72: }
73:
74: public ExecutableStatement getNestedStatement() {
75: return nestedStatement;
76: }
77:
78: public IfNode setElseIf(IfNode elseIf) {
79: return this .elseIf = elseIf;
80: }
81:
82: public ExecutableStatement getElseBlock() {
83: return elseBlock;
84: }
85:
86: public IfNode setElseBlock(char[] block) {
87: elseBlock = (ExecutableStatement) subCompileExpression(block);
88: return this;
89: }
90: }
|