1 8 9 package javax.xml.transform; 10 11 import java.lang.reflect.Method ; 12 import java.lang.reflect.InvocationTargetException ; 13 14 18 public class TransformerException extends Exception { 19 20 21 SourceLocator locator; 22 23 29 public SourceLocator getLocator() { 30 return locator; 31 } 32 33 39 public void setLocator(SourceLocator location) { 40 locator = location; 41 } 42 43 44 Throwable containedException; 45 46 52 public Throwable getException() { 53 return containedException; 54 } 55 56 61 public Throwable getCause() { 62 63 return ((containedException == this) 64 ? null 65 : containedException); 66 } 67 68 92 public synchronized Throwable initCause(Throwable cause) { 93 94 if (this.containedException != null) { 95 throw new IllegalStateException ("Can't overwrite cause"); 96 } 97 98 if (cause == this) { 99 throw new IllegalArgumentException ( 100 "Self-causation not permitted"); 101 } 102 103 this.containedException = cause; 104 105 return this; 106 } 107 108 113 public TransformerException(String message) { 114 115 super(message); 116 117 this.containedException = null; 118 this.locator = null; 119 } 120 121 126 public TransformerException(Throwable e) { 127 128 super(e.toString()); 129 130 this.containedException = e; 131 this.locator = null; 132 } 133 134 144 public TransformerException(String message, Throwable e) { 145 146 super(((message == null) || (message.length() == 0)) 147 ? e.toString() 148 : message); 149 150 this.containedException = e; 151 this.locator = null; 152 } 153 154 164 public TransformerException(String message, SourceLocator locator) { 165 166 super(message); 167 168 this.containedException = null; 169 this.locator = locator; 170 } 171 172 180 public TransformerException(String message, SourceLocator locator, 181 Throwable e) { 182 183 super(message); 184 185 this.containedException = e; 186 this.locator = locator; 187 } 188 189 196 public String getMessageAndLocation() { 197 198 StringBuffer sbuffer = new StringBuffer (); 199 String message = super.getMessage(); 200 201 if (null != message) { 202 sbuffer.append(message); 203 } 204 205 if (null != locator) { 206 String systemID = locator.getSystemId(); 207 int line = locator.getLineNumber(); 208 int column = locator.getColumnNumber(); 209 210 if (null != systemID) { 211 sbuffer.append("; SystemID: "); 212 sbuffer.append(systemID); 213 } 214 215 if (0 != line) { 216 sbuffer.append("; Line#: "); 217 sbuffer.append(line); 218 } 219 220 if (0 != column) { 221 sbuffer.append("; Column#: "); 222 sbuffer.append(column); 223 } 224 } 225 226 return sbuffer.toString(); 227 } 228 229 235 public String getLocationAsString() { 236 237 if (null != locator) { 238 StringBuffer sbuffer = new StringBuffer (); 239 String systemID = locator.getSystemId(); 240 int line = locator.getLineNumber(); 241 int column = locator.getColumnNumber(); 242 243 if (null != systemID) { 244 sbuffer.append("; SystemID: "); 245 sbuffer.append(systemID); 246 } 247 248 if (0 != line) { 249 sbuffer.append("; Line#: "); 250 sbuffer.append(line); 251 } 252 253 if (0 != column) { 254 sbuffer.append("; Column#: "); 255 sbuffer.append(column); 256 } 257 258 return sbuffer.toString(); 259 } else { 260 return null; 261 } 262 } 263 264 269 public void printStackTrace() { 270 printStackTrace(new java.io.PrintWriter (System.err, true)); 271 } 272 273 279 public void printStackTrace(java.io.PrintStream s) { 280 printStackTrace(new java.io.PrintWriter (s)); 281 } 282 283 289 public void printStackTrace(java.io.PrintWriter s) { 290 291 if (s == null) { 292 s = new java.io.PrintWriter (System.err, true); 293 } 294 295 try { 296 String locInfo = getLocationAsString(); 297 298 if (null != locInfo) { 299 s.println(locInfo); 300 } 301 302 super.printStackTrace(s); 303 } catch (Throwable e) {} 304 305 Throwable exception = getException(); 306 307 for (int i = 0; (i < 10) && (null != exception); i++) { 308 s.println("---------"); 309 310 try { 311 if (exception instanceof TransformerException ) { 312 String locInfo = 313 ((TransformerException ) exception) 314 .getLocationAsString(); 315 316 if (null != locInfo) { 317 s.println(locInfo); 318 } 319 } 320 321 exception.printStackTrace(s); 322 } catch (Throwable e) { 323 s.println("Could not print stack trace..."); 324 } 325 326 try { 327 Method meth = 328 ((Object ) exception).getClass().getMethod("getException", 329 null); 330 331 if (null != meth) { 332 Throwable prev = exception; 333 334 exception = (Throwable ) meth.invoke(exception, null); 335 336 if (prev == exception) { 337 break; 338 } 339 } else { 340 exception = null; 341 } 342 } catch (InvocationTargetException ite) { 343 exception = null; 344 } catch (IllegalAccessException iae) { 345 exception = null; 346 } catch (NoSuchMethodException nsme) { 347 exception = null; 348 } 349 } 350 s.flush(); 352 } 353 } 354 | Popular Tags |