|
21 | 21 | */
|
22 | 22 | package org.exist.xslt;
|
23 | 23 |
|
| 24 | +import javax.annotation.Nullable; |
24 | 25 | import javax.xml.transform.ErrorListener;
|
25 | 26 | import javax.xml.transform.TransformerException;
|
26 | 27 | import org.apache.logging.log4j.LogManager;
|
|
31 | 32 | */
|
32 | 33 | public abstract class XSLTErrorsListener<E extends Exception> implements ErrorListener {
|
33 | 34 |
|
34 |
| - private final static Logger LOG = LogManager.getLogger(XSLTErrorsListener.class); |
| 35 | + private static final Logger LOG = LogManager.getLogger(XSLTErrorsListener.class); |
35 | 36 |
|
36 |
| - private final static int NO_ERROR = 0; |
37 |
| - private final static int WARNING = 1; |
38 |
| - private final static int ERROR = 2; |
39 |
| - private final static int FATAL = 3; |
| 37 | + private enum ErrorType { |
| 38 | + WARNING, ERROR, FATAL; |
| 39 | + } |
40 | 40 |
|
41 |
| - boolean stopOnError; |
42 |
| - boolean stopOnWarn; |
| 41 | + private final boolean stopOnError; |
| 42 | + private final boolean stopOnWarn; |
43 | 43 |
|
44 |
| - private int errorCode = NO_ERROR; |
45 |
| - private Exception exception; |
| 44 | + @Nullable private ErrorType errorType; |
| 45 | + @Nullable private TransformerException exception; |
46 | 46 |
|
47 |
| - public XSLTErrorsListener(boolean stopOnError, boolean stopOnWarn) { |
| 47 | + public XSLTErrorsListener(final boolean stopOnError, final boolean stopOnWarn) { |
48 | 48 | this.stopOnError = stopOnError;
|
49 | 49 | this.stopOnWarn = stopOnWarn;
|
50 | 50 | }
|
51 | 51 |
|
52 |
| - protected abstract void raiseError(String error, Exception ex) throws E; |
| 52 | + protected abstract void raiseError(String error, TransformerException ex) throws E; |
53 | 53 |
|
54 | 54 | public void checkForErrors() throws E {
|
55 |
| - switch (errorCode) { |
| 55 | + if (errorType == null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + switch (errorType) { |
56 | 60 | case WARNING:
|
57 | 61 | if (stopOnWarn) {
|
58 |
| - raiseError("XSL transform reported warning: " + exception.getMessage(), exception); |
| 62 | + raiseError("XSL transform reported warning: " + exception.getMessageAndLocation(), exception); |
59 | 63 | }
|
60 | 64 | break;
|
61 | 65 | case ERROR:
|
62 | 66 | if (stopOnError) {
|
63 |
| - raiseError("XSL transform reported error: " + exception.getMessage(), exception); |
| 67 | + raiseError("XSL transform reported error: " + exception.getMessageAndLocation(), exception); |
64 | 68 | }
|
65 | 69 | break;
|
66 | 70 | case FATAL:
|
67 |
| - raiseError("XSL transform reported error: " + exception.getMessage(), exception); |
| 71 | + raiseError("XSL transform reported error: " + exception.getMessageAndLocation(), exception); |
68 | 72 | }
|
69 | 73 | }
|
70 | 74 |
|
71 |
| - public void warning(TransformerException except) throws TransformerException { |
72 |
| - LOG.warn("XSL transform reports warning: {}", except.getMessage(), except); |
73 |
| - errorCode = WARNING; |
| 75 | + @Override |
| 76 | + public void warning(final TransformerException except) throws TransformerException { |
| 77 | + LOG.warn("XSL transform reports warning: {}", except.getMessage(), except); |
| 78 | + errorType = ErrorType.WARNING; |
74 | 79 | exception = except;
|
75 | 80 | if (stopOnWarn) {
|
76 | 81 | throw except;
|
77 | 82 | }
|
78 | 83 | }
|
79 | 84 |
|
80 |
| - public void error(TransformerException except) throws TransformerException { |
81 |
| - LOG.warn("XSL transform reports recoverable error: {}", except.getMessage(), except); |
82 |
| - errorCode = ERROR; |
| 85 | + @Override |
| 86 | + public void error(final TransformerException except) throws TransformerException { |
| 87 | + LOG.warn("XSL transform reports recoverable error: {}", except.getMessage(), except); |
| 88 | + errorType = ErrorType.ERROR; |
83 | 89 | exception = except;
|
84 | 90 | if (stopOnError) {
|
85 | 91 | throw except;
|
86 | 92 | }
|
87 | 93 | }
|
88 | 94 |
|
89 |
| - public void fatalError(TransformerException except) throws TransformerException { |
90 |
| - LOG.warn("XSL transform reports fatal error: {}", except.getMessage(), except); |
91 |
| - errorCode = FATAL; |
| 95 | + @Override |
| 96 | + public void fatalError(final TransformerException except) throws TransformerException { |
| 97 | + LOG.warn("XSL transform reports fatal error: {}", except.getMessage(), except); |
| 98 | + errorType = ErrorType.FATAL; |
92 | 99 | exception = except;
|
93 | 100 | throw except;
|
94 | 101 | }
|
|
0 commit comments