KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > ScriptException


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit;
39
40 import java.io.BufferedReader JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.PrintStream JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44 import java.io.StringReader JavaDoc;
45 import java.io.StringWriter JavaDoc;
46 import org.mozilla.javascript.EcmaError;
47 import org.mozilla.javascript.JavaScriptException;
48 import org.mozilla.javascript.WrappedException;
49
50 /**
51  * An exception that will be thrown if an error occurs during the processing of
52  * a script.
53  *
54  * @version $Revision: 100 $
55  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
56  */

57 public class ScriptException extends RuntimeException JavaDoc {
58     private static final long serialVersionUID = 4788896649084231283L;
59     private final Throwable JavaDoc throwable_;
60     private final String JavaDoc scriptSourceCode_;
61
62
63     /**
64      * Create an instance
65      *
66      * @param throwable The exception that was thrown from the script engine.
67      * @param scriptSourceCode The code that was being executed when this exception
68      * was thrown. This may be null if the exception was not caused by execution
69      * of javascript.
70      */

71     public ScriptException( final Throwable JavaDoc throwable, final String JavaDoc scriptSourceCode ) {
72         super(getMessageFrom(throwable));
73         throwable_ = throwable;
74         scriptSourceCode_ = scriptSourceCode;
75     }
76
77     private static String JavaDoc getMessageFrom( final Throwable JavaDoc throwable ) {
78         if( throwable == null ) {
79             return "null";
80         }
81         else {
82             return throwable.getMessage();
83         }
84     }
85
86     /**
87      * Create an instance
88      *
89      * @param throwable The exception that was thrown from the script engine.
90      */

91     public ScriptException( final Throwable JavaDoc throwable ) {
92         this( throwable, null );
93     }
94
95
96     /**
97      * Return the enclosed exception.
98      * @return The enclosed exception
99      */

100     public Throwable JavaDoc getEnclosedException() {
101         return throwable_;
102     }
103
104     /**
105      * Print the stack trace to System.out. If this exception contains another
106      * exception then the stack traces for both will be printed.
107      */

108     public void printStackTrace() {
109         printStackTrace(System.out);
110     }
111
112     /**
113      * Print the stack trace. If this exception contains another exception then
114      * the stack traces for both will be printed.
115      *
116      * @param writer Where the stack trace will be written
117      */

118     public void printStackTrace( final PrintWriter JavaDoc writer ) {
119         writer.write( createPrintableStackTrace() );
120     }
121
122
123     /**
124      * Print the stack trace. If this exception contains another exception then
125      * the stack traces for both will be printed.
126      *
127      * @param stream Where the stack trace will be written
128      */

129     public void printStackTrace( final PrintStream JavaDoc stream ) {
130         stream.print(createPrintableStackTrace());
131     }
132
133
134     private String JavaDoc createPrintableStackTrace() {
135         final StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
136         final PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
137
138         printWriter.println("======= EXCEPTION START ========");
139
140         if( throwable_ != null ) {
141             if( throwable_ instanceof EcmaError ) {
142                 final EcmaError ecmaError = (EcmaError)throwable_;
143                 printWriter.print("EcmaError: ");
144                 printWriter.print("lineNumber=[");
145                 printWriter.print(ecmaError.lineNumber());
146                 printWriter.print("] column=[");
147                 printWriter.print(ecmaError.columnNumber());
148                 printWriter.print("] lineSource=[");
149                 printWriter.print(getFailingLine());
150                 printWriter.print("] name=[");
151                 printWriter.print(ecmaError.getName());
152                 printWriter.print("] sourceName=[");
153                 printWriter.print(ecmaError.sourceName());
154                 printWriter.print("] message=[");
155                 printWriter.print(ecmaError.getMessage());
156                 printWriter.print("]");
157                 printWriter.println();
158             }
159             else {
160                 printWriter.println("Exception class=["+throwable_.getClass().getName()+"]");
161             }
162         }
163
164         super.printStackTrace(printWriter);
165         if( throwable_ != null && throwable_ instanceof JavaScriptException ) {
166             final Object JavaDoc value = ((JavaScriptException)throwable_).getValue();
167
168             printWriter.print("JavaScriptException value = ");
169             if( value instanceof Throwable JavaDoc ) {
170                 ((Throwable JavaDoc)value).printStackTrace(printWriter);
171             }
172             else {
173                 printWriter.println(value);
174             }
175         }
176         else if( throwable_ != null && throwable_ instanceof WrappedException ) {
177             final WrappedException wrappedException = (WrappedException)throwable_;
178             printWriter.print("WrappedException: ");
179             wrappedException.printStackTrace(printWriter);
180
181             final Throwable JavaDoc innerException = wrappedException.getWrappedException();
182             if( innerException == null ) {
183                 printWriter.println("Inside wrapped exception: null");
184             }
185             else {
186                 printWriter.println("Inside wrapped exception:");
187                 innerException.printStackTrace(printWriter);
188             }
189         }
190         else if( throwable_ != null ) {
191             printWriter.println( "Enclosed exception: " );
192             throwable_.printStackTrace( printWriter );
193         }
194
195         if( scriptSourceCode_ != null && scriptSourceCode_.length() > 0 ) {
196             printWriter.println("== CALLING JAVASCRIPT ==");
197             printWriter.println(scriptSourceCode_);
198         }
199         printWriter.println("======= EXCEPTION END ========");
200
201         return stringWriter.toString();
202     }
203
204
205     /**
206      * Return the source code line that failed
207      * @return the source code line that failed
208      */

209     public String JavaDoc getScriptSourceCode() {
210         return scriptSourceCode_;
211     }
212
213
214     /**
215      * Return the line of source that was being executed when this exception was
216      * thrown.
217      *
218      * @return The line of source or an empty string if the exception was not thrown
219      * due to the execution of a script.
220      */

221     public String JavaDoc getFailingLine() {
222         final int lineNumber = getFailingLineNumber();
223         if( lineNumber == -1 ) {
224             return "<no source>";
225         }
226
227         try {
228             final BufferedReader JavaDoc reader = new BufferedReader JavaDoc( new StringReader JavaDoc(scriptSourceCode_) );
229             for( int i=0; i<lineNumber-1; i++ ) {
230                 reader.readLine();
231             }
232             final String JavaDoc result = reader.readLine();
233             reader.close();
234             return result;
235         }
236         catch( final IOException JavaDoc e ) {
237             // Theoretically impossible
238
e.printStackTrace();
239         }
240         return "";
241     }
242
243
244     /**
245      * Return the line number of the source that was executing at the time of the
246      * exception.
247      *
248      * @return The line number or -1 if the exception was not thrown due to the
249      * execution of a script.
250      */

251     public int getFailingLineNumber() {
252         if( scriptSourceCode_ == null ) {
253             return -1;
254         }
255
256         if( throwable_ instanceof EcmaError ) {
257             final EcmaError ecmaError = (EcmaError)throwable_;
258             return ecmaError.lineNumber();
259         }
260
261         return -1;
262     }
263 }
264
Popular Tags