19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the  "License");
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License.
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License.
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: DTMException.java 468653 2006-10-28 07:07:05Z minchau $
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xml.dtm;
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.lang.reflect.InvocationTargetException;
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.lang.reflect.Method;
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport javax.xml.transform.SourceLocator;
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.res.XMLErrorResources;
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.res.XMLMessages;
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class specifies an exceptional condition that occured
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * in the DTM module.
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class DTMException extends RuntimeException {
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    static final long serialVersionUID = -775576419181334734L;
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /** Field locator specifies where the error occured.
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *  @serial */
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    SourceLocator locator;
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Method getLocator retrieves an instance of a SourceLocator
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * object that specifies where an error occured.
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @return A SourceLocator object, or null if none was specified.
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public SourceLocator getLocator() {
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return locator;
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Method setLocator sets an instance of a SourceLocator
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * object that specifies where an error occured.
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param location A SourceLocator object, or null to clear the location.
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public void setLocator(SourceLocator location) {
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        locator = location;
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /** Field containedException specifies a wrapped exception.  May be null.
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *  @serial */
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    Throwable containedException;
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * This method retrieves an exception that this exception wraps.
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @return An Throwable object, or null.
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @see #getCause
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public Throwable getException() {
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return containedException;
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Returns the cause of this throwable or <code>null</code> if the
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * cause is nonexistent or unknown.  (The cause is the throwable that
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * caused this throwable to get thrown.)
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public Throwable getCause() {
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return ((containedException == this)
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                ? null
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                : containedException);
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Initializes the <i>cause</i> of this throwable to the specified value.
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * (The cause is the throwable that caused this throwable to get thrown.)
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * <p>This method can be called at most once.  It is generally called from
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * within the constructor, or immediately after creating the
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * throwable.  If this throwable was created
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * with {@link #DTMException(Throwable)} or
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * {@link #DTMException(String,Throwable)}, this method cannot be called
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * even once.
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param  cause the cause (which is saved for later retrieval by the
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *         {@link #getCause()} method).  (A <tt>null</tt> value is
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *         permitted, and indicates that the cause is nonexistent or
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *         unknown.)
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @return  a reference to this <code>Throwable</code> instance.
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @throws IllegalArgumentException if <code>cause</code> is this
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *         throwable.  (A throwable cannot
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *         be its own cause.)
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @throws IllegalStateException if this throwable was
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *         created with {@link #DTMException(Throwable)} or
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *         {@link #DTMException(String,Throwable)}, or this method has already
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *         been called on this throwable.
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public synchronized Throwable initCause(Throwable cause) {
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if ((this.containedException == null) && (cause != null)) {
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            throw new IllegalStateException(XMLMessages.createXMLMessage(XMLErrorResources.ER_CANNOT_OVERWRITE_CAUSE, null)); //"Can't overwrite cause");
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (cause == this) {
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            throw new IllegalArgumentException(
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                XMLMessages.createXMLMessage(XMLErrorResources.ER_SELF_CAUSATION_NOT_PERMITTED, null)); //"Self-causation not permitted");
1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.containedException = cause;
1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return this;
1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Create a new DTMException.
1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param message The error or warning message.
1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public DTMException(String message) {
1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        super(message);
1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.containedException = null;
1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.locator            = null;
1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Create a new DTMException wrapping an existing exception.
1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param e The exception to be wrapped.
1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public DTMException(Throwable e) {
1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        super(e.getMessage());
1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.containedException = e;
1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.locator            = null;
1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
1579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Wrap an existing exception in a DTMException.
1589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
1599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * <p>This is used for throwing processor exceptions before
1609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * the processing has started.</p>
1619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
1629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param message The error or warning message, or null to
1639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *                use the message from the embedded exception.
1649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param e Any exception
1659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
1669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public DTMException(String message, Throwable e) {
1679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        super(((message == null) || (message.length() == 0))
1699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson              ? e.getMessage()
1709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson              : message);
1719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.containedException = e;
1739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.locator            = null;
1749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
1779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Create a new DTMException from a message and a Locator.
1789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
1799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * <p>This constructor is especially useful when an application is
1809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * creating its own exception from within a DocumentHandler
1819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * callback.</p>
1829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
1839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param message The error or warning message.
1849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param locator The locator object for the error or warning.
1859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
1869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public DTMException(String message, SourceLocator locator) {
1879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        super(message);
1899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.containedException = null;
1919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.locator            = locator;
1929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
1959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Wrap an existing exception in a DTMException.
1969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
1979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param message The error or warning message, or null to
1989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *                use the message from the embedded exception.
1999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param locator The locator object for the error or warning.
2009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param e Any exception
2019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
2029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public DTMException(String message, SourceLocator locator,
2039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                                Throwable e) {
2049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        super(message);
2069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.containedException = e;
2089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        this.locator            = locator;
2099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
2129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Get the error message with location information
2139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * appended.
2149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
2159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public String getMessageAndLocation() {
2169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        StringBuffer sbuffer = new StringBuffer();
2189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        String       message = super.getMessage();
2199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (null != message) {
2219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            sbuffer.append(message);
2229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
2239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (null != locator) {
2259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            String systemID = locator.getSystemId();
2269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            int    line     = locator.getLineNumber();
2279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            int    column   = locator.getColumnNumber();
2289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            if (null != systemID) {
2309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append("; SystemID: ");
2319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append(systemID);
2329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
2339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            if (0 != line) {
2359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append("; Line#: ");
2369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append(line);
2379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
2389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            if (0 != column) {
2409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append("; Column#: ");
2419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append(column);
2429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
2439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
2449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return sbuffer.toString();
2469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
2499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Get the location information as a string.
2509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
2519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @return A string with location info, or null
2529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * if there is no location information.
2539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
2549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public String getLocationAsString() {
2559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (null != locator) {
2579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            StringBuffer sbuffer  = new StringBuffer();
2589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            String       systemID = locator.getSystemId();
2599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            int          line     = locator.getLineNumber();
2609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            int          column   = locator.getColumnNumber();
2619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            if (null != systemID) {
2639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append("; SystemID: ");
2649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append(systemID);
2659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
2669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            if (0 != line) {
2689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append("; Line#: ");
2699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append(line);
2709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
2719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            if (0 != column) {
2739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append("; Column#: ");
2749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                sbuffer.append(column);
2759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
2769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            return sbuffer.toString();
2789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        } else {
2799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            return null;
2809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
2819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
2849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Print the the trace of methods from where the error
2859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * originated.  This will trace all nested exception
2869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * objects, as well as this object.
2879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
2889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public void printStackTrace() {
2899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        printStackTrace(new java.io.PrintWriter(System.err, true));
2909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
2939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Print the the trace of methods from where the error
2949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * originated.  This will trace all nested exception
2959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * objects, as well as this object.
2969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param s The stream where the dump will be sent to.
2979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
2989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public void printStackTrace(java.io.PrintStream s) {
2999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        printStackTrace(new java.io.PrintWriter(s));
3009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
3019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
3039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Print the the trace of methods from where the error
3049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * originated.  This will trace all nested exception
3059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * objects, as well as this object.
3069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param s The writer where the dump will be sent to.
3079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
3089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public void printStackTrace(java.io.PrintWriter s) {
3099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (s == null) {
3119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            s = new java.io.PrintWriter(System.err, true);
3129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
3139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        try {
3159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            String locInfo = getLocationAsString();
3169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            if (null != locInfo) {
3189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                s.println(locInfo);
3199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
3209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            super.printStackTrace(s);
3229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        } catch (Throwable e) {}
3239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        boolean isJdk14OrHigher = false;
3259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        try {
3269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            Throwable.class.getMethod("getCause", (Class<?>) null);
3279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            isJdk14OrHigher = true;
3289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        } catch (NoSuchMethodException nsme) {
3299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            // do nothing
3309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
3319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // The printStackTrace method of the Throwable class in jdk 1.4
3339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // and higher will include the cause when printing the backtrace.
3349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // The following code is only required when using jdk 1.3 or lower
3359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (!isJdk14OrHigher) {
3369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            Throwable exception = getException();
3379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            for (int i = 0; (i < 10) && (null != exception); i++) {
3399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                s.println("---------");
3409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                try {
3429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    if (exception instanceof DTMException) {
3439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        String locInfo =
3449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                            ((DTMException) exception)
3459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                                .getLocationAsString();
3469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        if (null != locInfo) {
3489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                            s.println(locInfo);
3499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        }
3509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    }
3519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    exception.printStackTrace(s);
3539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                } catch (Throwable e) {
3549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    s.println("Could not print stack trace...");
3559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                }
3569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                try {
3589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    Method meth =
3599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        ((Object) exception).getClass().getMethod("getException",
3609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                            (Class<?>) null);
3619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    if (null != meth) {
3639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        Throwable prev = exception;
3649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        exception = (Throwable) meth.invoke(exception, (Class<?>) null);
3669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        if (prev == exception) {
3689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                            break;
3699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        }
3709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    } else {
3719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        exception = null;
3729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    }
3739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                } catch (InvocationTargetException ite) {
3749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    exception = null;
3759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                } catch (IllegalAccessException iae) {
3769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    exception = null;
3779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                } catch (NoSuchMethodException nsme) {
3789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    exception = null;
3799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                }
3809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
3819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
3829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
3839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
384