| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.commons.scxml; |
| 19 |
|
|
| 20 |
|
import java.util.HashSet; |
| 21 |
|
import java.util.IdentityHashMap; |
| 22 |
|
import java.util.Iterator; |
| 23 |
|
import java.util.List; |
| 24 |
|
import java.util.Map; |
| 25 |
|
import java.util.Set; |
| 26 |
|
|
| 27 |
|
import org.apache.commons.logging.Log; |
| 28 |
|
import org.apache.commons.logging.LogFactory; |
| 29 |
|
import org.apache.commons.scxml.model.Data; |
| 30 |
|
import org.apache.commons.scxml.model.Datamodel; |
| 31 |
|
import org.apache.commons.scxml.model.Parallel; |
| 32 |
|
import org.apache.commons.scxml.model.Path; |
| 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 |
|
import org.w3c.dom.CharacterData; |
| 37 |
|
import org.w3c.dom.Node; |
| 38 |
|
import org.w3c.dom.Text; |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
public final class SCXMLHelper { |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
public static boolean isStringEmpty(final String attr) { |
| 53 |
1051 |
if (attr == null || attr.trim().length() == 0) { |
| 54 |
439 |
return true; |
| 55 |
|
} |
| 56 |
612 |
return false; |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
public static boolean isDescendant(final TransitionTarget tt, |
| 70 |
|
final TransitionTarget ctx) { |
| 71 |
482 |
TransitionTarget parent = tt.getParent(); |
| 72 |
787 |
while (parent != null) { |
| 73 |
459 |
if (parent == ctx) { |
| 74 |
154 |
return true; |
| 75 |
|
} |
| 76 |
305 |
parent = parent.getParent(); |
| 77 |
|
} |
| 78 |
328 |
return false; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
public static Set getAncestorClosure(final Set states, |
| 91 |
|
final Set upperBounds) { |
| 92 |
701 |
Set closure = new HashSet(states.size() * 2); |
| 93 |
701 |
for (Iterator i = states.iterator(); i.hasNext();) { |
| 94 |
612 |
TransitionTarget tt = (TransitionTarget) i.next(); |
| 95 |
1589 |
while (tt != null) { |
| 96 |
1087 |
if (!closure.add(tt)) { |
| 97 |
|
|
| 98 |
29 |
break; |
| 99 |
|
} |
| 100 |
1058 |
if (upperBounds != null && upperBounds.contains(tt)) { |
| 101 |
81 |
break; |
| 102 |
|
} |
| 103 |
977 |
tt = tt.getParent(); |
| 104 |
|
} |
| 105 |
|
} |
| 106 |
701 |
return closure; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
public static boolean isLegalConfig(final Set states, |
| 121 |
|
final ErrorReporter errRep) { |
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
209 |
boolean legalConfig = true; |
| 131 |
209 |
Map counts = new IdentityHashMap(); |
| 132 |
209 |
Set scxmlCount = new HashSet(); |
| 133 |
209 |
for (Iterator i = states.iterator(); i.hasNext();) { |
| 134 |
222 |
TransitionTarget tt = (TransitionTarget) i.next(); |
| 135 |
222 |
TransitionTarget parent = null; |
| 136 |
435 |
while ((parent = tt.getParent()) != null) { |
| 137 |
213 |
HashSet cnt = (HashSet) counts.get(parent); |
| 138 |
213 |
if (cnt == null) { |
| 139 |
188 |
cnt = new HashSet(); |
| 140 |
188 |
counts.put(parent, cnt); |
| 141 |
|
} |
| 142 |
213 |
cnt.add(tt); |
| 143 |
213 |
tt = parent; |
| 144 |
|
} |
| 145 |
|
|
| 146 |
222 |
scxmlCount.add(tt); |
| 147 |
|
} |
| 148 |
|
|
| 149 |
209 |
for (Iterator i = counts.entrySet().iterator(); i.hasNext();) { |
| 150 |
188 |
Map.Entry entry = (Map.Entry) i.next(); |
| 151 |
188 |
TransitionTarget tt = (TransitionTarget) entry.getKey(); |
| 152 |
188 |
Set count = (Set) entry.getValue(); |
| 153 |
188 |
if (tt instanceof Parallel) { |
| 154 |
11 |
Parallel p = (Parallel) tt; |
| 155 |
11 |
if (count.size() < p.getStates().size()) { |
| 156 |
1 |
errRep.onError(ErrorReporter.ILLEGAL_CONFIG, |
| 157 |
|
"Not all AND states active for parallel " |
| 158 |
|
+ p.getId(), entry); |
| 159 |
1 |
legalConfig = false; |
| 160 |
|
} |
| 161 |
|
} else { |
| 162 |
177 |
if (count.size() > 1) { |
| 163 |
1 |
errRep.onError(ErrorReporter.ILLEGAL_CONFIG, |
| 164 |
|
"Multiple OR states active for state " |
| 165 |
|
+ tt.getId(), entry); |
| 166 |
1 |
legalConfig = false; |
| 167 |
|
} |
| 168 |
|
} |
| 169 |
188 |
count.clear(); |
| 170 |
|
} |
| 171 |
209 |
if (scxmlCount.size() > 1) { |
| 172 |
1 |
errRep.onError(ErrorReporter.ILLEGAL_CONFIG, |
| 173 |
|
"Multiple top-level OR states active!", scxmlCount); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
209 |
scxmlCount.clear(); |
| 177 |
209 |
counts.clear(); |
| 178 |
209 |
return legalConfig; |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
public static TransitionTarget getLCA(final TransitionTarget tt1, |
| 190 |
|
final TransitionTarget tt2) { |
| 191 |
84 |
if (tt1 == tt2) { |
| 192 |
1 |
return tt1; |
| 193 |
83 |
} else if (isDescendant(tt1, tt2)) { |
| 194 |
2 |
return tt2; |
| 195 |
81 |
} else if (isDescendant(tt2, tt1)) { |
| 196 |
2 |
return tt1; |
| 197 |
|
} |
| 198 |
79 |
Set parents = new HashSet(); |
| 199 |
79 |
TransitionTarget tmp = tt1; |
| 200 |
133 |
while ((tmp = tmp.getParent()) != null) { |
| 201 |
54 |
if (tmp instanceof State) { |
| 202 |
46 |
parents.add(tmp); |
| 203 |
|
} |
| 204 |
|
} |
| 205 |
79 |
tmp = tt2; |
| 206 |
86 |
while ((tmp = tmp.getParent()) != null) { |
| 207 |
41 |
if (tmp instanceof State) { |
| 208 |
|
|
| 209 |
37 |
if (!parents.add(tmp)) { |
| 210 |
34 |
parents.clear(); |
| 211 |
34 |
return tmp; |
| 212 |
|
} |
| 213 |
|
} |
| 214 |
|
} |
| 215 |
45 |
return null; |
| 216 |
|
} |
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
public static Set getStatesExited(final Transition t, |
| 233 |
|
final Set currentStates) { |
| 234 |
89 |
Set allStates = new HashSet(); |
| 235 |
89 |
Path p = t.getPath(); |
| 236 |
|
|
| 237 |
89 |
allStates.addAll(p.getUpwardSegment()); |
| 238 |
89 |
TransitionTarget source = t.getParent(); |
| 239 |
89 |
for (Iterator act = currentStates.iterator(); act.hasNext();) { |
| 240 |
93 |
TransitionTarget a = (TransitionTarget) act.next(); |
| 241 |
93 |
if (isDescendant(a, source)) { |
| 242 |
8 |
boolean added = false; |
| 243 |
8 |
added = allStates.add(a); |
| 244 |
20 |
while (added && a != source) { |
| 245 |
12 |
a = a.getParent(); |
| 246 |
12 |
added = allStates.add(a); |
| 247 |
|
} |
| 248 |
|
} |
| 249 |
|
} |
| 250 |
89 |
if (p.isCrossRegion()) { |
| 251 |
0 |
for (Iterator regions = p.getRegionsExited().iterator(); |
| 252 |
0 |
regions.hasNext();) { |
| 253 |
0 |
Parallel par = ((Parallel) ((State) regions.next()). |
| 254 |
|
getParent()); |
| 255 |
|
|
| 256 |
0 |
for (Iterator siblings = par.getStates().iterator(); |
| 257 |
0 |
siblings.hasNext();) { |
| 258 |
0 |
State s = (State) siblings.next(); |
| 259 |
0 |
for (Iterator act = currentStates.iterator(); |
| 260 |
0 |
act.hasNext();) { |
| 261 |
0 |
TransitionTarget a = (TransitionTarget) act.next(); |
| 262 |
0 |
if (isDescendant(a, s)) { |
| 263 |
|
|
| 264 |
0 |
boolean added = false; |
| 265 |
0 |
added = allStates.add(a); |
| 266 |
0 |
while (added && a != s) { |
| 267 |
0 |
a = a.getParent(); |
| 268 |
0 |
added = allStates.add(a); |
| 269 |
|
} |
| 270 |
|
} |
| 271 |
|
} |
| 272 |
|
} |
| 273 |
|
} |
| 274 |
|
} |
| 275 |
89 |
return allStates; |
| 276 |
|
} |
| 277 |
|
|
| 278 |
|
|
| 279 |
|
|
| 280 |
|
|
| 281 |
|
|
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
|
|
| 287 |
|
|
| 288 |
|
public static boolean inConflict(final Transition t1, |
| 289 |
|
final Transition t2, final Set currentStates) { |
| 290 |
0 |
Set ts1 = getStatesExited(t1, currentStates); |
| 291 |
0 |
Set ts2 = getStatesExited(t2, currentStates); |
| 292 |
0 |
ts1.retainAll(ts2); |
| 293 |
0 |
if (ts1.isEmpty()) { |
| 294 |
0 |
return false; |
| 295 |
|
} |
| 296 |
0 |
return true; |
| 297 |
|
} |
| 298 |
|
|
| 299 |
|
|
| 300 |
|
|
| 301 |
|
|
| 302 |
|
|
| 303 |
|
|
| 304 |
|
|
| 305 |
|
|
| 306 |
|
public static boolean subtypeOf(final Class child, final Class parent) { |
| 307 |
5 |
if (child == null || parent == null) { |
| 308 |
0 |
return false; |
| 309 |
|
} |
| 310 |
6 |
for (Class current = child; current != Object.class; |
| 311 |
7 |
current = current.getSuperclass()) { |
| 312 |
11 |
if (current == parent) { |
| 313 |
4 |
return true; |
| 314 |
|
} |
| 315 |
|
} |
| 316 |
1 |
return false; |
| 317 |
|
} |
| 318 |
|
|
| 319 |
|
|
| 320 |
|
|
| 321 |
|
|
| 322 |
|
|
| 323 |
|
|
| 324 |
|
|
| 325 |
|
|
| 326 |
|
public static boolean implementationOf(final Class clas, |
| 327 |
|
final Class interfayce) { |
| 328 |
20 |
if (clas == null || interfayce == null || !interfayce.isInterface()) { |
| 329 |
0 |
return false; |
| 330 |
|
} |
| 331 |
20 |
for (Class current = clas; current != Object.class; |
| 332 |
40 |
current = current.getSuperclass()) { |
| 333 |
40 |
Class[] implementedInterfaces = current.getInterfaces(); |
| 334 |
60 |
for (int i = 0; i < implementedInterfaces.length; i++) { |
| 335 |
20 |
if (implementedInterfaces[i] == interfayce) { |
| 336 |
0 |
return true; |
| 337 |
|
} |
| 338 |
|
} |
| 339 |
|
} |
| 340 |
20 |
return false; |
| 341 |
|
} |
| 342 |
|
|
| 343 |
|
|
| 344 |
|
|
| 345 |
|
|
| 346 |
|
|
| 347 |
|
|
| 348 |
|
|
| 349 |
|
public static void setNodeValue(final Node node, final String value) { |
| 350 |
6 |
switch(node.getNodeType()) { |
| 351 |
|
case Node.ATTRIBUTE_NODE: |
| 352 |
0 |
node.setNodeValue(value); |
| 353 |
0 |
break; |
| 354 |
|
case Node.ELEMENT_NODE: |
| 355 |
|
|
| 356 |
6 |
if (node.hasChildNodes()) { |
| 357 |
6 |
Node child = node.getFirstChild(); |
| 358 |
12 |
while (child != null) { |
| 359 |
6 |
if (child.getNodeType() == Node.TEXT_NODE) { |
| 360 |
6 |
node.removeChild(child); |
| 361 |
|
} |
| 362 |
6 |
child = child.getNextSibling(); |
| 363 |
|
} |
| 364 |
|
} |
| 365 |
|
|
| 366 |
6 |
Text txt = node.getOwnerDocument().createTextNode(value); |
| 367 |
6 |
node.appendChild(txt); |
| 368 |
6 |
break; |
| 369 |
|
case Node.TEXT_NODE: |
| 370 |
|
case Node.CDATA_SECTION_NODE: |
| 371 |
0 |
((CharacterData) node).setData(value); |
| 372 |
0 |
break; |
| 373 |
|
default: |
| 374 |
0 |
String err = "Trying to set value of a strange Node type: " |
| 375 |
|
+ node.getNodeType(); |
| 376 |
|
|
| 377 |
0 |
throw new IllegalArgumentException(err); |
| 378 |
|
} |
| 379 |
6 |
} |
| 380 |
|
|
| 381 |
|
|
| 382 |
|
|
| 383 |
|
|
| 384 |
|
|
| 385 |
|
|
| 386 |
|
|
| 387 |
|
public static String getNodeValue(final Node node) { |
| 388 |
16 |
String result = ""; |
| 389 |
16 |
if (node == null) { |
| 390 |
0 |
return result; |
| 391 |
|
} |
| 392 |
16 |
switch(node.getNodeType()) { |
| 393 |
|
case Node.ATTRIBUTE_NODE: |
| 394 |
0 |
result = node.getNodeValue(); |
| 395 |
0 |
break; |
| 396 |
|
case Node.ELEMENT_NODE: |
| 397 |
16 |
if (node.hasChildNodes()) { |
| 398 |
16 |
Node child = node.getFirstChild(); |
| 399 |
16 |
StringBuffer buf = new StringBuffer(); |
| 400 |
32 |
while (child != null) { |
| 401 |
16 |
if (child.getNodeType() == Node.TEXT_NODE) { |
| 402 |
16 |
buf.append(((CharacterData) child).getData()); |
| 403 |
|
} |
| 404 |
16 |
child = child.getNextSibling(); |
| 405 |
|
} |
| 406 |
16 |
result = buf.toString(); |
| 407 |
|
} |
| 408 |
|
break; |
| 409 |
|
case Node.TEXT_NODE: |
| 410 |
|
case Node.CDATA_SECTION_NODE: |
| 411 |
0 |
result = ((CharacterData) node).getData(); |
| 412 |
0 |
break; |
| 413 |
|
default: |
| 414 |
0 |
String err = "Trying to get value of a strange Node type: " |
| 415 |
|
+ node.getNodeType(); |
| 416 |
|
|
| 417 |
0 |
throw new IllegalArgumentException(err); |
| 418 |
|
} |
| 419 |
16 |
return result.trim(); |
| 420 |
|
} |
| 421 |
|
|
| 422 |
|
|
| 423 |
|
|
| 424 |
|
|
| 425 |
|
|
| 426 |
|
|
| 427 |
|
|
| 428 |
|
|
| 429 |
|
|
| 430 |
|
public static void cloneDatamodel(final Datamodel datamodel, |
| 431 |
|
final Context ctx, final Evaluator evaluator, |
| 432 |
|
final Log log) { |
| 433 |
188 |
if (datamodel == null) { |
| 434 |
179 |
return; |
| 435 |
|
} |
| 436 |
9 |
List data = datamodel.getData(); |
| 437 |
9 |
if (data == null) { |
| 438 |
0 |
return; |
| 439 |
|
} |
| 440 |
9 |
for (Iterator iter = data.iterator(); iter.hasNext();) { |
| 441 |
14 |
Data datum = (Data) iter.next(); |
| 442 |
14 |
Node datumNode = datum.getNode(); |
| 443 |
14 |
Node valueNode = null; |
| 444 |
14 |
if (datumNode != null) { |
| 445 |
10 |
valueNode = datumNode.cloneNode(true); |
| 446 |
|
} |
| 447 |
|
|
| 448 |
14 |
if (!SCXMLHelper.isStringEmpty(datum.getSrc())) { |
| 449 |
0 |
ctx.setLocal(datum.getName(), valueNode); |
| 450 |
14 |
} else if (!SCXMLHelper.isStringEmpty(datum. |
| 451 |
|
getExpr())) { |
| 452 |
4 |
Object value = null; |
| 453 |
|
try { |
| 454 |
4 |
value = evaluator.eval(ctx, datum.getExpr()); |
| 455 |
0 |
} catch (SCXMLExpressionException see) { |
| 456 |
0 |
if (log != null) { |
| 457 |
0 |
log.error(see.getMessage(), see); |
| 458 |
|
} else { |
| 459 |
0 |
Log defaultLog = LogFactory.getLog(SCXMLHelper.class); |
| 460 |
0 |
defaultLog.error(see.getMessage(), see); |
| 461 |
|
} |
| 462 |
4 |
} |
| 463 |
4 |
ctx.setLocal(datum.getName(), value); |
| 464 |
|
} else { |
| 465 |
10 |
ctx.setLocal(datum.getName(), valueNode); |
| 466 |
|
} |
| 467 |
|
} |
| 468 |
9 |
} |
| 469 |
|
|
| 470 |
|
|
| 471 |
|
|
| 472 |
|
|
| 473 |
|
private SCXMLHelper() { |
| 474 |
0 |
super(); |
| 475 |
0 |
} |
| 476 |
|
|
| 477 |
|
} |
| 478 |
|
|