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