01: package org.wings.macro;
02:
03: import org.mvel.MVEL;
04:
05: /**
06: * <code>ForMacro<code>.
07: * <p/>
08: * User: rrd
09: * Date: 13.08.2007
10: * Time: 11:00:33
11: *
12: * @author rrd
13: * @version $Id
14: */
15: public class ForMacro extends AbstractMacro {
16:
17: private String initialize;
18: private String condition;
19: private String statement;
20: private String variable;
21:
22: public ForMacro(String instructions) {
23: String[] instr = instructions.split(";");
24:
25: initialize = instr[0];
26: condition = instr[1];
27: statement = instr[2].replaceAll("\\+\\+", "+1").replaceAll(
28: "--", "-1");
29: }
30:
31: private void initialize(MacroContext ctx) {
32: String[] init = initialize.split("=");
33:
34: variable = init[0].trim();
35: Object value = init[1].trim();
36:
37: try {
38: value = Integer.parseInt((String) value);
39: } catch (NumberFormatException e) {
40: // do nothing
41: }
42:
43: // noinspection unchecked
44: ctx.put(variable, value);
45: }
46:
47: private boolean condition(MacroContext ctx) {
48: return MVEL.evalToBoolean(condition, ctx);
49: }
50:
51: private void statement(MacroContext ctx) {
52: Object result = MVEL.eval(statement, ctx);
53:
54: // noinspection unchecked
55: ctx.put(variable, result);
56: }
57:
58: /**
59: * {@inheritDoc}
60: */
61: public void execute(MacroContext ctx) {
62:
63: for (initialize(ctx); condition(ctx); statement(ctx)) {
64: for (Instruction instruction : instructions) {
65: instruction.execute(ctx);
66: }
67: }
68: }
69: }
|