Source Home >> Java Source 1.6.0 >> com.sun.org.apache.xml.internal.dtm.DTMException V 0.09
  • 001/*
  • 002 * Copyright 1999-2004 The Apache Software Foundation.
  • 003 *
  • 004 * Licensed under the Apache License, Version 2.0 (the "License");
  • 005 * you may not use this file except in compliance with the License.
  • 006 * You may obtain a copy of the License at
  • 007 *
  • 008 * http://www.apache.org/licenses/LICENSE-2.0
  • 009 *
  • 010 * Unless required by applicable law or agreed to in writing, software
  • 011 * distributed under the License is distributed on an "AS IS" BASIS,
  • 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • 013 * See the License for the specific language governing permissions and
  • 014 * limitations under the License.
  • 015 */
  • 016/*
  • 017 * $Id: DTMException.java,v 1.3 2005/09/28 13:48:50 pvedula Exp $
  • 018 */
  • 019package com.sun.org.apache.xml.internal.dtm;
  • 020
  • 021
  • 022import java.lang.reflect.InvocationTargetException;
  • 023import java.lang.reflect.Method;
  • 024
  • 025import javax.xml.transform.SourceLocator;
  • 026
  • 027import com.sun.org.apache.xml.internal.res.XMLErrorResources;
  • 028import com.sun.org.apache.xml.internal.res.XMLMessages;
  • 029
  • 030
  • 031/**
  • 032 * This class specifies an exceptional condition that occured
  • 033 * in the DTM module.
  • 034 */
  • 035public class DTMException extends RuntimeException {
  • 036 static final long serialVersionUID = -775576419181334734L;
  • 037
  • 038 /** Field locator specifies where the error occured.
  • 039 * @serial */
  • 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 * @serial */
  • 064 Throwable containedException;
  • 065
  • 066 /**
  • 067 * This method retrieves an exception that this exception wraps.
  • 068 *
  • 069 * @return An Throwable object, or null.
  • 070 * @see #getCause
  • 071 */
  • 072 public Throwable getException() {
  • 073 return containedException;
  • 074 }
  • 075
  • 076 /**
  • 077 * Returns the cause of this throwable or <code>null</code> if the
  • 078 * cause is nonexistent or unknown. (The cause is the throwable that
  • 079 * caused this throwable to get thrown.)
  • 080 */
  • 081 public Throwable getCause() {
  • 082
  • 083 return ((containedException == this)
  • 084 ? null
  • 085 : containedException);
  • 086 }
  • 087
  • 088 /**
  • 089 * Initializes the <i>cause</i> of this throwable to the specified value.
  • 090 * (The cause is the throwable that caused this throwable to get thrown.)
  • 091 *
  • 092 * <p>This method can be called at most once. It is generally called from
  • 093 * within the constructor, or immediately after creating the
  • 094 * throwable. If this throwable was created
  • 095 * with {@link #DTMException(Throwable)} or
  • 096 * {@link #DTMException(String,Throwable)}, this method cannot be called
  • 097 * even once.
  • 098 *
  • 099 * @param cause the cause (which is saved for later retrieval by the
  • 100 * {@link #getCause()} method). (A <tt>null</tt> value is
  • 101 * permitted, and indicates that the cause is nonexistent or
  • 102 * unknown.)
  • 103 * @return a reference to this <code>Throwable</code> instance.
  • 104 * @throws IllegalArgumentException if <code>cause</code> is this
  • 105 * throwable. (A throwable cannot
  • 106 * be its own cause.)
  • 107 * @throws IllegalStateException if this throwable was
  • 108 * created with {@link #DTMException(Throwable)} or
  • 109 * {@link #DTMException(String,Throwable)}, or this method has already
  • 110 * been called on this throwable.
  • 111 */
  • 112 public synchronized Throwable initCause(Throwable cause) {
  • 113
  • 114 if ((this.containedException == null) && (cause != null)) {
  • 115 throw new IllegalStateException(XMLMessages.createXMLMessage(XMLErrorResources.ER_CANNOT_OVERWRITE_CAUSE, null)); //"Can't overwrite cause");
  • 116 }
  • 117
  • 118 if (cause == this) {
  • 119 throw new IllegalArgumentException(
  • 120 XMLMessages.createXMLMessage(XMLErrorResources.ER_SELF_CAUSATION_NOT_PERMITTED, null)); //"Self-causation not permitted");
  • 121 }
  • 122
  • 123 this.containedException = cause;
  • 124
  • 125 return this;
  • 126 }
  • 127
  • 128 /**
  • 129 * Create a new DTMException.
  • 130 *
  • 131 * @param message The error or warning message.
  • 132 */
  • 133 public DTMException(String message) {
  • 134
  • 135 super(message);
  • 136
  • 137 this.containedException = null;
  • 138 this.locator = null;
  • 139 }
  • 140
  • 141 /**
  • 142 * Create a new DTMException wrapping an existing exception.
  • 143 *
  • 144 * @param e The exception to be wrapped.
  • 145 */
  • 146 public DTMException(Throwable e) {
  • 147
  • 148 super(e.getMessage());
  • 149
  • 150 this.containedException = e;
  • 151 this.locator = null;
  • 152 }
  • 153
  • 154 /**
  • 155 * Wrap an existing exception in a DTMException.
  • 156 *
  • 157 * <p>This is used for throwing processor exceptions before
  • 158 * the processing has started.</p>
  • 159 *
  • 160 * @param message The error or warning message, or null to
  • 161 * use the message from the embedded exception.
  • 162 * @param e Any exception
  • 163 */
  • 164 public DTMException(String message, Throwable e) {
  • 165
  • 166 super(((message == null) || (message.length() == 0))
  • 167 ? e.getMessage()
  • 168 : message);
  • 169
  • 170 this.containedException = e;
  • 171 this.locator = null;
  • 172 }
  • 173
  • 174 /**
  • 175 * Create a new DTMException from a message and a Locator.
  • 176 *
  • 177 * <p>This constructor is especially useful when an application is
  • 178 * creating its own exception from within a DocumentHandler
  • 179 * callback.</p>
  • 180 *
  • 181 * @param message The error or warning message.
  • 182 * @param locator The locator object for the error or warning.
  • 183 */
  • 184 public DTMException(String message, SourceLocator locator) {
  • 185
  • 186 super(message);
  • 187
  • 188 this.containedException = null;
  • 189 this.locator = locator;
  • 190 }
  • 191
  • 192 /**
  • 193 * Wrap an existing exception in a DTMException.
  • 194 *
  • 195 * @param message The error or warning message, or null to
  • 196 * use the message from the embedded exception.
  • 197 * @param locator The locator object for the error or warning.
  • 198 * @param e Any exception
  • 199 */
  • 200 public DTMException(String message, SourceLocator locator,
  • 201 Throwable e) {
  • 202
  • 203 super(message);
  • 204
  • 205 this.containedException = e;
  • 206 this.locator = locator;
  • 207 }
  • 208
  • 209 /**
  • 210 * Get the error message with location information
  • 211 * appended.
  • 212 */
  • 213 public String getMessageAndLocation() {
  • 214
  • 215 StringBuffer sbuffer = new StringBuffer();
  • 216 String message = super.getMessage();
  • 217
  • 218 if (null != message) {
  • 219 sbuffer.append(message);
  • 220 }
  • 221
  • 222 if (null != locator) {
  • 223 String systemID = locator.getSystemId();
  • 224 int line = locator.getLineNumber();
  • 225 int column = locator.getColumnNumber();
  • 226
  • 227 if (null != systemID) {
  • 228 sbuffer.append("; SystemID: ");
  • 229 sbuffer.append(systemID);
  • 230 }
  • 231
  • 232 if (0 != line) {
  • 233 sbuffer.append("; Line#: ");
  • 234 sbuffer.append(line);
  • 235 }
  • 236
  • 237 if (0 != column) {
  • 238 sbuffer.append("; Column#: ");
  • 239 sbuffer.append(column);
  • 240 }
  • 241 }
  • 242
  • 243 return sbuffer.toString();
  • 244 }
  • 245
  • 246 /**
  • 247 * Get the location information as a string.
  • 248 *
  • 249 * @return A string with location info, or null
  • 250 * if there is no location information.
  • 251 */
  • 252 public String getLocationAsString() {
  • 253
  • 254 if (null != locator) {
  • 255 StringBuffer sbuffer = new StringBuffer();
  • 256 String systemID = locator.getSystemId();
  • 257 int line = locator.getLineNumber();
  • 258 int column = locator.getColumnNumber();
  • 259
  • 260 if (null != systemID) {
  • 261 sbuffer.append("; SystemID: ");
  • 262 sbuffer.append(systemID);
  • 263 }
  • 264
  • 265 if (0 != line) {
  • 266 sbuffer.append("; Line#: ");
  • 267 sbuffer.append(line);
  • 268 }
  • 269
  • 270 if (0 != column) {
  • 271 sbuffer.append("; Column#: ");
  • 272 sbuffer.append(column);
  • 273 }
  • 274
  • 275 return sbuffer.toString();
  • 276 } else {
  • 277 return null;
  • 278 }
  • 279 }
  • 280
  • 281 /**
  • 282 * Print the the trace of methods from where the error
  • 283 * originated. This will trace all nested exception
  • 284 * objects, as well as this object.
  • 285 */
  • 286 public void printStackTrace() {
  • 287 printStackTrace(new java.io.PrintWriter(System.err, true));
  • 288 }
  • 289
  • 290 /**
  • 291 * Print the the trace of methods from where the error
  • 292 * originated. This will trace all nested exception
  • 293 * objects, as well as this object.
  • 294 * @param s The stream where the dump will be sent to.
  • 295 */
  • 296 public void printStackTrace(java.io.PrintStream s) {
  • 297 printStackTrace(new java.io.PrintWriter(s));
  • 298 }
  • 299
  • 300 /**
  • 301 * Print the the trace of methods from where the error
  • 302 * originated. This will trace all nested exception
  • 303 * objects, as well as this object.
  • 304 * @param s The writer where the dump will be sent to.
  • 305 */
  • 306 public void printStackTrace(java.io.PrintWriter s) {
  • 307
  • 308 if (s == null) {
  • 309 s = new java.io.PrintWriter(System.err, true);
  • 310 }
  • 311
  • 312 try {
  • 313 String locInfo = getLocationAsString();
  • 314
  • 315 if (null != locInfo) {
  • 316 s.println(locInfo);
  • 317 }
  • 318
  • 319 super.printStackTrace(s);
  • 320 } catch (Throwable e) {}
  • 321
  • 322 boolean isJdk14OrHigher = false;
  • 323 try {
  • 324 Throwable.class.getMethod("getCause", (Class[]) null);
  • 325 isJdk14OrHigher = true;
  • 326 } catch (NoSuchMethodException nsme) {
  • 327 // do nothing
  • 328 }
  • 329
  • 330 // The printStackTrace method of the Throwable class in jdk 1.4
  • 331 // and higher will include the cause when printing the backtrace.
  • 332 // The following code is only required when using jdk 1.3 or lower
  • 333 if (!isJdk14OrHigher) {
  • 334 Throwable exception = getException();
  • 335
  • 336 for (int i = 0; (i < 10) && (null != exception); i++) {
  • 337 s.println("---------");
  • 338
  • 339 try {
  • 340 if (exception instanceof DTMException) {
  • 341 String locInfo =
  • 342 ((DTMException) exception)
  • 343 .getLocationAsString();
  • 344
  • 345 if (null != locInfo) {
  • 346 s.println(locInfo);
  • 347 }
  • 348 }
  • 349
  • 350 exception.printStackTrace(s);
  • 351 } catch (Throwable e) {
  • 352 s.println("Could not print stack trace...");
  • 353 }
  • 354
  • 355 try {
  • 356 Method meth =
  • 357 ((Object) exception).getClass().getMethod("getException",
  • 358 (Class[]) null);
  • 359
  • 360 if (null != meth) {
  • 361 Throwable prev = exception;
  • 362
  • 363 exception = (Throwable) meth.invoke(exception, (Object[]) null);
  • 364
  • 365 if (prev == exception) {
  • 366 break;
  • 367 }
  • 368 } else {
  • 369 exception = null;
  • 370 }
  • 371 } catch (InvocationTargetException ite) {
  • 372 exception = null;
  • 373 } catch (IllegalAccessException iae) {
  • 374 exception = null;
  • 375 } catch (NoSuchMethodException nsme) {
  • 376 exception = null;
  • 377 }
  • 378 }
  • 379 }
  • 380 }
  • 381}

文件:DTMException.java
包名:com.sun.org.apache.xml.internal.dtm
类名:DTMException
继承:RuntimeException
接口: