001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.wscompile;
038:
039: import com.sun.istack.Nullable;
040: import com.sun.istack.SAXParseException2;
041: import com.sun.tools.ws.resources.ModelMessages;
042: import com.sun.tools.xjc.api.ErrorListener;
043: import org.xml.sax.ErrorHandler;
044: import org.xml.sax.Locator;
045: import org.xml.sax.SAXParseException;
046:
047: /**
048: * Implemented by the driver of the compiler engine to handle
049: * errors found during the compiliation.
050: *
051: * <p>
052: * This class implements {@link org.xml.sax.ErrorHandler} so it can be
053: * passed to anywhere where {@link org.xml.sax.ErrorHandler} is expected.
054: *
055: * <p>
056: * However, to make the error handling easy (and make it work
057: * with visitor patterns nicely),
058: * none of the methods on thi class throws {@link org.xml.sax.SAXException}.
059: * Instead, when the compilation needs to be aborted,
060: * it throws {@link AbortException}, which is unchecked.
061: *
062: * <p>
063: * This also implements the externally visible {@link com.sun.tools.xjc.api.ErrorListener}
064: * so that we can reuse our internal implementation for testing and such.
065: *
066: * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
067: * @author Vivek Pandey
068: */
069: public abstract class ErrorReceiver implements ErrorHandler,
070: ErrorListener {
071:
072: //
073: //
074: // convenience methods for callers
075: //
076: //
077: /**
078: * @param loc
079: * can be null if the location is unknown
080: */
081: public final void error(Locator loc, String msg) {
082: error(new SAXParseException2(msg, loc));
083: }
084:
085: public final void error(Locator loc, String msg, Exception e) {
086: error(new SAXParseException2(msg, loc, e));
087: }
088:
089: public final void error(String msg, Exception e) {
090: error(new SAXParseException2(msg, null, e));
091: }
092:
093: public void error(Exception e) {
094: error(e.getMessage(), e);
095: }
096:
097: /**
098: * Reports a warning.
099: */
100: public final void warning(@Nullable
101: Locator loc, String msg) {
102: warning(new SAXParseException(msg, loc));
103: }
104:
105: //
106: //
107: // ErrorHandler implementation, but can't throw SAXException
108: //
109: //
110: public abstract void error(SAXParseException exception)
111: throws AbortException;
112:
113: public abstract void fatalError(SAXParseException exception)
114: throws AbortException;
115:
116: public abstract void warning(SAXParseException exception)
117: throws AbortException;
118:
119: /**
120: * This method will be invoked periodically to allow {@link com.sun.tools.xjc.AbortException}
121: * to be thrown, especially when this is driven by some kind of GUI.
122: */
123: public void pollAbort() throws AbortException {
124: }
125:
126: /**
127: * Reports verbose messages to users.
128: *
129: * This method can be used to report additional non-essential
130: * messages. The implementation usually discards them
131: * unless some specific debug option is turned on.
132: */
133: public abstract void info(SAXParseException exception) /*REVISIT:throws AbortException*/;
134:
135: /**
136: * Reports a debug message to users.
137: *
138: * @see #info(SAXParseException)
139: */
140: public final void debug(String msg) {
141: info(new SAXParseException(msg, null));
142: }
143:
144: //
145: //
146: // convenience methods for derived classes
147: //
148: //
149:
150: /**
151: * Returns the human readable string representation of the
152: * {@link org.xml.sax.Locator} part of the specified
153: * {@link SAXParseException}.
154: *
155: * @return non-null valid object.
156: */
157: protected final String getLocationString(SAXParseException e) {
158: if (e.getLineNumber() != -1 || e.getSystemId() != null) {
159: int line = e.getLineNumber();
160: return ModelMessages.CONSOLE_ERROR_REPORTER_LINE_X_OF_Y(
161: line == -1 ? "?" : Integer.toString(line),
162: getShortName(e.getSystemId()));
163: } else {
164: return ""; //for unkown location just return empty string
165: }
166: }
167:
168: /** Computes a short name of a given URL for display. */
169: private String getShortName(String url) {
170: if (url == null)
171: return ModelMessages
172: .CONSOLE_ERROR_REPORTER_UNKNOWN_LOCATION();
173: return url;
174: }
175: }
|