1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml.model;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.scxml.Context;
26 import org.apache.commons.scxml.ErrorReporter;
27 import org.apache.commons.scxml.Evaluator;
28 import org.apache.commons.scxml.EventDispatcher;
29 import org.apache.commons.scxml.SCInstance;
30 import org.apache.commons.scxml.SCXMLExpressionException;
31
32 /***
33 * The class in this SCXML object model that corresponds to the
34 * <if> SCXML element, which serves as a container for conditionally
35 * executed elements. <else> and <elseif> can optionally
36 * appear within an <if> as immediate children, and serve to partition
37 * the elements within an <if>.
38 *
39 */
40 public class If extends Action {
41
42 /***
43 * Serial version UID.
44 */
45 private static final long serialVersionUID = 1L;
46
47 /***
48 * An conditional expression which can be evaluated to true or false.
49 */
50 private String cond;
51
52 /***
53 * The set of executable elements (those that inheriting from
54 * Action) that are contained in this <if> element.
55 */
56 private List actions;
57
58 /***
59 * The boolean value that dictates whether the particular child action
60 * should be executed.
61 */
62 private boolean execute;
63
64 /***
65 * Constructor.
66 */
67 public If() {
68 super();
69 this.actions = new ArrayList();
70 this.execute = false;
71 }
72
73 /***
74 * Get the executable actions contained in this <if>.
75 *
76 * @return Returns the actions.
77 */
78 public final List getActions() {
79 return actions;
80 }
81
82 /***
83 * Add an Action to the list of executable actions contained in
84 * this <if>.
85 *
86 * @param action The action to add.
87 */
88 public final void addAction(final Action action) {
89 if (action != null) {
90 this.actions.add(action);
91 }
92 }
93
94 /***
95 * Get the conditional expression.
96 *
97 * @return Returns the cond.
98 */
99 public final String getCond() {
100 return cond;
101 }
102
103 /***
104 * Set the conditional expression.
105 *
106 * @param cond The cond to set.
107 */
108 public final void setCond(final String cond) {
109 this.cond = cond;
110 }
111
112 /***
113 * {@inheritDoc}
114 */
115 public void execute(final EventDispatcher evtDispatcher,
116 final ErrorReporter errRep, final SCInstance scInstance,
117 final Log appLog, final Collection derivedEvents)
118 throws ModelException, SCXMLExpressionException {
119 State parentState = getParentState();
120 Context ctx = scInstance.getContext(parentState);
121 Evaluator eval = scInstance.getEvaluator();
122 ctx.setLocal(getNamespacesKey(), getNamespaces());
123 execute = eval.evalCond(ctx, cond).booleanValue();
124 ctx.setLocal(getNamespacesKey(), null);
125
126 for (Iterator ifiter = actions.iterator(); ifiter.hasNext();) {
127 Action aa = (Action) ifiter.next();
128 if (execute && !(aa instanceof ElseIf)
129 && !(aa instanceof Else)) {
130 aa.execute(evtDispatcher, errRep, scInstance, appLog,
131 derivedEvents);
132 } else if (execute
133 && (aa instanceof ElseIf || aa instanceof Else)) {
134 break;
135 } else if (aa instanceof Else) {
136 execute = true;
137 } else if (aa instanceof ElseIf) {
138 ctx.setLocal(getNamespacesKey(), getNamespaces());
139 execute = eval.evalCond(ctx, ((ElseIf) aa).getCond())
140 .booleanValue();
141 ctx.setLocal(getNamespacesKey(), null);
142 }
143 }
144 }
145
146 }
147