KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > util > IVException


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

50
51 package org.openlaszlo.iv.flash.util;
52
53 import java.util.*;
54 import java.io.*;
55
56 /**
57  * This exception is thrown if there any errors during generation process.
58  * <P>
59  * The exception can be created with message from resource bundles
60  * All messages in this file are defined by their keys which are to be used when throwing the exception.
61  * <p>
62  * Usage:<BR>
63  * <UL>
64  * <LI>wrap some other exception into IVException and rethrow it with a message from resorce and parameters:<br>
65  * <pre><CODE>
66  * ...
67  * } catch( IOException e ) {
68  * throw new IVException( e, Resource.ERRCMDFILEREAD, url.getName(), getCommandName() );
69  * }
70  * </CODE></pre>
71  * <LI><CODE>throw new IVException( Resource.INFINITELOOP );</CODE>
72  * </UL>
73  *
74  * @author Dmitry Skavish
75  */

76 public class IVException extends Exception JavaDoc {
77
78     private String JavaDoc message;
79     private Throwable JavaDoc cause;
80     private String JavaDoc key;
81     private Object JavaDoc[] parms;
82     private ResourceBundle bundle;
83
84     public IVException() {
85     }
86
87     public IVException( String JavaDoc key ) {
88         this.key = key;
89     }
90
91     public IVException( ResourceBundle bundle, String JavaDoc key ) {
92         this.key = key;
93         this.bundle = bundle;
94     }
95
96     public IVException( String JavaDoc key, Object JavaDoc[] parms ) {
97         this.key = key;
98         this.parms = parms;
99     }
100
101     public IVException( ResourceBundle bundle, String JavaDoc key, Object JavaDoc[] parms ) {
102         this.key = key;
103         this.parms = parms;
104         this.bundle = bundle;
105     }
106
107     public IVException( String JavaDoc key, Throwable JavaDoc cause ) {
108         this.key = key;
109         this.cause = cause;
110     }
111
112     public IVException( ResourceBundle bundle, String JavaDoc key, Throwable JavaDoc cause ) {
113         this.key = key;
114         this.cause = cause;
115         this.bundle = bundle;
116     }
117
118     public IVException( String JavaDoc key, Object JavaDoc[] parms, Throwable JavaDoc cause ) {
119         this.key = key;
120         this.parms = parms;
121         this.cause = cause;
122     }
123
124     public IVException( ResourceBundle bundle, String JavaDoc key, Object JavaDoc[] parms, Throwable JavaDoc cause ) {
125         this.key = key;
126         this.parms = parms;
127         this.cause = cause;
128         this.bundle = bundle;
129     }
130
131     public IVException( Throwable JavaDoc cause ) {
132         this.cause = cause;
133     }
134
135     public Throwable JavaDoc getCause() {
136         return cause;
137     }
138
139     public String JavaDoc getMessageKey() {
140         return key;
141     }
142
143     public String JavaDoc getMessage() {
144         if( message == null ) {
145             String JavaDoc ourMsg = bundle==null? Log.getMessage(key, parms): Log.getMessage(bundle, key, parms);
146             StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
147             if (ourMsg != null) {
148                 msg.append(ourMsg);
149             }
150             if (getCause() != null) {
151                 String JavaDoc causeMsg = getCause().getMessage();
152                 if (causeMsg != null) {
153                     if (ourMsg != null) {
154                         msg.append("\n\t-> ");
155                     }
156                     msg.append(causeMsg);
157                 }
158             }
159             message = msg.toString();
160         }
161         return message;
162     }
163
164     /**
165      * Prints this throwable and its backtrace to the specified print stream.
166      *
167      * @param s <code>PrintStream</code> to use for output
168      */

169     public void printStackTrace(PrintStream s) {
170         if( Util.javaVersion >= 1.4 ) super.printStackTrace(s);
171         else {
172             synchronized (s) {
173                 s.println(this);
174                 IVVector trace = getStackTrace(this);
175                 for (int i=0; i < trace.size(); i++)
176                     s.println(trace.elementAt(i));
177
178                 Throwable JavaDoc ourCause = getCause();
179                 if (ourCause != null)
180                     printStackTraceAsCause(ourCause, s, trace);
181             }
182         }
183     }
184
185     /**
186      * Print our stack trace as a cause for the specified stack trace.
187      */

188     private static void printStackTraceAsCause(Throwable JavaDoc t, PrintStream s, IVVector causedTrace) {
189
190         // Compute number of frames in common between this and caused
191
IVVector trace = getStackTrace(t);
192         int m = trace.size()-1, n = causedTrace.size()-1;
193         while (m >= 0 && n >=0 && trace.elementAt(m).equals(causedTrace.elementAt(n)) ) {
194             m--; n--;
195         }
196         int framesInCommon = trace.size() - 1 - m;
197
198         s.println("caused by: " + t);
199         for (int i=0; i <= m; i++)
200             s.println(trace.elementAt(i));
201         if (framesInCommon != 0)
202             s.println("\t... " + framesInCommon + " more");
203
204         // Recurse if we have a cause
205
if( t instanceof IVException ) {
206             Throwable JavaDoc ourCause = ((IVException)t).getCause();
207             if (ourCause != null)
208                 printStackTraceAsCause(ourCause, s, trace);
209         }
210     }
211
212     /**
213      * Prints this throwable and its backtrace to the specified
214      * print writer.
215      *
216      * @param s <code>PrintWriter</code> to use for output
217      * @since JDK1.1
218      */

219     public void printStackTrace(PrintWriter s) {
220         if( Util.javaVersion >= 1.4 ) super.printStackTrace(s);
221         else {
222             synchronized (s) {
223                 s.println(this);
224                 IVVector trace = getStackTrace(this);
225                 for (int i=0; i < trace.size(); i++)
226                     s.println(trace.elementAt(i));
227
228                 Throwable JavaDoc ourCause = getCause();
229                 if (ourCause != null)
230                     printStackTraceAsCause(ourCause, s, trace);
231             }
232         }
233     }
234
235     /**
236      * Print our stack trace as a cause for the specified stack trace.
237      */

238     private static void printStackTraceAsCause(Throwable JavaDoc t, PrintWriter s, IVVector causedTrace) {
239
240         // Compute number of frames in common between this and caused
241
IVVector trace = getStackTrace(t);
242         int m = trace.size()-1, n = causedTrace.size()-1;
243         while (m >= 0 && n >=0 && trace.elementAt(m).equals(causedTrace.elementAt(n)) ) {
244             m--; n--;
245         }
246         int framesInCommon = trace.size() - 1 - m;
247
248         s.println("caused by: " + t);
249         for (int i=0; i <= m; i++)
250             s.println(trace.elementAt(i));
251         if (framesInCommon != 0)
252             s.println("\t... " + framesInCommon + " more");
253
254         // Recurse if we have a cause
255
if( t instanceof IVException ) {
256             Throwable JavaDoc ourCause = ((IVException)t).getCause();
257             if (ourCause != null)
258                 printStackTraceAsCause(ourCause, s, trace);
259         }
260     }
261
262     private void super_printStackTrace( PrintWriter pw ) {
263         super.printStackTrace(pw);
264     }
265
266     private static IVVector getStackTrace( Throwable JavaDoc t ) {
267         StringWriter sw = new StringWriter();
268         PrintWriter pw = new PrintWriter(sw, true);
269
270         if( t instanceof IVException ) {
271             ((IVException)t).super_printStackTrace(pw);
272         } else {
273             t.printStackTrace(pw);
274         }
275
276         StringTokenizer st = new StringTokenizer(sw.getBuffer().toString(), Util.lineSeparator);
277         IVVector v = new IVVector(20);
278         while (st.hasMoreTokens()) {
279             String JavaDoc s = st.nextToken();
280             if( s.startsWith("\tat") )
281                 v.addElement(s);
282         }
283         return v;
284     }
285
286 }
287
Popular Tags