Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JDOMException.java 12KB

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