| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class StandardErrorHandler implements org.xml.sax.ErrorHandler { |
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public void setSilent(boolean silent) { |
| 48 |
this.silent = silent; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 69 |
public void error(SAXParseException e) throws SAXException { |
| 70 |
|
| 71 |
errorCount++; |
| 72 |
if (!silent) { |
| 73 |
reportError(e, false); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public void fatalError( SAXParseException e) throws SAXException { |
| 82 |
|
| 83 |
fatalErrorCount++; |
| 84 |
if (!silent) { |
| 85 |
reportError(e, true); |
| 86 |
} |
| 87 |
throw e; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public int getWarningCount() { |
| 119 |
return warningCount; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public int getErrorCount() { |
| 129 |
return errorCount; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public int getFatalErrorCount() { |
| 139 |
return fatalErrorCount; |
| 140 |
} |
| 141 |
} |
| 142 |
|