Debian Codegraph @

net/sf/saxon/testdriver/ErrorCollector.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.testdriver;
9
10 import net.sf.saxon.lib.NamespaceConstant;
11 import net.sf.saxon.lib.StandardErrorListener;
12 import net.sf.saxon.trans.QuitParsingException;
13 import net.sf.saxon.trans.XPathException;
14
15 import javax.xml.transform.TransformerException;
16 import java.util.HashSet;
17 import java.util.Set;
18
19 public class ErrorCollector extends StandardErrorListener {
20
21 private Set<String> errorCodes = new HashSet<String>();
22 private boolean foundWarnings = false;
23 private boolean madeEarlyExit = false;
24
25 public TransformerException lastException;
26
27 /**
28 * Receive notification of a warning.
29 * <p>Transformers can use this method to report conditions that
30 * are not errors or fatal errors. The default behaviour is to
31 * take no action.</p>
32 * <p>After invoking this method, the Transformer must continue with
33 * the transformation. It should still be possible for the
34 * application to process the document through to the end.</p>
35 *
36 * @param exception The warning information encapsulated in a
37 * transformer exception.
38 * @see javax.xml.transform.TransformerException
39 */
40 @Override
41 public void warning(TransformerException exception) {
42 if (exception instanceof QuitParsingException) {
43 System.err.println("** Early exit from parsing");
44 madeEarlyExit = true;
45 } else {
46 foundWarnings = true;
47 }
48 super.warning(exception);
49 }
50
51 @Override
52 public void error(TransformerException exception) {
53 lastException = exception;
54 addErrorCode(exception);
55 super.error(exception);
56 }
57
58 @Override
59 public void fatalError(TransformerException exception) {
60 addErrorCode(exception);
61 super.fatalError(exception);
62 }
63
64 /**
65 * Make a clean copy of this ErrorListener. This is necessary because the
66 * standard error listener is stateful (it remembers how many errors there have been)
67 *
68 * @param hostLanguage the host language (not used by this implementation)
69 * @return a copy of this error listener
70 */
71 @Override
72 public StandardErrorListener makeAnother(int hostLanguage) {
73 return this;
74 }
75
76 private void addErrorCode(TransformerException exception) {
77 if (exception instanceof XPathException) {
78 String errorCode = ((XPathException) exception).getErrorCodeLocalPart();
79 if (errorCode != null) {
80 String ns = ((XPathException) exception).getErrorCodeNamespace();
81 if (ns != null && !NamespaceConstant.ERR.equals(ns)) {
82 errorCode = "Q{" + ns + "}" + errorCode;
83 }
84 errorCodes.add(errorCode);
85 } else {
86 errorCodes.add("error-with-no-error-code");
87 }
88 }
89 }
90
91 public Set<String> getErrorCodes() {
92 return errorCodes;
93 }
94
95 public boolean getFoundWarnings() {
96 return foundWarnings;
97 }
98
99 public boolean isMadeEarlyExit() {
100 return madeEarlyExit;
101 }
102 }