1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xerces.internal.util;
  23 
  24 import java.io.PrintWriter;
  25 
  26 import com.sun.org.apache.xerces.internal.xni.XNIException;
  27 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
  28 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;
  29 
  30 /**
  31  * Default error handler.
  32  *
  33  * @author Andy Clark, IBM
  34  *
  35  */
  36 public class DefaultErrorHandler
  37     implements XMLErrorHandler {
  38 
  39     //
  40     // Data
  41     //
  42 
  43     /** Print writer. */
  44     protected PrintWriter fOut;
  45 
  46     //
  47     // Constructors
  48     //
  49 
  50     /**
  51      * Constructs an error handler that prints error messages to
  52      * <code>System.err</code>.
  53      */
  54     public DefaultErrorHandler() {
  55         this(new PrintWriter(System.err));
  56     } // <init>()
  57 
  58     /**
  59      * Constructs an error handler that prints error messages to the
  60      * specified <code>PrintWriter</code.
  61      */
  62     public DefaultErrorHandler(PrintWriter out) {
  63         fOut = out;
  64     } // <init>(PrintWriter)
  65 
  66     //
  67     // ErrorHandler methods
  68     //
  69 
  70     /** Warning. */
  71     public void warning(String domain, String key, XMLParseException ex)
  72         throws XNIException {
  73         printError("Warning", ex);
  74     } // warning(XMLParseException)
  75 
  76     /** Error. */
  77     public void error(String domain, String key, XMLParseException ex)
  78         throws XNIException {
  79         printError("Error", ex);
  80     } // error(XMLParseException)
  81 
  82     /** Fatal error. */
  83     public void fatalError(String domain, String key, XMLParseException ex)
  84         throws XNIException {
  85         printError("Fatal Error", ex);
  86         throw ex;
  87     } // fatalError(XMLParseException)
  88 
  89     //
  90     // Private methods
  91     //
  92 
  93     /** Prints the error message. */
  94     private void printError(String type, XMLParseException ex) {
  95 
  96         fOut.print("[");
  97         fOut.print(type);
  98         fOut.print("] ");
  99         String systemId = ex.getExpandedSystemId();
 100         if (systemId != null) {
 101             int index = systemId.lastIndexOf('/');
 102             if (index != -1)
 103                 systemId = systemId.substring(index + 1);
 104             fOut.print(systemId);
 105         }
 106         fOut.print(':');
 107         fOut.print(ex.getLineNumber());
 108         fOut.print(':');
 109         fOut.print(ex.getColumnNumber());
 110         fOut.print(": ");
 111         fOut.print(ex.getMessage());
 112         fOut.println();
 113         fOut.flush();
 114 
 115     } // printError(String,SAXParseException)
 116 
 117 } // class DefaultErrorHandler