Debian Codegraph @

net/sf/saxon/lib/StandardErrorHandler.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.lib;
9
10 import net.sf.saxon.expr.parser.ExplicitLocation;
11 import net.sf.saxon.trans.SaxonErrorCode;
12 import net.sf.saxon.trans.XPathException;
13 import org.xml.sax.SAXException;
14 import org.xml.sax.SAXParseException;
15
16 import javax.xml.transform.ErrorListener;
17
18 /**
19 * A default implementation of the SAX ErrorHandler interface. Used by Saxon to catch XML parsing errors
20 * if no error handler is supplied by the application.
21 */
22
23 public class StandardErrorHandler implements org.xml.sax.ErrorHandler {
24
25 ////////////////////////////////////////////////////////////////////////////
26 // Implement the org.xml.sax.ErrorHandler interface.
27 ////////////////////////////////////////////////////////////////////////////
28
29 private ErrorListener errorListener;
30 private int warningCount = 0;
31 private int errorCount = 0;
32 private int fatalErrorCount = 0;
33 private boolean silent = false;
34
35 public StandardErrorHandler(ErrorListener listener) {
36 if (listener == null) {
37 throw new NullPointerException();
38 }
39 errorListener = listener;
40 }
41
42 /**
43 * Indicate whether the error handler should report errors to the ErrorListener
44 * @param silent if true, errors should not be reported. Used during doc-available() processing
45 */
46
47 public void setSilent(boolean silent) {
48 this.silent = silent;
49 }
50
51 /**
52 * Callback interface for SAX: not for application use
53 */
54
55 public void warning(SAXParseException e) {
56 try {
57 warningCount++;
58 if (!silent) {
59 errorListener.warning(XPathException.makeXPathException(e));
60 }
61 } catch (Exception ignored) {
62 }
63 }
64
65 /**
66 * Callback interface for SAX: not for application use
67 */
68
69 public void error(SAXParseException e) throws SAXException {
70 //System.err.println("ErrorHandler.error " + e.getMessage());
71 errorCount++;
72 if (!silent) {
73 reportError(e, false);
74 }
75 }
76
77 /**
78 * Callback interface for SAX: not for application use
79 */
80
81 public void fatalError(/*@NotNull*/ SAXParseException e) throws SAXException {
82 //System.err.println("ErrorHandler.fatalError " + e.getMessage());
83 fatalErrorCount++;
84 if (!silent) {
85 reportError(e, true);
86 }
87 throw e;
88 }
89
90 /**
91 * Common routine for SAX errors and fatal errors
92 *
93 * @param e the exception being handled
94 * @param isFatal true if the error is classified as fatal
95 */
96
97 protected void reportError(SAXParseException e, boolean isFatal) {
98 try {
99 ExplicitLocation loc =
100 new ExplicitLocation(e.getSystemId(), e.getLineNumber(), e.getColumnNumber());
101 XPathException err = new XPathException("Error reported by XML parser", loc, e);
102 err.setErrorCode(SaxonErrorCode.SXXP0003);
103 if (isFatal) {
104 errorListener.fatalError(err);
105 } else {
106 errorListener.error(err);
107 }
108 } catch (Exception ignored) {
109 }
110 }
111
112 /**
113 * Return the number of warnings (including warnings) reported
114 *
115 * @return the number of warnings
116 */
117
118 public int getWarningCount() {
119 return warningCount;
120 }
121
122 /**
123 * Return the number of errors reported
124 *
125 * @return the number of non-fatal errors
126 */
127
128 public int getErrorCount() {
129 return errorCount;
130 }
131
132 /**
133 * Return the number of fatal errors reported
134 *
135 * @return the number of fatal errors
136 */
137
138 public int getFatalErrorCount() {
139 return fatalErrorCount;
140 }
141 }
142