Source Home >> Java Source 1.6.0 >> javax.xml.transform.TransformerException V 0.09
  • 001/*
  • 002 * The contents of this file are subject to the terms
  • 003 * of the Common Development and Distribution License
  • 004 * (the "License"). You may not use this file except
  • 005 * in compliance with the License.
  • 006 *
  • 007 * You can obtain a copy of the license at
  • 008 * https://jaxp.dev.java.net/CDDLv1.0.html.
  • 009 * See the License for the specific language governing
  • 010 * permissions and limitations under the License.
  • 011 *
  • 012 * When distributing Covered Code, include this CDDL
  • 013 * HEADER in each file and include the License file at
  • 014 * https://jaxp.dev.java.net/CDDLv1.0.html
  • 015 * If applicable add the following below this CDDL HEADER
  • 016 * with the fields enclosed by brackets "[]" replaced with
  • 017 * your own identifying information: Portions Copyright
  • 018 * [year] [name of copyright owner]
  • 019 */
  • 020
  • 021/*
  • 022 * $Id: TransformerException.java,v 1.5 2005/11/03 19:34:23 jeffsuttor Exp $
  • 023 * @(#)TransformerException.java 1.28 06/06/21
  • 024 *
  • 025 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
  • 026 */
  • 027
  • 028package javax.xml.transform;
  • 029
  • 030import java.lang.reflect.Method;
  • 031import java.lang.reflect.InvocationTargetException;
  • 032
  • 033/**
  • 034 * This class specifies an exceptional condition that occured
  • 035 * during the transformation process.
  • 036 */
  • 037public class TransformerException extends Exception {
  • 038
  • 039 /** Field locator specifies where the error occured */
  • 040 SourceLocator locator;
  • 041
  • 042 /**
  • 043 * Method getLocator retrieves an instance of a SourceLocator
  • 044 * object that specifies where an error occured.
  • 045 *
  • 046 * @return A SourceLocator object, or null if none was specified.
  • 047 */
  • 048 public SourceLocator getLocator() {
  • 049 return locator;
  • 050 }
  • 051
  • 052 /**
  • 053 * Method setLocator sets an instance of a SourceLocator
  • 054 * object that specifies where an error occured.
  • 055 *
  • 056 * @param location A SourceLocator object, or null to clear the location.
  • 057 */
  • 058 public void setLocator(SourceLocator location) {
  • 059 locator = location;
  • 060 }
  • 061
  • 062 /** Field containedException specifies a wrapped exception. May be null. */
  • 063 Throwable containedException;
  • 064
  • 065 /**
  • 066 * This method retrieves an exception that this exception wraps.
  • 067 *
  • 068 * @return An Throwable object, or null.
  • 069 * @see #getCause
  • 070 */
  • 071 public Throwable getException() {
  • 072 return containedException;
  • 073 }
  • 074
  • 075 /**
  • 076 * Returns the cause of this throwable or <code>null</code> if the
  • 077 * cause is nonexistent or unknown. (The cause is the throwable that
  • 078 * caused this throwable to get thrown.)
  • 079 */
  • 080 public Throwable getCause() {
  • 081
  • 082 return ((containedException == this)
  • 083 ? null
  • 084 : containedException);
  • 085 }
  • 086
  • 087 /**
  • 088 * Initializes the <i>cause</i> of this throwable to the specified value.
  • 089 * (The cause is the throwable that caused this throwable to get thrown.)
  • 090 *
  • 091 * <p>This method can be called at most once. It is generally called from
  • 092 * within the constructor, or immediately after creating the
  • 093 * throwable. If this throwable was created
  • 094 * with {@link #TransformerException(Throwable)} or
  • 095 * {@link #TransformerException(String,Throwable)}, this method cannot be called
  • 096 * even once.
  • 097 *
  • 098 * @param cause the cause (which is saved for later retrieval by the
  • 099 * {@link #getCause()} method). (A <code>null</code> value is
  • 100 * permitted, and indicates that the cause is nonexistent or
  • 101 * unknown.)
  • 102 * @return a reference to this <code>Throwable</code> instance.
  • 103 * @throws IllegalArgumentException if <code>cause</code> is this
  • 104 * throwable. (A throwable cannot
  • 105 * be its own cause.)
  • 106 * @throws IllegalStateException if this throwable was
  • 107 * created with {@link #TransformerException(Throwable)} or
  • 108 * {@link #TransformerException(String,Throwable)}, or this method has already
  • 109 * been called on this throwable.
  • 110 */
  • 111 public synchronized Throwable initCause(Throwable cause) {
  • 112
  • 113 if (this.containedException != null) {
  • 114 throw new IllegalStateException("Can't overwrite cause");
  • 115 }
  • 116
  • 117 if (cause == this) {
  • 118 throw new IllegalArgumentException(
  • 119 "Self-causation not permitted");
  • 120 }
  • 121
  • 122 this.containedException = cause;
  • 123
  • 124 return this;
  • 125 }
  • 126
  • 127 /**
  • 128 * Create a new TransformerException.
  • 129 *
  • 130 * @param message The error or warning message.
  • 131 */
  • 132 public TransformerException(String message) {
  • 133
  • 134 super(message);
  • 135
  • 136 this.containedException = null;
  • 137 this.locator = null;
  • 138 }
  • 139
  • 140 /**
  • 141 * Create a new TransformerException wrapping an existing exception.
  • 142 *
  • 143 * @param e The exception to be wrapped.
  • 144 */
  • 145 public TransformerException(Throwable e) {
  • 146
  • 147 super(e.toString());
  • 148
  • 149 this.containedException = e;
  • 150 this.locator = null;
  • 151 }
  • 152
  • 153 /**
  • 154 * Wrap an existing exception in a TransformerException.
  • 155 *
  • 156 * <p>This is used for throwing processor exceptions before
  • 157 * the processing has started.</p>
  • 158 *
  • 159 * @param message The error or warning message, or null to
  • 160 * use the message from the embedded exception.
  • 161 * @param e Any exception
  • 162 */
  • 163 public TransformerException(String message, Throwable e) {
  • 164
  • 165 super(((message == null) || (message.length() == 0))
  • 166 ? e.toString()
  • 167 : message);
  • 168
  • 169 this.containedException = e;
  • 170 this.locator = null;
  • 171 }
  • 172
  • 173 /**
  • 174 * Create a new TransformerException from a message and a Locator.
  • 175 *
  • 176 * <p>This constructor is especially useful when an application is
  • 177 * creating its own exception from within a DocumentHandler
  • 178 * callback.</p>
  • 179 *
  • 180 * @param message The error or warning message.
  • 181 * @param locator The locator object for the error or warning.
  • 182 */
  • 183 public TransformerException(String message, SourceLocator locator) {
  • 184
  • 185 super(message);
  • 186
  • 187 this.containedException = null;
  • 188 this.locator = locator;
  • 189 }
  • 190
  • 191 /**
  • 192 * Wrap an existing exception in a TransformerException.
  • 193 *
  • 194 * @param message The error or warning message, or null to
  • 195 * use the message from the embedded exception.
  • 196 * @param locator The locator object for the error or warning.
  • 197 * @param e Any exception
  • 198 */
  • 199 public TransformerException(String message, SourceLocator locator,
  • 200 Throwable e) {
  • 201
  • 202 super(message);
  • 203
  • 204 this.containedException = e;
  • 205 this.locator = locator;
  • 206 }
  • 207
  • 208 /**
  • 209 * Get the error message with location information
  • 210 * appended.
  • 211 *
  • 212 * @return A <code>String</code> representing the error message with
  • 213 * location information appended.
  • 214 */
  • 215 public String getMessageAndLocation() {
  • 216
  • 217 StringBuffer sbuffer = new StringBuffer();
  • 218 String message = super.getMessage();
  • 219
  • 220 if (null != message) {
  • 221 sbuffer.append(message);
  • 222 }
  • 223
  • 224 if (null != locator) {
  • 225 String systemID = locator.getSystemId();
  • 226 int line = locator.getLineNumber();
  • 227 int column = locator.getColumnNumber();
  • 228
  • 229 if (null != systemID) {
  • 230 sbuffer.append("; SystemID: ");
  • 231 sbuffer.append(systemID);
  • 232 }
  • 233
  • 234 if (0 != line) {
  • 235 sbuffer.append("; Line#: ");
  • 236 sbuffer.append(line);
  • 237 }
  • 238
  • 239 if (0 != column) {
  • 240 sbuffer.append("; Column#: ");
  • 241 sbuffer.append(column);
  • 242 }
  • 243 }
  • 244
  • 245 return sbuffer.toString();
  • 246 }
  • 247
  • 248 /**
  • 249 * Get the location information as a string.
  • 250 *
  • 251 * @return A string with location info, or null
  • 252 * if there is no location information.
  • 253 */
  • 254 public String getLocationAsString() {
  • 255
  • 256 if (null != locator) {
  • 257 StringBuffer sbuffer = new StringBuffer();
  • 258 String systemID = locator.getSystemId();
  • 259 int line = locator.getLineNumber();
  • 260 int column = locator.getColumnNumber();
  • 261
  • 262 if (null != systemID) {
  • 263 sbuffer.append("; SystemID: ");
  • 264 sbuffer.append(systemID);
  • 265 }
  • 266
  • 267 if (0 != line) {
  • 268 sbuffer.append("; Line#: ");
  • 269 sbuffer.append(line);
  • 270 }
  • 271
  • 272 if (0 != column) {
  • 273 sbuffer.append("; Column#: ");
  • 274 sbuffer.append(column);
  • 275 }
  • 276
  • 277 return sbuffer.toString();
  • 278 } else {
  • 279 return null;
  • 280 }
  • 281 }
  • 282
  • 283 /**
  • 284 * Print the the trace of methods from where the error
  • 285 * originated. This will trace all nested exception
  • 286 * objects, as well as this object.
  • 287 */
  • 288 public void printStackTrace() {
  • 289 printStackTrace(new java.io.PrintWriter(System.err, true));
  • 290 }
  • 291
  • 292 /**
  • 293 * Print the the trace of methods from where the error
  • 294 * originated. This will trace all nested exception
  • 295 * objects, as well as this object.
  • 296 * @param s The stream where the dump will be sent to.
  • 297 */
  • 298 public void printStackTrace(java.io.PrintStream s) {
  • 299 printStackTrace(new java.io.PrintWriter(s));
  • 300 }
  • 301
  • 302 /**
  • 303 * Print the the trace of methods from where the error
  • 304 * originated. This will trace all nested exception
  • 305 * objects, as well as this object.
  • 306 * @param s The writer where the dump will be sent to.
  • 307 */
  • 308 public void printStackTrace(java.io.PrintWriter s) {
  • 309
  • 310 if (s == null) {
  • 311 s = new java.io.PrintWriter(System.err, true);
  • 312 }
  • 313
  • 314 try {
  • 315 String locInfo = getLocationAsString();
  • 316
  • 317 if (null != locInfo) {
  • 318 s.println(locInfo);
  • 319 }
  • 320
  • 321 super.printStackTrace(s);
  • 322 } catch (Throwable e) {}
  • 323
  • 324 Throwable exception = getException();
  • 325
  • 326 for (int i = 0; (i < 10) && (null != exception); i++) {
  • 327 s.println("---------");
  • 328
  • 329 try {
  • 330 if (exception instanceof TransformerException) {
  • 331 String locInfo =
  • 332 ((TransformerException) exception)
  • 333 .getLocationAsString();
  • 334
  • 335 if (null != locInfo) {
  • 336 s.println(locInfo);
  • 337 }
  • 338 }
  • 339
  • 340 exception.printStackTrace(s);
  • 341 } catch (Throwable e) {
  • 342 s.println("Could not print stack trace...");
  • 343 }
  • 344
  • 345 try {
  • 346 Method meth =
  • 347 ((Object) exception).getClass().getMethod("getException",
  • 348 (Class[]) null);
  • 349
  • 350 if (null != meth) {
  • 351 Throwable prev = exception;
  • 352
  • 353 exception = (Throwable) meth.invoke(exception, (Object[]) null);
  • 354
  • 355 if (prev == exception) {
  • 356 break;
  • 357 }
  • 358 } else {
  • 359 exception = null;
  • 360 }
  • 361 } catch (InvocationTargetException ite) {
  • 362 exception = null;
  • 363 } catch (IllegalAccessException iae) {
  • 364 exception = null;
  • 365 } catch (NoSuchMethodException nsme) {
  • 366 exception = null;
  • 367 }
  • 368 }
  • 369 // insure output is written
  • 370 s.flush();
  • 371 }
  • 372}

文件:TransformerException.java
包名:javax.xml.transform
类名:TransformerException
继承:Exception
接口: