Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
If |
|
| 2.0;2 |
1 | /* |
|
2 | * |
|
3 | * Copyright 2005 The Apache Software Foundation. |
|
4 | * |
|
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 | * you may not use this file except in compliance with the License. |
|
7 | * You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | * |
|
17 | */ |
|
18 | package org.apache.commons.scxml.model; |
|
19 | ||
20 | import java.util.ArrayList; |
|
21 | import java.util.Collection; |
|
22 | import java.util.Iterator; |
|
23 | import java.util.List; |
|
24 | ||
25 | import org.apache.commons.logging.Log; |
|
26 | import org.apache.commons.scxml.Context; |
|
27 | import org.apache.commons.scxml.ErrorReporter; |
|
28 | import org.apache.commons.scxml.Evaluator; |
|
29 | import org.apache.commons.scxml.EventDispatcher; |
|
30 | import org.apache.commons.scxml.SCInstance; |
|
31 | import org.apache.commons.scxml.SCXMLExpressionException; |
|
32 | ||
33 | /** |
|
34 | * The class in this SCXML object model that corresponds to the |
|
35 | * <if> SCXML element, which serves as a container for conditionally |
|
36 | * executed elements. <else> and <elseif> can optionally |
|
37 | * appear within an <if> as immediate children, and serve to partition |
|
38 | * the elements within an <if>. |
|
39 | * |
|
40 | */ |
|
41 | public class If extends Action { |
|
42 | ||
43 | /** |
|
44 | * An conditional expression which can be evaluated to true or false. |
|
45 | */ |
|
46 | private String cond; |
|
47 | ||
48 | /** |
|
49 | * The set of executable elements (those that inheriting from |
|
50 | * Action) that are contained in this <if> element. |
|
51 | */ |
|
52 | private List actions; |
|
53 | ||
54 | /** |
|
55 | * The boolean value that dictates whether the particular child action |
|
56 | * should be executed. |
|
57 | */ |
|
58 | private boolean execute; |
|
59 | ||
60 | /** |
|
61 | * Constructor. |
|
62 | */ |
|
63 | public If() { |
|
64 | 11 | super(); |
65 | 11 | this.actions = new ArrayList(); |
66 | 11 | this.execute = false; |
67 | 11 | } |
68 | ||
69 | /** |
|
70 | * Get the executable actions contained in this <if>. |
|
71 | * |
|
72 | * @return Returns the actions. |
|
73 | */ |
|
74 | public final List getActions() { |
|
75 | 4 | return actions; |
76 | } |
|
77 | ||
78 | /** |
|
79 | * Add an Action to the list of executable actions contained in |
|
80 | * this <if>. |
|
81 | * |
|
82 | * @param action The action to add. |
|
83 | */ |
|
84 | public final void addAction(final Action action) { |
|
85 | 14 | if (action != null) { |
86 | 14 | this.actions.add(action); |
87 | } |
|
88 | 14 | } |
89 | ||
90 | /** |
|
91 | * Get the conditional expression. |
|
92 | * |
|
93 | * @return Returns the cond. |
|
94 | */ |
|
95 | public final String getCond() { |
|
96 | 4 | return cond; |
97 | } |
|
98 | ||
99 | /** |
|
100 | * Set the conditional expression. |
|
101 | * |
|
102 | * @param cond The cond to set. |
|
103 | */ |
|
104 | public final void setCond(final String cond) { |
|
105 | 11 | this.cond = cond; |
106 | 11 | } |
107 | ||
108 | /** |
|
109 | * {@inheritDoc} |
|
110 | */ |
|
111 | public void execute(final EventDispatcher evtDispatcher, |
|
112 | final ErrorReporter errRep, final SCInstance scInstance, |
|
113 | final Log appLog, final Collection derivedEvents) |
|
114 | throws ModelException, SCXMLExpressionException { |
|
115 | 7 | State parentState = getParentState(); |
116 | 7 | Context ctx = scInstance.getContext(parentState); |
117 | 7 | Evaluator eval = scInstance.getEvaluator(); |
118 | 7 | execute = eval.evalCond(ctx, cond).booleanValue(); |
119 | // The "if" statement is a "container" |
|
120 | 7 | for (Iterator ifiter = actions.iterator(); ifiter.hasNext();) { |
121 | 11 | Action aa = (Action) ifiter.next(); |
122 | 11 | if (execute && !(aa instanceof ElseIf) |
123 | && !(aa instanceof Else)) { |
|
124 | 7 | aa.execute(evtDispatcher, errRep, scInstance, appLog, |
125 | derivedEvents); |
|
126 | 4 | } else if (execute |
127 | && (aa instanceof ElseIf || aa instanceof Else)) { |
|
128 | 0 | break; |
129 | 4 | } else if (aa instanceof Else) { |
130 | 1 | execute = true; |
131 | 3 | } else if (aa instanceof ElseIf) { |
132 | 1 | execute = eval.evalCond(ctx, ((ElseIf) aa).getCond()) |
133 | .booleanValue(); |
|
134 | } |
|
135 | } |
|
136 | 7 | } |
137 | ||
138 | } |
|
139 |