1 56 57 package org.jdom; 58 59 import java.io.*; 60 import java.lang.reflect.*; 61 import java.rmi.*; 62 import java.sql.*; 63 64 import org.xml.sax.*; 65 66 76 public class JDOMException extends Exception { 77 78 private static final String CVS_ID = 79 "@(#) $RCSfile: JDOMException.java,v $ $Revision: 1.23 $ $Date: 2004/02/27 11:32:57 $ $Name: $"; 80 81 82 private Throwable cause; 83 84 87 public JDOMException() { 88 super("Error occurred in JDOM application."); 89 } 90 91 97 public JDOMException(String message) { 98 super(message); 99 } 100 101 111 public JDOMException(String message, Throwable cause) { 112 super(message); 113 this.cause = cause; 114 } 115 116 123 public Throwable initCause(Throwable cause) { 125 this.cause = cause; 126 return this; 127 } 128 129 136 public String getMessage() { 137 String msg = super.getMessage(); 139 140 Throwable parent = this; 141 Throwable child; 142 143 while((child = getNestedException(parent)) != null) { 145 String msg2 = child.getMessage(); 147 148 if (child instanceof SAXException) { 152 Throwable grandchild = ((SAXException)child).getException(); 153 if (grandchild != null && msg2 != null && msg2.equals(grandchild.getMessage())) { 157 msg2 = null; 158 } 159 } 160 161 if (msg2 != null) { 163 if (msg != null) { 164 msg += ": " + msg2; 165 } else { 166 msg = msg2; 167 } 168 } 169 170 if (child instanceof JDOMException) { 173 break; 174 } 175 parent = child; 176 } 177 178 return msg; 180 } 181 182 187 public void printStackTrace() { 188 super.printStackTrace(); 190 191 Throwable parent = this; 192 Throwable child; 193 194 while((child = getNestedException(parent)) != null) { 196 System.err.print("Caused by: "); 197 child.printStackTrace(); 198 if (child instanceof JDOMException) { 201 break; 202 } 203 parent = child; 204 } 205 } 206 207 214 public void printStackTrace(PrintStream s) { 215 super.printStackTrace(s); 217 218 Throwable parent = this; 219 Throwable child; 220 221 while((child = getNestedException(parent)) != null) { 223 s.print("Caused by: "); 224 child.printStackTrace(s); 225 if (child instanceof JDOMException) { 228 break; 229 } 230 parent = child; 231 } 232 } 233 234 241 public void printStackTrace(PrintWriter w) { 242 super.printStackTrace(w); 244 245 Throwable parent = this; 246 Throwable child; 247 248 while((child = getNestedException(parent)) != null) { 250 w.print("Caused by: "); 251 child.printStackTrace(w); 252 if (child instanceof JDOMException) { 255 break; 256 } 257 parent = child; 258 } 259 } 260 261 267 public Throwable getCause() { 268 return cause; 269 } 270 271 private static Throwable getNestedException(Throwable parent) { 274 if (parent instanceof JDOMException) { 275 return ((JDOMException)parent).getCause(); 276 } 277 278 if (parent instanceof SAXException) { 279 return ((SAXException)parent).getException(); 280 } 281 282 if (parent instanceof SQLException) { 283 return ((SQLException)parent).getNextException(); 284 } 285 286 if (parent instanceof InvocationTargetException) { 287 return ((InvocationTargetException)parent).getTargetException(); 288 } 289 290 if (parent instanceof ExceptionInInitializerError ) { 291 return ((ExceptionInInitializerError )parent).getException(); 292 } 293 294 if (parent instanceof RemoteException) { 295 return ((RemoteException)parent).detail; 296 } 297 298 301 Throwable nestedException = getNestedException(parent, "javax.naming.NamingException", "getRootCause"); 302 if (nestedException != null) { 303 return nestedException; 304 } 305 306 nestedException = getNestedException(parent, "javax.servlet.ServletException", "getRootCause"); 307 if (nestedException != null) { 308 return nestedException; 309 } 310 311 return null; 312 } 313 314 private static Throwable getNestedException( 317 Throwable parent, String className, String methodName) { 318 try { 319 Class testClass = Class.forName(className); 321 Class objectClass = parent.getClass(); 322 if (testClass.isAssignableFrom(objectClass)) { 323 Class [] argClasses = new Class [0]; 325 Method method = testClass.getMethod(methodName, argClasses); 326 Object [] args = new Object [0]; 327 return (Throwable )method.invoke(parent, args); 328 } 329 } 330 catch(Exception ex) { 331 } 336 337 return null; 338 } 339 } 340 | Popular Tags |