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 /*
  27  * Use is subject to the license terms.
  28  */
  29 package com.sun.tools.internal.xjc;
  30 
  31 import com.sun.istack.internal.SAXParseException2;
  32 import com.sun.tools.internal.xjc.api.ErrorListener;
  33 
  34 import org.xml.sax.ErrorHandler;
  35 import org.xml.sax.Locator;
  36 import org.xml.sax.SAXParseException;
  37 
  38 /**
  39  * Implemented by the driver of the compiler engine to handle
  40  * errors found during the compiliation.
  41  *
  42  * <p>
  43  * This class implements {@link ErrorHandler} so it can be
  44  * passed to anywhere where {@link ErrorHandler} is expected.
  45  *
  46  * <p>
  47  * However, to make the error handling easy (and make it work
  48  * with visitor patterns nicely),
  49  * none of the methods on thi class throws {@link org.xml.sax.SAXException}.
  50  * Instead, when the compilation needs to be aborted,
  51  * it throws {@link AbortException}, which is unchecked.
  52  *
  53  * <p>
  54  * This also implements the externally visible {@link ErrorListener}
  55  * so that we can reuse our internal implementation for testing and such.
  56  *
  57  * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  58  */
  59 public abstract class ErrorReceiver  implements ErrorHandler, ErrorListener {
  60 
  61 //
  62 //
  63 // convenience methods for callers
  64 //
  65 //
  66     /**
  67      * @param loc
  68      *      can be null if the location is unknown
  69      */
  70     public final void error( Locator loc, String msg ) {
  71         error( new SAXParseException2(msg,loc) );
  72     }
  73 
  74     public final void error( Locator loc, String msg, Exception e ) {
  75         error( new SAXParseException2(msg,loc,e) );
  76     }
  77 
  78     public final void error( String msg, Exception e ) {
  79         error( new SAXParseException2(msg,null,e) );
  80     }
  81 
  82     public void error(Exception e) {
  83         error(e.getMessage(),e);
  84     }
  85 
  86     /**
  87      * @param loc
  88      *      can be null if the location is unknown
  89      */
  90     public final void warning( Locator loc, String msg ) {
  91         warning( new SAXParseException(msg,loc) );
  92     }
  93 
  94 //
  95 //
  96 // ErrorHandler implementation, but can't throw SAXException
  97 //
  98 //
  99     public abstract void error(SAXParseException exception) throws AbortException;
 100     public abstract void fatalError(SAXParseException exception) throws AbortException;
 101     public abstract void warning(SAXParseException exception) throws AbortException;
 102 
 103     /**
 104      * This method will be invoked periodically to allow {@link AbortException}
 105      * to be thrown, especially when this is driven by some kind of GUI.
 106      */
 107     public void pollAbort() throws AbortException {
 108     }
 109 
 110     /**
 111      * Reports verbose messages to users.
 112      *
 113      * This method can be used to report additional non-essential
 114      * messages. The implementation usually discards them
 115      * unless some specific debug option is turned on.
 116      */
 117     public abstract void info(SAXParseException exception) /*REVISIT:throws AbortException*/;
 118 
 119     /**
 120      * Reports a debug message to users.
 121      *
 122      * @see #info(SAXParseException)
 123      */
 124     public final void debug( String msg ) {
 125         info( new SAXParseException(msg,null) );
 126     }
 127 
 128 //
 129 //
 130 // convenience methods for derived classes
 131 //
 132 //
 133 
 134   /**
 135    * Returns the human readable string representation of the
 136    * {@link org.xml.sax.Locator} part of the specified
 137    * {@link SAXParseException}.
 138    *
 139    * @return  non-null valid object.
 140    */
 141   protected final String getLocationString( SAXParseException e ) {
 142       if(e.getLineNumber()!=-1 || e.getSystemId()!=null) {
 143           int line = e.getLineNumber();
 144           return Messages.format( Messages.LINE_X_OF_Y,
 145               line==-1?"?":Integer.toString( line ),
 146               getShortName( e.getSystemId() ) );
 147       } else {
 148           return Messages.format( Messages.UNKNOWN_LOCATION );
 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 Messages.format( Messages.UNKNOWN_FILE );
 156 
 157 // sometimes the user deals with a set of schems that reference each other
 158 // in a complicated way, and end up importing two versions of the same schema.
 159 // just printing the file name makes it very difficult to recognize of this problem.
 160 // so I decided to change it back to print the full URL.
 161 
 162 //      int idx;
 163 //
 164 //      // system Id can be URL, so we can't use File.separator
 165 //      idx = url.lastIndexOf('/');
 166 //      if(idx!=-1)     return url.substring(idx+1);
 167 //      idx = url.lastIndexOf('\\');
 168 //      if(idx!=-1)     return url.substring(idx+1);
 169 
 170       return url;
 171   }
 172 }