/*-- 

 $Id: JDOMException.java,v 1.11 2002/01/08 09:17:10 jhunter Exp $

 Copyright (C) 2000 Brett McLaughlin & Jason Hunter.
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions, and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions, and the disclaimer that follows 
    these conditions in the documentation and/or other materials 
    provided with the distribution.

 3. The name "JDOM" must not be used to endorse or promote products
    derived from this software without prior written permission.  For
    written permission, please contact license@jdom.org.
 
 4. Products derived from this software may not be called "JDOM", nor
    may "JDOM" appear in their name, without prior written permission
    from the JDOM Project Management (pm@jdom.org).
 
 In addition, we request (but do not require) that you include in the 
 end-user documentation provided with the redistribution and/or in the 
 software itself an acknowledgement equivalent to the following:
     "This product includes software developed by the
      JDOM Project (http://www.jdom.org/)."
 Alternatively, the acknowledgment may be graphical using the logos 
 available at http://www.jdom.org/images/logos.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED.  IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 This software consists of voluntary contributions made by many 
 individuals on behalf of the JDOM Project and was originally 
 created by Brett McLaughlin <brett@jdom.org> and 
 Jason Hunter <jhunter@jdom.org>.  For more information on the 
 JDOM Project, please see <http://www.jdom.org/>.
 
 */

package org.jdom;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.lang.reflect.InvocationTargetException;
import java.lang.ExceptionInInitializerError;
import java.rmi.RemoteException;
import org.xml.sax.SAXException;

/**
 * <b><code>JDOMException</code></b>
 * <p>
 * This <code>Exception</code> subclass is the top level
 *   <code>Exception</code> that JDOM classes
 *   can throw.  It's subclasses add specificity to the 
 *   problems that can occur using JDOM, but this single
 *   <code>Exception</code> can be caught to handle all
 *   JDOM specific problems.
 * </p>
 * 
 * @author Brett McLaughlin
 * @author Jason Hunter
 * @version $Revision: 1.11 $, $Date: 2002/01/08 09:17:10 $
 */
public class JDOMException extends Exception {

    private static final String CVS_ID = 
      "@(#) $RCSfile: JDOMException.java,v $ $Revision: 1.11 $ $Date: 2002/01/08 09:17:10 $ $Name: jdom_1_0_b8 $";

    /** A wrapped <code>Throwable</code> */
    protected Throwable cause;

    /**
     * <p>
     * This will create an <code>Exception</code>.
     * </p>
     */
    public JDOMException() {
        super("Error occurred in JDOM application.");
    }

    /**
     * <p>
     * This will create an <code>Exception</code> with the given message.
     * </p>
     *
     * @param message <code>String</code> message indicating
     *                the problem that occurred.
     */    
    public JDOMException(String message)  {
        super(message);
    }

    /**
     * <p>
     * This will create an <code>Exception</code> with the given message
     *   and wrap another <code>Exception</code>.  This is useful when
     *   the originating <code>Exception</code> should be held on to.
     * </p>
     *
     * @param message <code>String</code> message indicating
     *                the problem that occurred.
     * @param cause <code>Throwable</code> that caused this
     *                  to be thrown.
     */    
    public JDOMException(String message, Throwable cause)  {
        super(message);    
        this.cause = cause;
    }    

    /** 
     * <p>
     * Intializes the cause of this exception to be the specified value.
     * </p>
     * @param cause <code>Throwable</code> that caused this
     *                  to be thrown.
     */
    // Created to match the JDK 1.4 Throwable method.
    public Throwable initCause(Throwable cause)  {
        this.cause = cause;
        return cause;
    }    

    /**
     * <p>
     * This returns the message for the <code>Exception</code>. If
     *   there are one or more nested exceptions, their messages
     *   are appended.
     * </p>
     *
     * @return <code>String</code> - message for <code>Exception</code>.
     */
    public String getMessage() {
        // Get this exception's message.
        String msg = super.getMessage();

        Throwable parent = this;
        Throwable child;
        
        // Look for nested exceptions.
        while((child = getNestedException(parent)) != null) {
            // Get the child's message.
            String msg2 = child.getMessage();
            
            // Special case: If a SAXException has no message of its own, but has a 
            // nested exception, then it returns the nested exception's message as its
            // message. We don't want to add that message twice.
            if (child instanceof SAXException) {
                Throwable grandchild = ((SAXException)child).getException();
                // If the SAXException tells us that it's message is identical to 
                // its nested exception's message, then we skip it, so we don't
                // add it twice.
                if (grandchild != null && msg2 != null && msg2.equals(grandchild.getMessage())) {
                    msg2 = null;
                }
            }

            // If we found a message for the child exception, we append it.
            if (msg2 != null) {
                if (msg != null) {
                    msg += ": " + msg2;
                } else {
                    msg = msg2;
                }
            }

            // Any nested JDOMException will append its own children,
            // so we need to break out of here.
            if (child instanceof JDOMException) {
                break;
            }
            parent = child;
        }

        // Return the completed message.
        return msg;
    }

    /**
     * <p>
     * This prints the stack trace of the <code>Exception</code>. If
     *   there is a root cause, the stack trace of the root
     *   <code>Exception</code> is printed right after.
     * </p>
     */
    public void printStackTrace() {
        // Print the stack trace for this exception.
        super.printStackTrace();

        Throwable parent = this;
        Throwable child;

        // Print the stack trace for each nested exception.
        while((child = getNestedException(parent)) != null) {
            if (child != null) {
                System.err.print("Caused by: ");
                child.printStackTrace();
                // Any nested JDOMException will print its own children,
                // so we need to break out of here.
                if (child instanceof JDOMException) {
                    break;
                }
                parent = child;
            }
        }
    }

    /**
     * <p>
     * This prints the stack trace of the <code>Exception</code> to the given
     *   PrintStream. If there is a root cause, the stack trace of the root
     *   <code>Exception</code> is printed right after.
     * </p>
     */
    public void printStackTrace(PrintStream s) {
        // Print the stack trace for this exception.
        super.printStackTrace(s);

        Throwable parent = this;
        Throwable child;

        // Print the stack trace for each nested exception.
        while((child = getNestedException(parent)) != null) {
            if (child != null) {
                System.err.print("Caused by: ");
                child.printStackTrace(s);
                // Any nested JDOMException will print its own children,
                // so we need to break out of here.
                if (child instanceof JDOMException) {
                    break;
                }
                parent = child;
            }
        }
    }

    /**
     * <p>
     * This prints the stack trace of the <code>Exception</code> to the given
     *   PrintWriter. If there is a root cause, the stack trace of the root
     *   <code>Exception</code> is printed right after.
     * </p>
     */
    public void printStackTrace(PrintWriter w) {
        // Print the stack trace for this exception.
        super.printStackTrace(w);

        Throwable parent = this;
        Throwable child;

        // Print the stack trace for each nested exception.
        while((child = getNestedException(parent)) != null) {
            if (child != null) {
                System.err.print("Caused by: ");
                child.printStackTrace(w);
                // Any nested JDOMException will print its own children,
                // so we need to break out of here.
                if (child instanceof JDOMException) {
                    break;
                }
                parent = child;
            }
        }
    }

    /**
     * <p>
     * This will return the root cause <code>Throwable</code>, or null
     *   if one does not exist.
     * </p>
     * 
     * @return <code>Throwable</code> - the wrapped <code>Throwable</code>.
     */
    public Throwable getCause()  {
        return cause;             
    }

    // If this Throwable has a nested (child) exception, then we return it.
    // Otherwise we return null.
    private static Throwable getNestedException(Throwable parent) {
        if (parent instanceof JDOMException) {
            return ((JDOMException)parent).getCause();
        }
        
        if (parent instanceof SAXException) {
            return ((SAXException)parent).getException();
        }
        
        if (parent instanceof SQLException) {
            return ((SQLException)parent).getNextException();
        }
        
        if (parent instanceof InvocationTargetException) {
            return ((InvocationTargetException)parent).getTargetException();
        }
        
        if (parent instanceof ExceptionInInitializerError) {
            return ((ExceptionInInitializerError)parent).getException();
        }
        
        if (parent instanceof RemoteException) {
            return ((RemoteException)parent).detail;
        }
        
        // These classes are not part of standard JDK 1.1 or 1.2, so we 
        // use reflection to access them.

        Throwable nestedException = getNestedException(parent, "javax.naming.NamingException", "getRootCause");
        if (nestedException != null) {
            return nestedException;
        }
        
        nestedException = getNestedException(parent, "javax.servlet.ServletException", "getRootCause");
        if (nestedException != null) {
            return nestedException;
        }

        return null;
    }

    // This method uses reflection to obtain the nest exception of a Throwable. We use reflection
    // because the desired class may not exist in the currently-running VM.
    private static Throwable getNestedException(
                                 Throwable parent, String className, String methodName) {
        try {
            // See if this Throwable is of the desired type, by using isAssignableFrom().
            Class testClass = Class.forName(className);
            Class objectClass = parent.getClass();
            if (testClass.isAssignableFrom(objectClass)) {
                // Use reflection to call the specified method.
                Class[] argClasses = new Class[0];
                Method method = testClass.getMethod(methodName, argClasses);
                Object[] args = new Object[0];
                return (Throwable)method.invoke(parent, args);
            }
        }
        catch(Exception ex) {
            // Most likely, the desired class is not available in this VM. That's fine.
            // Even if it's caused by something else, we don't want to display an error
            // here, since we're already in the process of trying to display the original
            // error - another error here will just confuse things.
        }

        return null;
    }
}
