001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.util;
019:
020: import java.io.PrintWriter;
021:
022: import org.apache.xerces.xni.XNIException;
023: import org.apache.xerces.xni.parser.XMLErrorHandler;
024: import org.apache.xerces.xni.parser.XMLParseException;
025:
026: /**
027: * Default error handler.
028: *
029: * @author Andy Clark, IBM
030: *
031: * @version $Id: DefaultErrorHandler.java 449104 2006-09-22 22:03:29Z mrglavas $
032: */
033: public class DefaultErrorHandler implements XMLErrorHandler {
034:
035: //
036: // Data
037: //
038:
039: /** Print writer. */
040: protected PrintWriter fOut;
041:
042: //
043: // Constructors
044: //
045:
046: /**
047: * Constructs an error handler that prints error messages to
048: * <code>System.err</code>.
049: */
050: public DefaultErrorHandler() {
051: this (new PrintWriter(System.err));
052: } // <init>()
053:
054: /**
055: * Constructs an error handler that prints error messages to the
056: * specified <code>PrintWriter</code>.
057: */
058: public DefaultErrorHandler(PrintWriter out) {
059: fOut = out;
060: } // <init>(PrintWriter)
061:
062: //
063: // ErrorHandler methods
064: //
065:
066: /** Warning. */
067: public void warning(String domain, String key, XMLParseException ex)
068: throws XNIException {
069: printError("Warning", ex);
070: } // warning(XMLParseException)
071:
072: /** Error. */
073: public void error(String domain, String key, XMLParseException ex)
074: throws XNIException {
075: printError("Error", ex);
076: } // error(XMLParseException)
077:
078: /** Fatal error. */
079: public void fatalError(String domain, String key,
080: XMLParseException ex) throws XNIException {
081: printError("Fatal Error", ex);
082: throw ex;
083: } // fatalError(XMLParseException)
084:
085: //
086: // Private methods
087: //
088:
089: /** Prints the error message. */
090: private void printError(String type, XMLParseException ex) {
091:
092: fOut.print("[");
093: fOut.print(type);
094: fOut.print("] ");
095: String systemId = ex.getExpandedSystemId();
096: if (systemId != null) {
097: int index = systemId.lastIndexOf('/');
098: if (index != -1)
099: systemId = systemId.substring(index + 1);
100: fOut.print(systemId);
101: }
102: fOut.print(':');
103: fOut.print(ex.getLineNumber());
104: fOut.print(':');
105: fOut.print(ex.getColumnNumber());
106: fOut.print(": ");
107: fOut.print(ex.getMessage());
108: fOut.println();
109: fOut.flush();
110:
111: } // printError(String,SAXParseException)
112:
113: } // class DefaultErrorHandler
|