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