View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2005 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.util;
22  
23  import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
24  import org.xml.sax.ErrorHandler;
25  import org.xml.sax.SAXException;
26  import org.xml.sax.SAXParseException;
27  
28  /**
29   * Wraps {@link XMLErrorHandler} and make it look like a SAX {@link ErrorHandler}.
30   *
31   * <p>
32   * The derived class should override the {@link #getErrorHandler()} method
33   * so that it will return the correct {@link XMLErrorHandler} instance.
34   * This method will be called whenever an error/warning is found.
35   *
36   * <p>
37   * Experience shows that it is better to store the actual
38   * {@link XMLErrorHandler} in one place and looks up that variable,
39   * rather than copying it into every component that needs an error handler
40   * and update all of them whenever it is changed, IMO.
41   *
42   * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
43   *
44   */
45  public abstract class ErrorHandlerProxy implements ErrorHandler {
46  
47      public void error(SAXParseException e) throws SAXException {
48          XMLErrorHandler eh = getErrorHandler();
49          if (eh instanceof ErrorHandlerWrapper) {
50              ((ErrorHandlerWrapper)eh).fErrorHandler.error(e);
51          }
52          else {
53              eh.error("","",ErrorHandlerWrapper.createXMLParseException(e));
54          }
55          // if an XNIException is thrown, just let it go.
56          // REVISIT: is this OK? or should we try to wrap it into SAXException?
57      }
58  
59      public void fatalError(SAXParseException e) throws SAXException {
60          XMLErrorHandler eh = getErrorHandler();
61          if (eh instanceof ErrorHandlerWrapper) {
62              ((ErrorHandlerWrapper)eh).fErrorHandler.fatalError(e);
63          }
64          else {
65              eh.fatalError("","",ErrorHandlerWrapper.createXMLParseException(e));
66          }
67      }
68  
69      public void warning(SAXParseException e) throws SAXException {
70          XMLErrorHandler eh = getErrorHandler();
71          if (eh instanceof ErrorHandlerWrapper) {
72              ((ErrorHandlerWrapper)eh).fErrorHandler.warning(e);
73          }
74          else {
75              eh.warning("","",ErrorHandlerWrapper.createXMLParseException(e));
76          }
77      }
78  
79      protected abstract XMLErrorHandler getErrorHandler();
80  }