Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.internal.ws.wscompile; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 29 | import com.sun.istack.internal.SAXParseException2; |
aoqi@0 | 30 | import com.sun.tools.internal.ws.resources.ModelMessages; |
aoqi@0 | 31 | import com.sun.tools.internal.xjc.api.ErrorListener; |
aoqi@0 | 32 | import org.xml.sax.ErrorHandler; |
aoqi@0 | 33 | import org.xml.sax.Locator; |
aoqi@0 | 34 | import org.xml.sax.SAXParseException; |
aoqi@0 | 35 | |
aoqi@0 | 36 | /** |
aoqi@0 | 37 | * Implemented by the driver of the compiler engine to handle |
aoqi@0 | 38 | * errors found during the compiliation. |
aoqi@0 | 39 | * |
aoqi@0 | 40 | * <p> |
aoqi@0 | 41 | * This class implements {@link org.xml.sax.ErrorHandler} so it can be |
aoqi@0 | 42 | * passed to anywhere where {@link org.xml.sax.ErrorHandler} is expected. |
aoqi@0 | 43 | * |
aoqi@0 | 44 | * <p> |
aoqi@0 | 45 | * However, to make the error handling easy (and make it work |
aoqi@0 | 46 | * with visitor patterns nicely), |
aoqi@0 | 47 | * none of the methods on this class throws {@link org.xml.sax.SAXException}. |
aoqi@0 | 48 | * Instead, when the compilation needs to be aborted, |
aoqi@0 | 49 | * it throws {@link AbortException}, which is unchecked. |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * <p> |
aoqi@0 | 52 | * This also implements the externally visible {@link com.sun.tools.internal.xjc.api.ErrorListener} |
aoqi@0 | 53 | * so that we can reuse our internal implementation for testing and such. |
aoqi@0 | 54 | * |
aoqi@0 | 55 | * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
aoqi@0 | 56 | * @author Vivek Pandey |
aoqi@0 | 57 | */ |
aoqi@0 | 58 | public abstract class ErrorReceiver implements ErrorHandler, ErrorListener { |
aoqi@0 | 59 | |
aoqi@0 | 60 | // |
aoqi@0 | 61 | // |
aoqi@0 | 62 | // convenience methods for callers |
aoqi@0 | 63 | // |
aoqi@0 | 64 | // |
aoqi@0 | 65 | /** |
aoqi@0 | 66 | * @param loc |
aoqi@0 | 67 | * can be null if the location is unknown |
aoqi@0 | 68 | */ |
aoqi@0 | 69 | public final void error( Locator loc, String msg ) { |
aoqi@0 | 70 | error( new SAXParseException2(msg,loc) ); |
aoqi@0 | 71 | } |
aoqi@0 | 72 | |
aoqi@0 | 73 | public final void error( Locator loc, String msg, Exception e ) { |
aoqi@0 | 74 | error( new SAXParseException2(msg,loc,e) ); |
aoqi@0 | 75 | } |
aoqi@0 | 76 | |
aoqi@0 | 77 | public final void error( String msg, Exception e ) { |
aoqi@0 | 78 | error( new SAXParseException2(msg,null,e) ); |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | public void error(Exception e) { |
aoqi@0 | 82 | error(e.getMessage(),e); |
aoqi@0 | 83 | } |
aoqi@0 | 84 | |
aoqi@0 | 85 | /** |
aoqi@0 | 86 | * Reports a warning. |
aoqi@0 | 87 | */ |
aoqi@0 | 88 | public final void warning( @Nullable Locator loc, String msg ) { |
aoqi@0 | 89 | warning( new SAXParseException(msg,loc) ); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | // |
aoqi@0 | 93 | // |
aoqi@0 | 94 | // ErrorHandler implementation, but can't throw SAXException |
aoqi@0 | 95 | // |
aoqi@0 | 96 | // |
aoqi@0 | 97 | public abstract void error(SAXParseException exception) throws AbortException; |
aoqi@0 | 98 | public abstract void fatalError(SAXParseException exception) throws AbortException; |
aoqi@0 | 99 | public abstract void warning(SAXParseException exception) throws AbortException; |
aoqi@0 | 100 | |
aoqi@0 | 101 | /** |
aoqi@0 | 102 | * This method will be invoked periodically to allow {@link com.sun.tools.internal.xjc.AbortException} |
aoqi@0 | 103 | * to be thrown, especially when this is driven by some kind of GUI. |
aoqi@0 | 104 | */ |
aoqi@0 | 105 | public void pollAbort() throws AbortException { |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | |
aoqi@0 | 109 | /** |
aoqi@0 | 110 | * Reports verbose messages to users. |
aoqi@0 | 111 | * |
aoqi@0 | 112 | * This method can be used to report additional non-essential |
aoqi@0 | 113 | * messages. The implementation usually discards them |
aoqi@0 | 114 | * unless some specific debug option is turned on. |
aoqi@0 | 115 | */ |
aoqi@0 | 116 | public abstract void info(SAXParseException exception) /*REVISIT:throws AbortException*/; |
aoqi@0 | 117 | |
aoqi@0 | 118 | /** |
aoqi@0 | 119 | * Reports a debug message to users. |
aoqi@0 | 120 | * |
aoqi@0 | 121 | * @see #info(SAXParseException) |
aoqi@0 | 122 | */ |
aoqi@0 | 123 | public final void debug( String msg ) { |
aoqi@0 | 124 | info( new SAXParseException(msg,null) ); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | |
aoqi@0 | 127 | public abstract void debug(SAXParseException exception); |
aoqi@0 | 128 | |
aoqi@0 | 129 | // |
aoqi@0 | 130 | // |
aoqi@0 | 131 | // convenience methods for derived classes |
aoqi@0 | 132 | // |
aoqi@0 | 133 | // |
aoqi@0 | 134 | |
aoqi@0 | 135 | /** |
aoqi@0 | 136 | * Returns the human readable string representation of the |
aoqi@0 | 137 | * {@link org.xml.sax.Locator} part of the specified |
aoqi@0 | 138 | * {@link SAXParseException}. |
aoqi@0 | 139 | * |
aoqi@0 | 140 | * @return non-null valid object. |
aoqi@0 | 141 | */ |
aoqi@0 | 142 | protected final String getLocationString( SAXParseException e ) { |
aoqi@0 | 143 | if(e.getLineNumber()!=-1 || e.getSystemId()!=null) { |
aoqi@0 | 144 | int line = e.getLineNumber(); |
aoqi@0 | 145 | return ModelMessages.CONSOLE_ERROR_REPORTER_LINE_X_OF_Y(line==-1?"?":Integer.toString( line ), |
aoqi@0 | 146 | getShortName( e.getSystemId())); |
aoqi@0 | 147 | } else { |
aoqi@0 | 148 | return ""; //for unkown location just return empty string |
aoqi@0 | 149 | } |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | /** Computes a short name of a given URL for display. */ |
aoqi@0 | 153 | private String getShortName( String url ) { |
aoqi@0 | 154 | if(url==null) |
aoqi@0 | 155 | return ModelMessages.CONSOLE_ERROR_REPORTER_UNKNOWN_LOCATION(); |
aoqi@0 | 156 | return url; |
aoqi@0 | 157 | } |
aoqi@0 | 158 | } |