listingerrorhandler.java

来自「JAVA 所有包」· Java 代码 · 共 565 行 · 第 1/2 页

JAVA
565
字号
        if (null == pw)            pw = new PrintWriter(System.err, true);                SourceLocator locator = null;        Throwable cause = exception;        // Try to find the locator closest to the cause.        do        {            // Find the current locator, if one present            if(cause instanceof SAXParseException)            {                // A SAXSourceLocator is a Xalan helper class                 //  that implements both a SourceLocator and a SAX Locator                //@todo check that the new locator actually has                 //  as much or more information as the                 //  current one already does                locator = new SAXSourceLocator((SAXParseException)cause);            }            else if (cause instanceof TransformerException)            {                SourceLocator causeLocator = ((TransformerException)cause).getLocator();                if(null != causeLocator)                {                    locator = causeLocator;                }            }                        // Then walk back down the chain of exceptions            if(cause instanceof TransformerException)                cause = ((TransformerException)cause).getCause();            else if(cause instanceof WrappedRuntimeException)                cause = ((WrappedRuntimeException)cause).getException();            else if(cause instanceof SAXException)                cause = ((SAXException)cause).getException();            else                cause = null;        }        while(null != cause);        // Formatting note: mimic javac-like errors:        //  path\filename:123: message-here        //  systemId:L=1;C=2: message-here        if(null != locator)        {            String id = (locator.getPublicId() != locator.getPublicId())                      ? locator.getPublicId()                        : (null != locator.getSystemId())                          ? locator.getSystemId() : "SystemId-Unknown";            pw.print(id + ":Line=" + locator.getLineNumber()                             + ";Column=" + locator.getColumnNumber()+": ");            pw.println("exception:" + exception.getMessage());            pw.println("root-cause:"                        + ((null != cause) ? cause.getMessage() : "null"));            logSourceLine(pw, locator);         }        else        {            pw.print("SystemId-Unknown:locator-unavailable: ");            pw.println("exception:" + exception.getMessage());            pw.println("root-cause:"                        + ((null != cause) ? cause.getMessage() : "null"));        }    }    /**     * Print out the specific source line that caused the exception,      * if possible to load it.       *     * @param pw PrintWriter to send output to     * @param locator Xalan wrapper for either a JAXP or a SAX      * source location object     */    public static void logSourceLine(PrintWriter pw, SourceLocator locator)    {        if (null == locator)            return;                    if (null == pw)            pw = new PrintWriter(System.err, true);        String url = locator.getSystemId();        // Bail immediately if we get SystemId-Unknown        //@todo future improvement: attempt to get resource         //  from a publicId if possible        if (null == url)        {            pw.println("line: (No systemId; cannot read file)");            pw.println();            return;        }                //@todo attempt to get DOM backpointer or other ids        try        {            int line = locator.getLineNumber();            int column = locator.getColumnNumber();            pw.println("line: " + getSourceLine(url, line));            StringBuffer buf = new StringBuffer("line: ");            for (int i = 1; i < column; i++)            {                buf.append(' ');            }            buf.append('^');            pw.println(buf.toString());        }        catch (Exception e)        {            pw.println("line: logSourceLine unavailable due to: " + e.getMessage());            pw.println();        }    }    /**     * Return the specific source line that caused the exception,      * if possible to load it; allow exceptions to be thrown.       *     * @author shane_curcuru@us.ibm.com     */    protected static String getSourceLine(String sourceUrl, int lineNum)            throws Exception    {        URL url = null;        // Get a URL from the sourceUrl        try        {            // Try to get a URL from it as-is            url = new URL(sourceUrl);        }        catch (java.net.MalformedURLException mue)        {            int indexOfColon = sourceUrl.indexOf(':');            int indexOfSlash = sourceUrl.indexOf('/');                        if ((indexOfColon != -1)                && (indexOfSlash != -1)                && (indexOfColon < indexOfSlash))            {                // The url is already absolute, but we could not get                 //  the system to form it, so bail                throw mue;            }            else            {                // The url is relative, so attempt to get absolute                url = new URL(SystemIDResolver.getAbsoluteURI(sourceUrl));                // If this fails, allow the exception to propagate            }        }                String line = null;        InputStream is = null;        BufferedReader br = null;        try        {            // Open the URL and read to our specified line            URLConnection uc = url.openConnection();            is = uc.getInputStream();            br = new BufferedReader(new InputStreamReader(is));            // Not the most efficient way, but it works            // (Feel free to patch to seek to the appropriate line)            for (int i = 1; i <= lineNum; i++)            {                line = br.readLine();            }                    }         // Allow exceptions to propagate from here, but ensure         //  streams are closed!        finally        {            br.close();            is.close();        }                // Return whatever we found        return line;    }        /* ======== Implement settable properties ======== */    /**     * User-settable behavior: when to re-throw exceptions.       *     * <p>This allows per-instance configuration of      * ListingErrorHandlers.  You can ask us to either throw      * an exception when we're called for various warning /      * error / fatalErrors, or simply log them and continue.</p>     *     * @param b if we should throw an exception on warnings     */    public void setThrowOnWarning(boolean b)    {        throwOnWarning = b;    }    /**     * User-settable behavior: when to re-throw exceptions.       *     * @return if we throw an exception on warnings     */    public boolean getThrowOnWarning()    {        return throwOnWarning;    }    /** If we should throw exception on warnings; default:false.  */    protected boolean throwOnWarning = false;    /**     * User-settable behavior: when to re-throw exceptions.       *     * <p>This allows per-instance configuration of      * ListingErrorHandlers.  You can ask us to either throw      * an exception when we're called for various warning /      * error / fatalErrors, or simply log them and continue.</p>     *     * <p>Note that the behavior of many parsers/transformers      * after an error is not necessarily defined!</p>     *     * @param b if we should throw an exception on errors     */    public void setThrowOnError(boolean b)    {        throwOnError = b;    }    /**     * User-settable behavior: when to re-throw exceptions.       *     * @return if we throw an exception on errors     */    public boolean getThrowOnError()    {        return throwOnError;    }    /** If we should throw exception on errors; default:true.  */    protected boolean throwOnError = true;    /**     * User-settable behavior: when to re-throw exceptions.       *     * <p>This allows per-instance configuration of      * ListingErrorHandlers.  You can ask us to either throw      * an exception when we're called for various warning /      * error / fatalErrors, or simply log them and continue.</p>     *     * <p>Note that the behavior of many parsers/transformers      * after a fatalError is not necessarily defined, most      * products will probably barf if you continue.</p>     *     * @param b if we should throw an exception on fatalErrors     */    public void setThrowOnFatalError(boolean b)    {        throwOnFatalError = b;    }    /**     * User-settable behavior: when to re-throw exceptions.       *     * @return if we throw an exception on fatalErrors     */    public boolean getThrowOnFatalError()    {        return throwOnFatalError;    }    /** If we should throw exception on fatalErrors; default:true.  */    protected boolean throwOnFatalError = true;}

⌨️ 快捷键说明

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