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.HashSet;
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import org.apache.commons.scxml.ErrorReporter;
29 import org.apache.commons.scxml.model.SCXML;
30 import org.apache.commons.scxml.model.State;
31 import org.apache.commons.scxml.model.Transition;
32 import org.apache.commons.scxml.model.TransitionTarget;
33
34 /***
35 * Custom error reporter that log execution errors.
36 */
37 public class SimpleErrorReporter implements ErrorReporter {
38
39 /*** Log. */
40 private Log log = LogFactory.getLog(getClass());
41
42 /***
43 * Constructor.
44 */
45 public SimpleErrorReporter() {
46 super();
47 }
48
49 /***
50 * @see ErrorReporter#onError(String, String, Object)
51 */
52 public void onError(final String errorCode, final String errDetail,
53 final Object errCtx) {
54
55
56 String errCode = errorCode.intern();
57 StringBuffer msg = new StringBuffer();
58 msg.append(errCode).append(" (");
59 msg.append(errDetail).append("): ");
60 if (errCode == ErrorReporter.NO_INITIAL) {
61 if (errCtx instanceof SCXML) {
62
63 msg.append("<SCXML>");
64 } else if (errCtx instanceof State) {
65
66
67 msg.append("State " + LogUtils.getTTPath((State) errCtx));
68 }
69 } else if (errCode == ErrorReporter.UNKNOWN_ACTION) {
70
71 msg.append("Action: " + errCtx.getClass().getName());
72 } else if (errCode == ErrorReporter.NON_DETERMINISTIC) {
73
74 msg.append(" [");
75 if (errCtx instanceof HashSet) {
76 for (Iterator i = ((Set) errCtx).iterator(); i.hasNext();) {
77 Transition t = (Transition) i.next();
78 msg.append(LogUtils.transToString(t.getParent(),
79 t.getTarget(), t));
80 if (i.hasNext()) {
81 msg.append(", ");
82 }
83 }
84 }
85 msg.append(']');
86 } else if (errCode == ErrorReporter.ILLEGAL_CONFIG) {
87
88 if (errCtx instanceof Map.Entry) {
89 TransitionTarget tt = (TransitionTarget)
90 (((Map.Entry) errCtx).getKey());
91 Set vals = (Set) (((Map.Entry) errCtx).getValue());
92 msg.append(LogUtils.getTTPath(tt) + " : [");
93 for (Iterator i = vals.iterator(); i.hasNext();) {
94 TransitionTarget tx = (TransitionTarget) i.next();
95 msg.append(LogUtils.getTTPath(tx));
96 if (i.hasNext()) {
97 msg.append(", ");
98 }
99 }
100 msg.append(']');
101 } else if (errCtx instanceof Set) {
102 Set vals = (Set) errCtx;
103 msg.append("<SCXML> : [");
104 for (Iterator i = vals.iterator(); i.hasNext();) {
105 TransitionTarget tx = (TransitionTarget) i.next();
106 msg.append(LogUtils.getTTPath(tx));
107 if (i.hasNext()) {
108 msg.append(", ");
109 }
110 }
111 msg.append(']');
112 }
113 }
114 if (log.isWarnEnabled()) {
115 log.warn(msg.toString());
116 }
117 }
118
119 }
120