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