01: package org.wings.macro;
02:
03: import org.mvel.MVEL;
04:
05: import java.util.Collection;
06: import java.util.ArrayList;
07:
08: /**
09: * <code>IfMacro<code>.
10: * <p/>
11: * User: rrd
12: * Date: 13.08.2007
13: * Time: 16:56:42
14: *
15: * @author rrd
16: * @version $Id
17: */
18: public class IfMacro extends AbstractMacro {
19:
20: private String condition;
21:
22: private Collection<IfMacro> elseIfMacros = new ArrayList<IfMacro>();
23: private AbstractMacro elseMacro;
24:
25: public IfMacro(String instruction) {
26: condition = instruction;
27: }
28:
29: private boolean condition(MacroContext ctx) {
30: return MVEL.evalToBoolean(condition, ctx);
31: }
32:
33: /**
34: * {@inheritDoc}
35: */
36: public void execute(MacroContext ctx) {
37:
38: if (condition(ctx)) {
39: for (Instruction instruction : instructions) {
40: instruction.execute(ctx);
41: }
42: return;
43: }
44:
45: for (IfMacro macro : elseIfMacros) {
46: if (macro.condition(ctx)) {
47: macro.execute(ctx);
48: return;
49: }
50: }
51:
52: if (elseMacro != null) {
53: elseMacro.execute(ctx);
54: }
55: }
56: }
|