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.Iterator;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.commons.scxml.ErrorReporter;
27 import org.apache.commons.scxml.model.SCXML;
28 import org.apache.commons.scxml.model.State;
29 import org.apache.commons.scxml.model.TransitionTarget;
30 import org.apache.commons.scxml.semantics.ErrorConstants;
31
32 /***
33 * Custom error reporter that log execution errors.
34 */
35 public class SimpleErrorReporter implements ErrorReporter, Serializable {
36
37 /*** Serial version UID. */
38 private static final long serialVersionUID = 1L;
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 == ErrorConstants.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 == ErrorConstants.UNKNOWN_ACTION) {
70
71 msg.append("Action: " + errCtx.getClass().getName());
72 } else if (errCode == ErrorConstants.ILLEGAL_CONFIG) {
73
74 if (errCtx instanceof Map.Entry) {
75 TransitionTarget tt = (TransitionTarget)
76 (((Map.Entry) errCtx).getKey());
77 Set vals = (Set) (((Map.Entry) errCtx).getValue());
78 msg.append(LogUtils.getTTPath(tt) + " : [");
79 for (Iterator i = vals.iterator(); i.hasNext();) {
80 TransitionTarget tx = (TransitionTarget) i.next();
81 msg.append(LogUtils.getTTPath(tx));
82 if (i.hasNext()) {
83 msg.append(", ");
84 }
85 }
86 msg.append(']');
87 } else if (errCtx instanceof Set) {
88 Set vals = (Set) errCtx;
89 msg.append("<SCXML> : [");
90 for (Iterator i = vals.iterator(); i.hasNext();) {
91 TransitionTarget tx = (TransitionTarget) i.next();
92 msg.append(LogUtils.getTTPath(tx));
93 if (i.hasNext()) {
94 msg.append(", ");
95 }
96 }
97 msg.append(']');
98 }
99 }
100 if (log.isWarnEnabled()) {
101 log.warn(msg.toString());
102 }
103 }
104
105 }
106