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