1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml.env;
18
19 import java.util.Iterator;
20 import java.util.LinkedList;
21
22 import org.apache.commons.scxml.model.Transition;
23 import org.apache.commons.scxml.model.TransitionTarget;
24
25 /***
26 * Helper methods for Commons SCXML logging.
27 *
28 */
29 public final class LogUtils {
30
31 /***
32 * Create a human readable log view of this transition.
33 *
34 * @param from The source TransitionTarget
35 * @param to The destination TransitionTarget
36 * @param transition The Transition that is taken
37 * @return String The human readable log entry
38 */
39 public static String transToString(final TransitionTarget from,
40 final TransitionTarget to, final Transition transition) {
41 StringBuffer buf = new StringBuffer("transition (");
42 buf.append("event = ").append(transition.getEvent());
43 buf.append(", cond = ").append(transition.getCond());
44 buf.append(", from = ").append(getTTPath(from));
45 buf.append(", to = ").append(getTTPath(to));
46 buf.append(')');
47 return buf.toString();
48 }
49
50 /***
51 * Write out this TransitionTarget location in a XPath style format.
52 *
53 * @param tt The TransitionTarget whose "path" is to needed
54 * @return String The XPath style location of the TransitionTarget within
55 * the SCXML document
56 */
57 public static String getTTPath(final TransitionTarget tt) {
58 TransitionTarget parent = tt.getParent();
59 if (parent == null) {
60 return "/" + tt.getId();
61 } else {
62 LinkedList pathElements = new LinkedList();
63 pathElements.addFirst(tt);
64 while (parent != null) {
65 pathElements.addFirst(parent);
66 parent = parent.getParent();
67 }
68 StringBuffer names = new StringBuffer();
69 for (Iterator i = pathElements.iterator(); i.hasNext();) {
70 TransitionTarget pathElement = (TransitionTarget) i.next();
71 names.append('/').append(pathElement.getId());
72 }
73 return names.toString();
74 }
75 }
76
77 /***
78 * Discourage instantiation since this is a utility class.
79 */
80 private LogUtils() {
81 super();
82 }
83
84 }