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