286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.util;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLLocator;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XNIException;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;
286N/Aimport org.xml.sax.ErrorHandler;
286N/Aimport org.xml.sax.SAXException;
286N/Aimport org.xml.sax.SAXParseException;
286N/A
286N/A/**
286N/A * This class wraps a SAX error handler in an XNI error handler.
286N/A *
286N/A * @see ErrorHandler
286N/A *
286N/A * @author Andy Clark, IBM
286N/A *
286N/A */
286N/Apublic class ErrorHandlerWrapper
286N/A implements XMLErrorHandler {
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A /** The SAX error handler. */
286N/A protected ErrorHandler fErrorHandler;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Default constructor. */
286N/A public ErrorHandlerWrapper() {}
286N/A
286N/A /** Wraps the specified SAX error handler. */
286N/A public ErrorHandlerWrapper(ErrorHandler errorHandler) {
286N/A setErrorHandler(errorHandler);
286N/A } // <init>(ErrorHandler)
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A /** Sets the SAX error handler. */
286N/A public void setErrorHandler(ErrorHandler errorHandler) {
286N/A fErrorHandler = errorHandler;
286N/A } // setErrorHandler(ErrorHandler)
286N/A
286N/A /** Returns the SAX error handler. */
286N/A public ErrorHandler getErrorHandler() {
286N/A return fErrorHandler;
286N/A } // getErrorHandler():ErrorHandler
286N/A
286N/A //
286N/A // XMLErrorHandler methods
286N/A //
286N/A
286N/A /**
286N/A * Reports a warning. Warnings are non-fatal and can be safely ignored
286N/A * by most applications.
286N/A *
286N/A * @param domain The domain of the warning. The domain can be any
286N/A * string but is suggested to be a valid URI. The
286N/A * domain can be used to conveniently specify a web
286N/A * site location of the relevent specification or
286N/A * document pertaining to this warning.
286N/A * @param key The warning key. This key can be any string and
286N/A * is implementation dependent.
286N/A * @param exception Exception.
286N/A *
286N/A * @throws XNIException Thrown to signal that the parser should stop
286N/A * parsing the document.
286N/A */
286N/A public void warning(String domain, String key,
286N/A XMLParseException exception) throws XNIException {
286N/A
286N/A if (fErrorHandler != null) {
286N/A SAXParseException saxException = createSAXParseException(exception);
286N/A
286N/A try {
286N/A fErrorHandler.warning(saxException);
286N/A }
286N/A catch (SAXParseException e) {
286N/A throw createXMLParseException(e);
286N/A }
286N/A catch (SAXException e) {
286N/A throw createXNIException(e);
286N/A }
286N/A }
286N/A
286N/A } // warning(String,String,XMLParseException)
286N/A
286N/A /**
286N/A * Reports an error. Errors are non-fatal and usually signify that the
286N/A * document is invalid with respect to its grammar(s).
286N/A *
286N/A * @param domain The domain of the error. The domain can be any
286N/A * string but is suggested to be a valid URI. The
286N/A * domain can be used to conveniently specify a web
286N/A * site location of the relevent specification or
286N/A * document pertaining to this error.
286N/A * @param key The error key. This key can be any string and
286N/A * is implementation dependent.
286N/A * @param exception Exception.
286N/A *
286N/A * @throws XNIException Thrown to signal that the parser should stop
286N/A * parsing the document.
286N/A */
286N/A public void error(String domain, String key,
286N/A XMLParseException exception) throws XNIException {
286N/A
286N/A if (fErrorHandler != null) {
286N/A SAXParseException saxException = createSAXParseException(exception);
286N/A
286N/A try {
286N/A fErrorHandler.error(saxException);
286N/A }
286N/A catch (SAXParseException e) {
286N/A throw createXMLParseException(e);
286N/A }
286N/A catch (SAXException e) {
286N/A throw createXNIException(e);
286N/A }
286N/A }
286N/A
286N/A } // error(String,String,XMLParseException)
286N/A
286N/A /**
286N/A * Report a fatal error. Fatal errors usually occur when the document
286N/A * is not well-formed and signifies that the parser cannot continue
286N/A * normal operation.
286N/A * <p>
286N/A * <strong>Note:</strong> The error handler should <em>always</em>
286N/A * throw an <code>XNIException</code> from this method. This exception
286N/A * can either be the same exception that is passed as a parameter to
286N/A * the method or a new XNI exception object. If the registered error
286N/A * handler fails to throw an exception, the continuing operation of
286N/A * the parser is undetermined.
286N/A *
286N/A * @param domain The domain of the fatal error. The domain can be
286N/A * any string but is suggested to be a valid URI. The
286N/A * domain can be used to conveniently specify a web
286N/A * site location of the relevent specification or
286N/A * document pertaining to this fatal error.
286N/A * @param key The fatal error key. This key can be any string
286N/A * and is implementation dependent.
286N/A * @param exception Exception.
286N/A *
286N/A * @throws XNIException Thrown to signal that the parser should stop
286N/A * parsing the document.
286N/A */
286N/A public void fatalError(String domain, String key,
286N/A XMLParseException exception) throws XNIException {
286N/A
286N/A if (fErrorHandler != null) {
286N/A SAXParseException saxException = createSAXParseException(exception);
286N/A
286N/A try {
286N/A fErrorHandler.fatalError(saxException);
286N/A }
286N/A catch (SAXParseException e) {
286N/A throw createXMLParseException(e);
286N/A }
286N/A catch (SAXException e) {
286N/A throw createXNIException(e);
286N/A }
286N/A }
286N/A
286N/A } // fatalError(String,String,XMLParseException)
286N/A
286N/A //
286N/A // Protected methods
286N/A //
286N/A
286N/A /** Creates a SAXParseException from an XMLParseException. */
286N/A protected static SAXParseException createSAXParseException(XMLParseException exception) {
286N/A return new SAXParseException(exception.getMessage(),
286N/A exception.getPublicId(),
286N/A exception.getExpandedSystemId(),
286N/A exception.getLineNumber(),
286N/A exception.getColumnNumber(),
286N/A exception.getException());
286N/A } // createSAXParseException(XMLParseException):SAXParseException
286N/A
286N/A /** Creates an XMLParseException from a SAXParseException. */
286N/A protected static XMLParseException createXMLParseException(SAXParseException exception) {
286N/A final String fPublicId = exception.getPublicId();
286N/A final String fExpandedSystemId = exception.getSystemId();
286N/A final int fLineNumber = exception.getLineNumber();
286N/A final int fColumnNumber = exception.getColumnNumber();
286N/A XMLLocator location = new XMLLocator() {
286N/A public String getPublicId() { return fPublicId; }
286N/A public String getExpandedSystemId() { return fExpandedSystemId; }
286N/A public String getBaseSystemId() { return null; }
286N/A public String getLiteralSystemId() { return null; }
286N/A public int getColumnNumber() { return fColumnNumber; }
286N/A public int getLineNumber() { return fLineNumber; }
286N/A public int getCharacterOffset() { return -1; }
286N/A public String getEncoding() { return null; }
286N/A public String getXMLVersion() { return null; }
286N/A };
286N/A return new XMLParseException(location, exception.getMessage(),exception);
286N/A } // createXMLParseException(SAXParseException):XMLParseException
286N/A
286N/A /** Creates an XNIException from a SAXException.
286N/A NOTE: care should be taken *not* to call this with a SAXParseException; this will
286N/A lose information!!! */
286N/A protected static XNIException createXNIException(SAXException exception) {
286N/A return new XNIException(exception.getMessage(),exception);
286N/A } // createXNIException(SAXException):XMLParseException
286N/A} // class ErrorHandlerWrapper