001: /*
002: $Id: ErrorReporter.java 2255 2005-06-08 20:06:19Z glaforge $
003:
004: Copyright 2004 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046:
047: package org.codehaus.groovy.tools;
048:
049: import java.io.PrintStream;
050: import java.io.PrintWriter;
051:
052: import org.codehaus.groovy.GroovyExceptionInterface;
053: import org.codehaus.groovy.control.CompilationFailedException;
054: import groovy.lang.GroovyRuntimeException;
055:
056: /**
057: * Provides services for reporting compilation errors to the
058: * user. Primary entry point is <code>write()</code>.
059: *
060: * @author <a href="mailto:cpoirier%20AT%20tapestry_os%20DOT%20org">Chris Poirier</a>
061: * @version $Revision: 2255 $
062: */
063:
064: public class ErrorReporter {
065: private Throwable base = null; // The exception on which to report
066: private boolean debug = false; // If true, stack traces are always output
067:
068: private Object output = null; // The stream/writer to which to output
069:
070: /**
071: * Configures a new Reporter. Default mode is not to report a stack trace unless
072: * the error was not of one of the supported types.
073: *
074: * @param e the exception on which to report
075: */
076:
077: public ErrorReporter(Throwable e) {
078: this .base = e;
079: }
080:
081: /**
082: * Configures a new Reporter.
083: *
084: * @param e the exception on which to report
085: * @param debug if set, stack traces will be output for all reports
086: */
087:
088: public ErrorReporter(Throwable e, boolean debug) {
089: this .base = e;
090: this .debug = debug;
091: }
092:
093: /**
094: * Writes the error to the specified <code>PrintStream</code>.
095: */
096:
097: public void write(PrintStream stream) {
098: this .output = stream;
099: dispatch(base, false);
100: stream.flush();
101: }
102:
103: /**
104: * Writes the error to the specified <code>PrintWriter</code>.
105: */
106:
107: public void write(PrintWriter writer) {
108: this .output = writer;
109: dispatch(base, false);
110: writer.flush();
111: }
112:
113: /**
114: * Runs the report once all initialization is complete.
115: */
116:
117: protected void dispatch(Throwable object, boolean child) {
118: if (object instanceof CompilationFailedException) {
119: report((CompilationFailedException) object, child);
120: } else if (object instanceof GroovyExceptionInterface) {
121: report((GroovyExceptionInterface) object, child);
122: } else if (object instanceof GroovyRuntimeException) {
123: report((GroovyRuntimeException) object, child);
124: } else if (object instanceof Exception) {
125: report((Exception) object, child);
126: } else {
127: report(object, child);
128: }
129:
130: }
131:
132: //---------------------------------------------------------------------------
133: // REPORTING ROUTINES
134:
135: /**
136: * For CompilationFailedException.
137: */
138:
139: protected void report(CompilationFailedException e, boolean child) {
140: println(e.toString());
141: stacktrace(e, false);
142: }
143:
144: /**
145: * For GroovyException.
146: */
147:
148: protected void report(GroovyExceptionInterface e, boolean child) {
149: println(((Exception) e).getMessage());
150: stacktrace((Exception) e, false);
151: }
152:
153: /**
154: * For Exception.
155: */
156:
157: protected void report(Exception e, boolean child) {
158: println(e.getMessage());
159: stacktrace(e, false);
160: }
161:
162: /**
163: * For everything else.
164: */
165:
166: protected void report(Throwable e, boolean child) {
167: println(">>> a serious error occurred: " + e.getMessage());
168: stacktrace(e, true);
169: }
170:
171: //---------------------------------------------------------------------------
172: // GENERAL SUPPORT ROUTINES
173:
174: /**
175: * Prints a line to the underlying <code>PrintStream</code>
176: */
177:
178: protected void println(String line) {
179: if (output instanceof PrintStream) {
180: ((PrintStream) output).println(line);
181: } else {
182: ((PrintWriter) output).println(line);
183: }
184: }
185:
186: protected void println(StringBuffer line) {
187: if (output instanceof PrintStream) {
188: ((PrintStream) output).println(line);
189: } else {
190: ((PrintWriter) output).println(line);
191: }
192: }
193:
194: /**
195: * Displays an exception's stack trace, if <code>debug</code> or
196: * <code>always</code>.
197: */
198:
199: protected void stacktrace(Throwable e, boolean always) {
200: if (debug || always) {
201: println(">>> stacktrace:");
202: if (output instanceof PrintStream) {
203: e.printStackTrace((PrintStream) output);
204: } else {
205: e.printStackTrace((PrintWriter) output);
206: }
207: }
208: }
209:
210: }
|