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 com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
  25 import org.xml.sax.ErrorHandler;
  26 import org.xml.sax.SAXException;
  27 import org.xml.sax.SAXParseException;
  28 
  29 /**
  30  * Wraps {@link XMLErrorHandler} and make it look like a SAX {@link ErrorHandler}.
  31  *
  32  * <p>
  33  * The derived class should override the {@link #getErrorHandler()} method
  34  * so that it will return the correct {@link XMLErrorHandler} instance.
  35  * This method will be called whenever an error/warning is found.
  36  *
  37  * <p>
  38  * Experience shows that it is better to store the actual
  39  * {@link XMLErrorHandler} in one place and looks up that variable,
  40  * rather than copying it into every component that needs an error handler
  41  * and update all of them whenever it is changed, IMO.
  42  *
  43  * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  44  *
  45  */
  46 public abstract class ErrorHandlerProxy implements ErrorHandler {
  47 
  48     public void error(SAXParseException e) throws SAXException {
  49         XMLErrorHandler eh = getErrorHandler();
  50         if (eh instanceof ErrorHandlerWrapper) {
  51             ((ErrorHandlerWrapper)eh).fErrorHandler.error(e);
  52         }
  53         else {
  54             eh.error("","",ErrorHandlerWrapper.createXMLParseException(e));
  55         }
  56         // if an XNIException is thrown, just let it go.
  57         // REVISIT: is this OK? or should we try to wrap it into SAXException?
  58     }
  59 
  60     public void fatalError(SAXParseException e) throws SAXException {
  61         XMLErrorHandler eh = getErrorHandler();
  62         if (eh instanceof ErrorHandlerWrapper) {
  63             ((ErrorHandlerWrapper)eh).fErrorHandler.fatalError(e);
  64         }
  65         else {
  66             eh.fatalError("","",ErrorHandlerWrapper.createXMLParseException(e));
  67         }
  68     }
  69 
  70     public void warning(SAXParseException e) throws SAXException {
  71         XMLErrorHandler eh = getErrorHandler();
  72         if (eh instanceof ErrorHandlerWrapper) {
  73             ((ErrorHandlerWrapper)eh).fErrorHandler.warning(e);
  74         }
  75         else {
  76             eh.warning("","",ErrorHandlerWrapper.createXMLParseException(e));
  77         }
  78     }
  79 
  80     protected abstract XMLErrorHandler getErrorHandler();
  81 }