annotate NGSrich_0.5.5/src/org/jdom/JDOMException.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
1 /*--
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
2
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
3 $Id: JDOMException.java,v 1.26 2008/12/10 00:59:51 jhunter Exp $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
4
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
5 Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
6 All rights reserved.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
7
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
8 Redistribution and use in source and binary forms, with or without
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
9 modification, are permitted provided that the following conditions
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
10 are met:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
11
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
12 1. Redistributions of source code must retain the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
13 notice, this list of conditions, and the following disclaimer.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
14
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
15 2. Redistributions in binary form must reproduce the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
16 notice, this list of conditions, and the disclaimer that follows
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
17 these conditions in the documentation and/or other materials
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
18 provided with the distribution.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
19
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
20 3. The name "JDOM" must not be used to endorse or promote products
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
21 derived from this software without prior written permission. For
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
22 written permission, please contact <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
23
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
24 4. Products derived from this software may not be called "JDOM", nor
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
25 may "JDOM" appear in their name, without prior written permission
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
26 from the JDOM Project Management <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
27
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
28 In addition, we request (but do not require) that you include in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
29 end-user documentation provided with the redistribution and/or in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
30 software itself an acknowledgement equivalent to the following:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
31 "This product includes software developed by the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
32 JDOM Project (http://www.jdom.org/)."
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
33 Alternatively, the acknowledgment may be graphical using the logos
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
34 available at http://www.jdom.org/images/logos.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
35
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
47 SUCH DAMAGE.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
48
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
49 This software consists of voluntary contributions made by many
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
50 individuals on behalf of the JDOM Project and was originally
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
53 on the JDOM Project, please see <http://www.jdom.org/>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
54
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
55 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
56
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
57 package org.jdom;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 import java.io.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60 import java.lang.reflect.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61 import java.sql.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63 import org.xml.sax.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 * The top level exception that JDOM classes can throw. Its subclasses add
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67 * specificity to the problems that can occur using JDOM. This single exception
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 * can be caught to handle all JDOM specific problems (some methods may throw
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 * {@link java.io.IOException} and such).
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71 * @version $Revision: 1.26 $, $Date: 2008/12/10 00:59:51 $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 * @author Brett McLaughlin
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73 * @author Jason Hunter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 public class JDOMException extends Exception {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 private static final String CVS_ID =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78 "@(#) $RCSfile: JDOMException.java,v $ $Revision: 1.26 $ $Date: 2008/12/10 00:59:51 $ $Name: jdom_1_1_1 $";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 /** A wrapped <code>Throwable</code> */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81 private Throwable cause;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84 * This will create an <code>Exception</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86 public JDOMException() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 super("Error occurred in JDOM application.");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91 * This will create an <code>Exception</code> with the given message.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 * @param message <code>String</code> message indicating
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 * the problem that occurred.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96 public JDOMException(String message) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 super(message);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101 * This will create an <code>Exception</code> with the given message
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102 * and wrap another <code>Exception</code>. This is useful when
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 * the originating <code>Exception</code> should be held on to.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105 * @param message <code>String</code> message indicating
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 * the problem that occurred.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 * @param cause <code>Throwable</code> that caused this
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108 * to be thrown.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110 public JDOMException(String message, Throwable cause) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 super(message);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112 this.cause = cause;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 * Intializes the cause of this exception to be the specified value.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118 * @param cause <code>Throwable</code> that caused this
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119 * to be thrown.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 * @return a pointer to this throwable
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 // Created to match the JDK 1.4 Throwable method.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 public Throwable initCause(Throwable cause) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 this.cause = cause;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 return this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 * This returns the message for the <code>Exception</code>. If
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130 * there are one or more nested exceptions, their messages
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 * are appended.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 * @return <code>String</code> - message for <code>Exception</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 public String getMessage() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136 // Get this exception's message.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 String msg = super.getMessage();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 Throwable parent = this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 Throwable child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142 // Look for nested exceptions.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 while((child = getNestedException(parent)) != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 // Get the child's message.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 String msg2 = child.getMessage();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 // Special case: If a SAXException has no message of its own, but has a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148 // nested exception, then it returns the nested exception's message as its
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149 // message. We don't want to add that message twice.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150 if (child instanceof SAXException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 Throwable grandchild = ((SAXException)child).getException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 // If the SAXException tells us that it's message is identical to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153 // its nested exception's message, then we skip it, so we don't
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 // add it twice.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 if (grandchild != null && msg2 != null && msg2.equals(grandchild.getMessage())) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156 msg2 = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 // If we found a message for the child exception, we append it.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 if (msg2 != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162 if (msg != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 msg += ": " + msg2;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 } else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165 msg = msg2;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 // Any nested JDOMException will append its own children,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 // so we need to break out of here.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171 if (child instanceof JDOMException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 parent = child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177 // Return the completed message.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178 return msg;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182 * This prints the stack trace of the <code>Exception</code>. If
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183 * there is a root cause, the stack trace of the root
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 * <code>Exception</code> is printed right after.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186 public void printStackTrace() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 // Print the stack trace for this exception.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
188 super.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
189
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
190 Throwable parent = this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
191 Throwable child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
192
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
193 // Print the stack trace for each nested exception.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
194 while((child = getNestedException(parent)) != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
195 System.err.print("Caused by: ");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
196 child.printStackTrace();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
197 // Any nested JDOMException will print its own children,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
198 // so we need to break out of here.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
199 if (child instanceof JDOMException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
200 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
201 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
202 parent = child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
203 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
204 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
205
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
206 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
207 * Prints the stack trace of the <code>Exception</code> to the given
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
208 * PrintStream. If there is a root cause, the stack trace of the root
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
209 * <code>Exception</code> is printed right after.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
210 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
211 * @param s PrintStream to print to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
212 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
213 public void printStackTrace(PrintStream s) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
214 // Print the stack trace for this exception.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
215 super.printStackTrace(s);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
216
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
217 Throwable parent = this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
218 Throwable child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
219
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
220 // Print the stack trace for each nested exception.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
221 while((child = getNestedException(parent)) != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
222 s.print("Caused by: ");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
223 child.printStackTrace(s);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
224 // Any nested JDOMException will print its own children,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
225 // so we need to break out of here.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
226 if (child instanceof JDOMException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
227 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
228 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
229 parent = child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
230 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
231 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
232
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
233 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
234 * Prints the stack trace of the <code>Exception</code> to the given
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
235 * PrintWriter. If there is a root cause, the stack trace of the root
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
236 * <code>Exception</code> is printed right after.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
237 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
238 * @param w PrintWriter to print to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
239 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
240 public void printStackTrace(PrintWriter w) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
241 // Print the stack trace for this exception.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
242 super.printStackTrace(w);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
243
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
244 Throwable parent = this;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
245 Throwable child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
246
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
247 // Print the stack trace for each nested exception.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
248 while((child = getNestedException(parent)) != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
249 w.print("Caused by: ");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
250 child.printStackTrace(w);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
251 // Any nested JDOMException will print its own children,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
252 // so we need to break out of here.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
253 if (child instanceof JDOMException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
254 break;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
255 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
256 parent = child;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
257 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
258 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
259
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
260 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
261 * This will return the root cause <code>Throwable</code>, or null
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
262 * if one does not exist.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
263 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
264 * @return <code>Throwable</code> - the wrapped <code>Throwable</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
265 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
266 public Throwable getCause() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
267 return cause;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
268 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
269
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
270 // If this Throwable has a nested (child) exception, then we return it.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
271 // Otherwise we return null.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
272 private static Throwable getNestedException(Throwable parent) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
273 if (parent instanceof JDOMException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
274 return ((JDOMException)parent).getCause();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
275 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
276
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
277 if (parent instanceof SAXException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
278 return ((SAXException)parent).getException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
279 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
280
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
281 if (parent instanceof SQLException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
282 return ((SQLException)parent).getNextException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
283 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
284
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
285 if (parent instanceof InvocationTargetException) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
286 return ((InvocationTargetException)parent).getTargetException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
287 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
288
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
289 if (parent instanceof ExceptionInInitializerError) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
290 return ((ExceptionInInitializerError)parent).getException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
291 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
292
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
293 // The RMI classes are not present in Android's Dalvik VM, so we use reflection to access them.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
294
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
295 Throwable nestedException = getNestedExceptionFromField(parent, "java.rmi.RemoteException", "detail");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
296 if (nestedException != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
297 return nestedException;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
298 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
299
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
300 // These classes are not part of standard JDK 1.1 or 1.2, so again we use reflection to access them.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
301
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
302 nestedException = getNestedException(parent, "javax.naming.NamingException", "getRootCause");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
303 if (nestedException != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
304 return nestedException;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
305 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
306
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
307 nestedException = getNestedException(parent, "javax.servlet.ServletException", "getRootCause");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
308 if (nestedException != null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
309 return nestedException;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
310 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
311
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
312 return null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
313 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
314
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
315 // This method uses reflection to obtain the nest exception of a Throwable. We use reflection
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
316 // because the desired class may not exist in the currently-running VM.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
317 private static Throwable getNestedException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
318 Throwable parent, String className, String methodName) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
319 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
320 // See if this Throwable is of the desired type, by using isAssignableFrom().
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
321 Class testClass = Class.forName(className);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
322 Class objectClass = parent.getClass();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
323 if (testClass.isAssignableFrom(objectClass)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
324 // Use reflection to call the specified method.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
325 Class[] argClasses = new Class[0];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
326 Method method = testClass.getMethod(methodName, argClasses);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
327 Object[] args = new Object[0];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
328 return (Throwable)method.invoke(parent, args);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
329 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
330 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
331 catch(Exception ex) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
332 // Most likely, the desired class is not available in this VM. That's fine.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
333 // Even if it's caused by something else, we don't want to display an error
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
334 // here, since we're already in the process of trying to display the original
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
335 // error - another error here will just confuse things.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
336 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
337
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
338 return null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
339 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
340
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
341 // This method is similar to getNestedException() except it looks for a field instead
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
342 // of a method.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
343 private static Throwable getNestedExceptionFromField(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
344 Throwable parent, String className, String fieldName) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
345 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
346 // See if this Throwable is of the desired type, by using isAssignableFrom().
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
347 Class testClass = Class.forName(className);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
348 Class objectClass = parent.getClass();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
349 if (testClass.isAssignableFrom(objectClass)) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
350 // Use reflection to call the specified method.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
351 Class[] argClasses = new Class[0];
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
352 Field field = testClass.getField(fieldName);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
353 return (Throwable)field.get(parent);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
354 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
355 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
356 catch(Exception ex) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
357 // Most likely, the desired class is not available in this VM. That's fine.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
358 // Could be that the named field isn't of type Throwable, but that should happen
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
359 // with proper call usage.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
360 // Even if it's caused by something else, we don't want to display an error
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
361 // here, since we're already in the process of trying to display the original
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
362 // error - another error here will just confuse things.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
363 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
364
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
365 return null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
366 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
367 }