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.xpointer;
  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  * The Default XPointer error handler used by the XInclude implementation.
  32  * XPointer error's are thrown so that they may be caught by the XInclude
  33  * implementation and reported as resource errors.
  34  *
  35  */
  36 final class XPointerErrorHandler implements XMLErrorHandler {
  37 
  38     //
  39     // Data
  40     //
  41 
  42     /** Print writer. */
  43     protected PrintWriter fOut;
  44 
  45     //
  46     // Constructors
  47     //
  48 
  49     /**
  50      * Constructs an error handler that prints error messages to
  51      * <code>System.err</code>.
  52      */
  53     public XPointerErrorHandler() {
  54         this(new PrintWriter(System.err));
  55     } // <init>()
  56 
  57     /**
  58      * Constructs an error handler that prints error messages to the
  59      * specified <code>PrintWriter</code.
  60      */
  61     public XPointerErrorHandler(PrintWriter out) {
  62         fOut = out;
  63     } // <init>(PrintWriter)
  64 
  65     //
  66     // ErrorHandler methods
  67     //
  68 
  69     /** Warning. */
  70     public void warning(String domain, String key, XMLParseException ex)
  71             throws XNIException {
  72         printError("Warning", ex);
  73     } // warning(XMLParseException)
  74 
  75     /** Error. */
  76     public void error(String domain, String key, XMLParseException ex)
  77             throws XNIException {
  78         printError("Error", ex);
  79         //throw 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