001    /*
002     * Copyright 1999-2002,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    package org.apache.commons.latka;
018    
019    import java.io.PrintStream;
020    import java.io.PrintWriter;
021    
022    import org.xml.sax.SAXException;
023    
024    /**
025     * Latka throws this exception to the client whenever a problem
026     * occurs that is not covered by one of the standard JDK exceptions
027     * (validation errors, SAX problems, etc.)
028     *
029     * @author Morgan Delagrange
030     * @author dIon Gillard
031     * @version $Id: LatkaException.java 155424 2005-02-26 13:09:29Z dirkv $
032     */
033    public class LatkaException extends Exception {
034      
035        /**
036         * The original exception
037         */
038        protected Exception _wrappedException = null;
039    
040        /**
041         * Standard exception constructor
042         * @param message some text explaining the exception
043         */
044        public LatkaException(String message) {
045            super(message);
046        }
047    
048        /**
049         * Wrapped exception.
050         *
051         * @param e exception to wrap
052         */
053        public LatkaException(Exception e) {
054            super(e.toString());
055            _wrappedException = e;
056        }
057    
058        /**
059         * Get a wrapped exception
060         *
061         * @return a wrapped exception, or null if no wrapped
062         *         exception exists.
063         */
064        public Exception getException() {
065            return _wrappedException;
066        }
067    
068        /**
069         * Print a wrapped exception to stdout and, in the case
070         * of SAXExceptions, an additional wrapped exception.
071         * This method does _not_ print the exception's message
072         * itself.
073         *
074         * @param e LatkaException with wrapped messages.
075         */
076        public static void printWrappedExceptions(LatkaException e) {
077            Exception wrappedException = e.getException();
078    
079            if (wrappedException != null) {
080                System.out.println("Wraps exception:");
081                e.printStackTrace();
082    
083                if (wrappedException instanceof SAXException) {
084                    Exception saxWrappedException =
085                      ((SAXException) wrappedException).getException();
086                    if (saxWrappedException != null) {
087                        System.out.println("Wraps exception:");
088                        e.printStackTrace();
089                    }
090                }
091    
092            }
093        }
094    
095        /**
096         * provide wrapped exception details 
097         */
098        public void printStackTrace() {
099            if (getException() != null) {
100                System.err.println("Wrapped Exception details:");
101                getException().printStackTrace();
102            }
103            
104            super.printStackTrace();
105        }
106        
107        /**
108         * provide wrapped exception details 
109         * @param s PrintStream to print to
110         */
111        public void printStackTrace(PrintStream s) {
112            if (getException() != null) {
113                s.println("Wrapped Exception details:");
114                getException().printStackTrace(s);
115            }
116            
117            super.printStackTrace(s);
118        }
119        
120        /**
121         * Print wrapped exception details, if any
122         * @param s the print writer to send output to
123         */
124        public void printStackTrace(PrintWriter s) {
125            if (getException() != null) {
126                s.println("Wrapped Exception details:");
127                getException().printStackTrace(s);
128            }
129            
130            super.printStackTrace(s);
131        }
132        
133    }