Skip to content

Commit 600f581

Browse files
authored
Merge pull request #4372 from evolvedbinary/improve-xslt-errors
Improve XSLT errors
2 parents 42e2c18 + 66c70c7 commit 600f581

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

exist-core/src/main/java/org/exist/http/servlets/XSLTServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class XSLTServlet extends HttpServlet {
8989
new XSLTErrorsListener<ServletException>(true, false) {
9090

9191
@Override
92-
protected void raiseError(String error, Exception ex) throws ServletException {
92+
protected void raiseError(final String error, final TransformerException ex) throws ServletException {
9393
throw new ServletException(error, ex);
9494
}
9595
};

exist-core/src/main/java/org/exist/xquery/functions/transform/Transform.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
188188
final XSLTErrorsListener<XPathException> errorListener =
189189
new XSLTErrorsListener<XPathException>(stopOnError, stopOnWarn) {
190190
@Override
191-
protected void raiseError(String error, Exception ex) throws XPathException {
191+
protected void raiseError(final String error, final TransformerException ex) throws XPathException {
192192
throw new XPathException(Transform.this, error, ex);
193193
}
194194
};

exist-core/src/main/java/org/exist/xslt/XSLTErrorsListener.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
package org.exist.xslt;
2323

24+
import javax.annotation.Nullable;
2425
import javax.xml.transform.ErrorListener;
2526
import javax.xml.transform.TransformerException;
2627
import org.apache.logging.log4j.LogManager;
@@ -31,64 +32,70 @@
3132
*/
3233
public abstract class XSLTErrorsListener<E extends Exception> implements ErrorListener {
3334

34-
private final static Logger LOG = LogManager.getLogger(XSLTErrorsListener.class);
35+
private static final Logger LOG = LogManager.getLogger(XSLTErrorsListener.class);
3536

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+
}
4040

41-
boolean stopOnError;
42-
boolean stopOnWarn;
41+
private final boolean stopOnError;
42+
private final boolean stopOnWarn;
4343

44-
private int errorCode = NO_ERROR;
45-
private Exception exception;
44+
@Nullable private ErrorType errorType;
45+
@Nullable private TransformerException exception;
4646

47-
public XSLTErrorsListener(boolean stopOnError, boolean stopOnWarn) {
47+
public XSLTErrorsListener(final boolean stopOnError, final boolean stopOnWarn) {
4848
this.stopOnError = stopOnError;
4949
this.stopOnWarn = stopOnWarn;
5050
}
5151

52-
protected abstract void raiseError(String error, Exception ex) throws E;
52+
protected abstract void raiseError(String error, TransformerException ex) throws E;
5353

5454
public void checkForErrors() throws E {
55-
switch (errorCode) {
55+
if (errorType == null) {
56+
return;
57+
}
58+
59+
switch (errorType) {
5660
case WARNING:
5761
if (stopOnWarn) {
58-
raiseError("XSL transform reported warning: " + exception.getMessage(), exception);
62+
raiseError("XSL transform reported warning: " + exception.getMessageAndLocation(), exception);
5963
}
6064
break;
6165
case ERROR:
6266
if (stopOnError) {
63-
raiseError("XSL transform reported error: " + exception.getMessage(), exception);
67+
raiseError("XSL transform reported error: " + exception.getMessageAndLocation(), exception);
6468
}
6569
break;
6670
case FATAL:
67-
raiseError("XSL transform reported error: " + exception.getMessage(), exception);
71+
raiseError("XSL transform reported error: " + exception.getMessageAndLocation(), exception);
6872
}
6973
}
7074

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;
7479
exception = except;
7580
if (stopOnWarn) {
7681
throw except;
7782
}
7883
}
7984

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;
8389
exception = except;
8490
if (stopOnError) {
8591
throw except;
8692
}
8793
}
8894

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;
9299
exception = except;
93100
throw except;
94101
}

0 commit comments

Comments
 (0)