1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.commons.scxml.io; |
19 |
|
|
20 |
|
import java.text.MessageFormat; |
21 |
|
import java.util.Iterator; |
22 |
|
import java.util.List; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import org.apache.commons.logging.LogFactory; |
26 |
|
import org.apache.commons.scxml.SCXMLHelper; |
27 |
|
import org.apache.commons.scxml.model.History; |
28 |
|
import org.apache.commons.scxml.model.Initial; |
29 |
|
import org.apache.commons.scxml.model.Invoke; |
30 |
|
import org.apache.commons.scxml.model.ModelException; |
31 |
|
import org.apache.commons.scxml.model.Parallel; |
32 |
|
import org.apache.commons.scxml.model.SCXML; |
33 |
|
import org.apache.commons.scxml.model.State; |
34 |
|
import org.apache.commons.scxml.model.Transition; |
35 |
|
import org.apache.commons.scxml.model.TransitionTarget; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
final class ModelUpdater { |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
static void updateSCXML(final SCXML scxml) throws ModelException { |
58 |
|
|
59 |
39 |
String initialstate = scxml.getInitialstate(); |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
39 |
State initialState = (State) scxml.getTargets().get(initialstate); |
66 |
39 |
if (initialState == null) { |
67 |
|
|
68 |
0 |
logAndThrowModelError(ERR_SCXML_NO_INIT, new Object[] { |
69 |
|
initialstate }); |
70 |
|
} |
71 |
39 |
scxml.setInitialState(initialState); |
72 |
39 |
Map targets = scxml.getTargets(); |
73 |
39 |
Map states = scxml.getStates(); |
74 |
39 |
Iterator i = states.keySet().iterator(); |
75 |
128 |
while (i.hasNext()) { |
76 |
89 |
updateState((State) states.get(i.next()), targets); |
77 |
|
} |
78 |
39 |
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
private static void updateState(final State s, final Map targets) |
89 |
|
throws ModelException { |
90 |
|
|
91 |
|
|
92 |
194 |
s.getOnEntry().setParent(s); |
93 |
194 |
s.getOnExit().setParent(s); |
94 |
|
|
95 |
194 |
Initial ini = s.getInitial(); |
96 |
194 |
Map c = s.getChildren(); |
97 |
194 |
TransitionTarget initialState = null; |
98 |
194 |
if (!c.isEmpty()) { |
99 |
40 |
if (ini == null) { |
100 |
0 |
logAndThrowModelError(ERR_STATE_NO_INIT, |
101 |
|
new Object[] {getStateName(s)}); |
102 |
|
} |
103 |
40 |
Transition initialTransition = ini.getTransition(); |
104 |
40 |
updateTransition(initialTransition, targets); |
105 |
40 |
initialState = initialTransition.getTarget(); |
106 |
|
|
107 |
|
|
108 |
40 |
if (initialState == null |
109 |
|
|| !SCXMLHelper.isDescendant(initialState, s)) { |
110 |
0 |
logAndThrowModelError(ERR_STATE_BAD_INIT, |
111 |
|
new Object[] {getStateName(s)}); |
112 |
|
} |
113 |
|
} |
114 |
194 |
List histories = s.getHistory(); |
115 |
194 |
Iterator histIter = histories.iterator(); |
116 |
199 |
while (histIter.hasNext()) { |
117 |
5 |
if (s.isSimple()) { |
118 |
0 |
logAndThrowModelError(ERR_HISTORY_SIMPLE_STATE, |
119 |
|
new Object[] {getStateName(s)}); |
120 |
|
} |
121 |
5 |
History h = (History) histIter.next(); |
122 |
5 |
Transition historyTransition = h.getTransition(); |
123 |
5 |
if (historyTransition == null) { |
124 |
|
|
125 |
1 |
if (initialState != null |
126 |
|
&& !(initialState instanceof History)) { |
127 |
1 |
historyTransition = new Transition(); |
128 |
1 |
historyTransition.setNext(initialState.getId()); |
129 |
1 |
historyTransition.setParent(h); |
130 |
1 |
h.setTransition(historyTransition); |
131 |
|
} else { |
132 |
0 |
logAndThrowModelError(ERR_HISTORY_NO_DEFAULT, |
133 |
|
new Object[] {h.getId(), getStateName(s)}); |
134 |
|
} |
135 |
|
} |
136 |
5 |
updateTransition(historyTransition, targets); |
137 |
5 |
State historyState = (State) historyTransition.getTarget(); |
138 |
5 |
if (historyState == null) { |
139 |
0 |
logAndThrowModelError(ERR_STATE_NO_HIST, |
140 |
|
new Object[] {getStateName(s)}); |
141 |
|
} |
142 |
5 |
if (!h.isDeep()) { |
143 |
3 |
if (!c.containsValue(historyState)) { |
144 |
0 |
logAndThrowModelError(ERR_STATE_BAD_SHALLOW_HIST, |
145 |
|
new Object[] {getStateName(s)}); |
146 |
|
} |
147 |
|
} else { |
148 |
2 |
if (!SCXMLHelper.isDescendant(historyState, s)) { |
149 |
0 |
logAndThrowModelError(ERR_STATE_BAD_DEEP_HIST, |
150 |
|
new Object[] {getStateName(s)}); |
151 |
|
} |
152 |
|
} |
153 |
|
} |
154 |
194 |
Map t = s.getTransitions(); |
155 |
194 |
Iterator i = t.keySet().iterator(); |
156 |
347 |
while (i.hasNext()) { |
157 |
153 |
Iterator j = ((List) t.get(i.next())).iterator(); |
158 |
311 |
while (j.hasNext()) { |
159 |
158 |
Transition trn = (Transition) j.next(); |
160 |
|
|
161 |
158 |
trn.setParent(s); |
162 |
158 |
updateTransition(trn, targets); |
163 |
|
} |
164 |
|
} |
165 |
194 |
Parallel p = s.getParallel(); |
166 |
194 |
Invoke inv = s.getInvoke(); |
167 |
194 |
if ((inv != null && p != null) |
168 |
|
|| (inv != null && !c.isEmpty()) |
169 |
|
|| (p != null && !c.isEmpty())) { |
170 |
0 |
logAndThrowModelError(ERR_STATE_BAD_CONTENTS, |
171 |
|
new Object[] {getStateName(s)}); |
172 |
|
} |
173 |
194 |
if (p != null) { |
174 |
5 |
updateParallel(p, targets); |
175 |
189 |
} else if (inv != null) { |
176 |
1 |
String ttype = inv.getTargettype(); |
177 |
1 |
if (ttype == null || ttype.trim().length() == 0) { |
178 |
0 |
logAndThrowModelError(ERR_INVOKE_NO_TARGETTYPE, |
179 |
|
new Object[] {getStateName(s)}); |
180 |
|
} |
181 |
1 |
String src = inv.getSrc(); |
182 |
1 |
boolean noSrc = (src == null || src.trim().length() == 0); |
183 |
1 |
String srcexpr = inv.getSrcexpr(); |
184 |
1 |
boolean noSrcexpr = (srcexpr == null |
185 |
|
|| srcexpr.trim().length() == 0); |
186 |
1 |
if (noSrc && noSrcexpr) { |
187 |
0 |
logAndThrowModelError(ERR_INVOKE_NO_SRC, |
188 |
|
new Object[] {getStateName(s)}); |
189 |
|
} |
190 |
1 |
if (!noSrc && !noSrcexpr) { |
191 |
0 |
logAndThrowModelError(ERR_INVOKE_AMBIGUOUS_SRC, |
192 |
|
new Object[] {getStateName(s)}); |
193 |
|
} |
194 |
|
} else { |
195 |
188 |
Iterator j = c.keySet().iterator(); |
196 |
281 |
while (j.hasNext()) { |
197 |
93 |
updateState((State) c.get(j.next()), targets); |
198 |
|
} |
199 |
|
} |
200 |
194 |
} |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
private static void updateParallel(final Parallel p, final Map targets) |
210 |
|
throws ModelException { |
211 |
5 |
Iterator i = p.getStates().iterator(); |
212 |
17 |
while (i.hasNext()) { |
213 |
12 |
updateState((State) i.next(), targets); |
214 |
|
} |
215 |
5 |
} |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
private static void updateTransition(final Transition t, |
225 |
|
final Map targets) throws ModelException { |
226 |
203 |
String next = t.getNext(); |
227 |
203 |
TransitionTarget tt = t.getTarget(); |
228 |
203 |
if (tt == null) { |
229 |
203 |
tt = (TransitionTarget) targets.get(next); |
230 |
203 |
if (tt == null) { |
231 |
0 |
logAndThrowModelError(ERR_TARGET_NOT_FOUND, new Object[] { |
232 |
|
next }); |
233 |
|
} |
234 |
203 |
t.setTarget(tt); |
235 |
|
} |
236 |
203 |
} |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
private static void logAndThrowModelError(final String errType, |
246 |
|
final Object[] msgArgs) throws ModelException { |
247 |
0 |
MessageFormat msgFormat = new MessageFormat(errType); |
248 |
0 |
String errMsg = msgFormat.format(msgArgs); |
249 |
0 |
org.apache.commons.logging.Log log = LogFactory. |
250 |
0 |
getLog(ModelUpdater.class); |
251 |
0 |
log.error(errMsg); |
252 |
0 |
throw new ModelException(errMsg); |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
private static String getStateName(final State state) { |
264 |
0 |
String badState = "anonymous state"; |
265 |
0 |
if (!SCXMLHelper.isStringEmpty(state.getId())) { |
266 |
0 |
badState = "state with ID \"" + state.getId() + "\""; |
267 |
|
} |
268 |
0 |
return badState; |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
private ModelUpdater() { |
275 |
0 |
super(); |
276 |
0 |
} |
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
private static final String ERR_SCXML_NO_INIT = "No SCXML child state " |
283 |
|
+ "with ID \"{0}\" found; illegal initialstate for SCXML document"; |
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
private static final String ERR_STATE_NO_INIT = "No initial element " |
290 |
|
+ "available for {0}"; |
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
private static final String ERR_STATE_BAD_INIT = "Initial state " |
297 |
|
+ "null or not a descendant of {0}"; |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
private static final String ERR_STATE_BAD_CONTENTS = "{0} should " |
305 |
|
+ "contain either one <parallel>, one <invoke> or any number of " |
306 |
|
+ "<state> children."; |
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
private static final String ERR_STATE_NO_HIST = "Referenced history state" |
312 |
|
+ " null for {0}"; |
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
private static final String ERR_STATE_BAD_SHALLOW_HIST = "History state" |
318 |
|
+ " for shallow history is not child for {0}"; |
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
|
323 |
|
private static final String ERR_STATE_BAD_DEEP_HIST = "History state" |
324 |
|
+ " for deep history is not descendant for {0}"; |
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
private static final String ERR_TARGET_NOT_FOUND = |
330 |
|
"Transition target with ID \"{0}\" not found"; |
331 |
|
|
332 |
|
|
333 |
|
|
334 |
|
|
335 |
|
private static final String ERR_HISTORY_SIMPLE_STATE = |
336 |
|
"Simple {0} contains history elements"; |
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
|
private static final String ERR_HISTORY_NO_DEFAULT = |
342 |
|
"No default target specified for history with ID \"{0}\"" |
343 |
|
+ " belonging to {1}"; |
344 |
|
|
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
private static final String ERR_INVOKE_NO_TARGETTYPE = "{0} contains " |
350 |
|
+ "<invoke> with no \"targettype\" attribute specified."; |
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
private static final String ERR_INVOKE_NO_SRC = "{0} contains " |
357 |
|
+ "<invoke> without a \"src\" or \"srcexpr\" attribute specified."; |
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
private static final String ERR_INVOKE_AMBIGUOUS_SRC = "{0} contains " |
364 |
|
+ "<invoke> with both \"src\" and \"srcexpr\" attributes specified," |
365 |
|
+ " must specify either one, but not both."; |
366 |
|
|
367 |
|
} |
368 |
|
|