listingerrorhandler.java

来自「java jdk 1.4的源码」· Java 代码 · 共 603 行 · 第 1/2 页

JAVA
603
字号
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights  * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment:   *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written  *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    nor may "Apache" appear in their name, without prior written *    permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 2000, Lotus * Development Corporation., http://www.lotus.com.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xml.utils;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import javax.xml.transform.ErrorListener;import javax.xml.transform.SourceLocator;import javax.xml.transform.TransformerException;import org.apache.xalan.res.XSLMessages;import org.apache.xalan.res.XSLTErrorResources;import org.xml.sax.ErrorHandler;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;/** * <meta name="usage" content="general"/> * Sample implementation of similar SAX ErrorHandler and JAXP ErrorListener.   * * <p>This implementation is suitable for various use cases, and  * provides some basic configuration API's as well to control  * when we re-throw errors, etc.</p> * * @author shane_curcuru@us.ibm.com * @version $Id: ListingErrorHandler.java,v 1.2 2002/06/07 17:13:54 sboag Exp $ */public class ListingErrorHandler implements ErrorHandler, ErrorListener{    protected PrintWriter m_pw = null;      /**     * Constructor ListingErrorHandler; user-supplied PrintWriter.       */    public ListingErrorHandler(PrintWriter pw)    {        if (null == pw)            throw new NullPointerException(XSLMessages.createMessage(XSLTErrorResources.ER_ERRORHANDLER_CREATED_WITH_NULL_PRINTWRITER, null));            // "ListingErrorHandler created with null PrintWriter!");                    m_pw = pw;    }    /**     * Constructor ListingErrorHandler; uses System.err.       */    public ListingErrorHandler()    {        m_pw = new PrintWriter(System.err, true);    }    /* ======== Implement org.xml.sax.ErrorHandler ======== */    /**     * Receive notification of a warning.     *     * <p>SAX parsers will use this method to report conditions that     * are not errors or fatal errors as defined by the XML 1.0     * recommendation.  The default behaviour is to take no action.</p>     *     * <p>The SAX parser must continue to provide normal parsing events     * after invoking this method: it should still be possible for the     * application to process the document through to the end.</p>     *     * <p>Filters may use this method to report other, non-XML warnings     * as well.</p>     *     * @param exception The warning information encapsulated in a     *                  SAX parse exception.     * @exception org.xml.sax.SAXException Any SAX exception, possibly     * wrapping another exception; only if setThrowOnWarning is true.     * @see org.xml.sax.SAXParseException      */    public void warning (SAXParseException exception)    	throws SAXException    {    	logExceptionLocation(m_pw, exception);        // Note: should we really call .toString() below, since         //  sometimes the message is not properly set?        m_pw.println("warning: " + exception.getMessage());        m_pw.flush();        if (getThrowOnWarning())            throw exception;    }            /**     * Receive notification of a recoverable error.     *     * <p>This corresponds to the definition of "error" in section 1.2     * of the W3C XML 1.0 Recommendation.  For example, a validating     * parser would use this callback to report the violation of a     * validity constraint.  The default behaviour is to take no     * action.</p>     *     * <p>The SAX parser must continue to provide normal parsing events     * after invoking this method: it should still be possible for the     * application to process the document through to the end.  If the     * application cannot do so, then the parser should report a fatal     * error even if the XML 1.0 recommendation does not require it to     * do so.</p>     *     * <p>Filters may use this method to report other, non-XML errors     * as well.</p>     *     * @param exception The error information encapsulated in a     *                  SAX parse exception.     * @exception org.xml.sax.SAXException Any SAX exception, possibly     * wrapping another exception; only if setThrowOnErroris true.     * @see org.xml.sax.SAXParseException      */    public void error (SAXParseException exception)    	throws SAXException    {    	logExceptionLocation(m_pw, exception);        m_pw.println("error: " + exception.getMessage());        m_pw.flush();        if (getThrowOnError())            throw exception;    }            /**     * Receive notification of a non-recoverable error.     *     * <p>This corresponds to the definition of "fatal error" in     * section 1.2 of the W3C XML 1.0 Recommendation.  For example, a     * parser would use this callback to report the violation of a     * well-formedness constraint.</p>     *     * <p>The application must assume that the document is unusable     * after the parser has invoked this method, and should continue     * (if at all) only for the sake of collecting addition error     * messages: in fact, SAX parsers are free to stop reporting any     * other events once this method has been invoked.</p>     *     * @param exception The error information encapsulated in a     *                  SAX parse exception.       * @exception org.xml.sax.SAXException Any SAX exception, possibly     * wrapping another exception; only if setThrowOnFatalError is true.     * @see org.xml.sax.SAXParseException     */    public void fatalError (SAXParseException exception)    	throws SAXException    {    	logExceptionLocation(m_pw, exception);        m_pw.println("fatalError: " + exception.getMessage());        m_pw.flush();        if (getThrowOnFatalError())            throw exception;    }    /* ======== Implement javax.xml.transform.ErrorListener ======== */    /**     * Receive notification of a warning.     *     * <p>{@link javax.xml.transform.Transformer} can use this method to report     * conditions that are not errors or fatal errors.  The default behaviour     * is to take no action.</p>     *     * <p>After invoking this method, the Transformer must continue with     * the transformation. It should still be possible for the     * application to process the document through to the end.</p>     *     * @param exception The warning information encapsulated in a     *                  transformer exception.     *     * @throws javax.xml.transform.TransformerException  only if      * setThrowOnWarning is true.     *     * @see javax.xml.transform.TransformerException     */    public void warning(TransformerException exception)        throws TransformerException    {    	logExceptionLocation(m_pw, exception);        m_pw.println("warning: " + exception.getMessage());        m_pw.flush();        if (getThrowOnWarning())            throw exception;    }    /**     * Receive notification of a recoverable error.     *     * <p>The transformer must continue to try and provide normal transformation     * after invoking this method.  It should still be possible for the     * application to process the document through to the end if no other errors     * are encountered.</p>     *     * @param exception The error information encapsulated in a     *                  transformer exception.     *     * @throws javax.xml.transform.TransformerException  only if      * setThrowOnError is true.     *     * @see javax.xml.transform.TransformerException     */    public void error(TransformerException exception)        throws TransformerException    {    	logExceptionLocation(m_pw, exception);        m_pw.println("error: " + exception.getMessage());        m_pw.flush();        if (getThrowOnError())            throw exception;    }    /**     * Receive notification of a non-recoverable error.     *     * <p>The transformer must continue to try and provide normal transformation     * after invoking this method.  It should still be possible for the     * application to process the document through to the end if no other errors     * are encountered, but there is no guarantee that the output will be     * useable.</p>     *     * @param exception The error information encapsulated in a     *                  transformer exception.     *     * @throws javax.xml.transform.TransformerException  only if      * setThrowOnError is true.     *     * @see javax.xml.transform.TransformerException     */    public void fatalError(TransformerException exception)        throws TransformerException    {    	logExceptionLocation(m_pw, exception);        m_pw.println("error: " + exception.getMessage());        m_pw.flush();        if (getThrowOnError())

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?