001: package net.sf.saxon;
002:
003: import net.sf.saxon.trans.DynamicError;
004: import net.sf.saxon.trans.SaxonErrorCode;
005: import org.xml.sax.SAXException;
006: import org.xml.sax.SAXParseException;
007:
008: import javax.xml.transform.ErrorListener;
009: import javax.xml.transform.TransformerException;
010: import java.io.PrintWriter;
011: import java.io.Writer;
012:
013: public class StandardErrorHandler implements org.xml.sax.ErrorHandler,
014: javax.xml.transform.SourceLocator {
015:
016: ////////////////////////////////////////////////////////////////////////////
017: // Implement the org.xml.sax.ErrorHandler interface.
018: ////////////////////////////////////////////////////////////////////////////
019:
020: private ErrorListener errorListener;
021: private String systemId;
022: private int lineNumber = -1;
023: private int columnNumber = -1;
024:
025: private Writer errorOutput = new PrintWriter(System.err);
026:
027: public StandardErrorHandler(ErrorListener listener) {
028: errorListener = listener;
029: }
030:
031: /**
032: * Set output for error messages produced by the default error handler.
033: * The default error handler does not throw an exception
034: * for parse errors or input I/O errors, rather it returns a result code and
035: * writes diagnostics to a user-specified output writer, which defaults to
036: * System.err<BR>
037: * This call has no effect if setErrorHandler() has been called to supply a
038: * user-defined error handler
039: * @param writer The Writer to use for error messages
040: */
041:
042: public void setErrorOutput(Writer writer) {
043: errorOutput = writer;
044: }
045:
046: /**
047: * Callback interface for SAX: not for application use
048: */
049:
050: public void warning(SAXParseException e) {
051: if (errorListener != null) {
052: try {
053: errorListener.warning(new TransformerException(e));
054: } catch (Exception err) {
055: }
056: }
057: }
058:
059: /**
060: * Callback interface for SAX: not for application use
061: */
062:
063: public void error(SAXParseException e) throws SAXException {
064: reportError(e, false);
065: //failed = true;
066: }
067:
068: /**
069: * Callback interface for SAX: not for application use
070: */
071:
072: public void fatalError(SAXParseException e) throws SAXException {
073: reportError(e, true);
074: //failed = true;
075: throw e;
076: }
077:
078: /**
079: * Common routine for SAX errors and fatal errors
080: */
081:
082: protected void reportError(SAXParseException e, boolean isFatal) {
083:
084: if (errorListener != null) {
085: try {
086: systemId = e.getSystemId();
087: lineNumber = e.getLineNumber();
088: columnNumber = e.getColumnNumber();
089: DynamicError err = new DynamicError(
090: "Error reported by XML parser", this , e);
091: err.setErrorCode(SaxonErrorCode.SXXP0003);
092: if (isFatal) {
093: errorListener.fatalError(err);
094: } else {
095: errorListener.error(err);
096: }
097: } catch (Exception err) {
098: }
099: } else {
100:
101: try {
102: String errcat = (isFatal ? "Fatal error" : "Error");
103: errorOutput.write(errcat + " reported by XML parser: "
104: + e.getMessage() + "\n");
105: errorOutput
106: .write(" URL: " + e.getSystemId() + "\n");
107: errorOutput.write(" Line: " + e.getLineNumber()
108: + "\n");
109: errorOutput.write(" Column: " + e.getColumnNumber()
110: + "\n");
111: errorOutput.flush();
112: } catch (Exception e2) {
113: System.err.println(e);
114: System.err.println(e2);
115: e2.printStackTrace();
116: }
117: ;
118: }
119: }
120:
121: ////////////////////////////////////////////////////////////////////////////
122: // Implement the SourceLocator interface.
123: ////////////////////////////////////////////////////////////////////////////
124:
125: public String getSystemId() {
126: return systemId;
127: }
128:
129: public String getPublicId() {
130: return null;
131: }
132:
133: public int getLineNumber() {
134: return lineNumber;
135: }
136:
137: public int getColumnNumber() {
138: return columnNumber;
139: }
140: }
141:
142: //
143: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
144: // you may not use this file except in compliance with the License. You may obtain a copy of the
145: // License at http://www.mozilla.org/MPL/
146: //
147: // Software distributed under the License is distributed on an "AS IS" basis,
148: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
149: // See the License for the specific language governing rights and limitations under the License.
150: //
151: // The Original Code is: all this file.
152: //
153: // The Initial Developer of the Original Code is Michael H. Kay.
154: //
155: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
156: //
157: // Contributor(s): none.
158: //
|