View Javadoc

1   /*
2    * Copyright 1999-2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.latka;
18  
19  import java.io.PrintStream;
20  import java.io.PrintWriter;
21  
22  import org.xml.sax.SAXException;
23  
24  /***
25   * Latka throws this exception to the client whenever a problem
26   * occurs that is not covered by one of the standard JDK exceptions
27   * (validation errors, SAX problems, etc.)
28   *
29   * @author Morgan Delagrange
30   * @author dIon Gillard
31   * @version $Id: LatkaException.java 155424 2005-02-26 13:09:29Z dirkv $
32   */
33  public class LatkaException extends Exception {
34    
35      /***
36       * The original exception
37       */
38      protected Exception _wrappedException = null;
39  
40      /***
41       * Standard exception constructor
42       * @param message some text explaining the exception
43       */
44      public LatkaException(String message) {
45          super(message);
46      }
47  
48      /***
49       * Wrapped exception.
50       *
51       * @param e exception to wrap
52       */
53      public LatkaException(Exception e) {
54          super(e.toString());
55          _wrappedException = e;
56      }
57  
58      /***
59       * Get a wrapped exception
60       *
61       * @return a wrapped exception, or null if no wrapped
62       *         exception exists.
63       */
64      public Exception getException() {
65          return _wrappedException;
66      }
67  
68      /***
69       * Print a wrapped exception to stdout and, in the case
70       * of SAXExceptions, an additional wrapped exception.
71       * This method does _not_ print the exception's message
72       * itself.
73       *
74       * @param e LatkaException with wrapped messages.
75       */
76      public static void printWrappedExceptions(LatkaException e) {
77          Exception wrappedException = e.getException();
78  
79          if (wrappedException != null) {
80              System.out.println("Wraps exception:");
81              e.printStackTrace();
82  
83              if (wrappedException instanceof SAXException) {
84                  Exception saxWrappedException =
85                    ((SAXException) wrappedException).getException();
86                  if (saxWrappedException != null) {
87                      System.out.println("Wraps exception:");
88                      e.printStackTrace();
89                  }
90              }
91  
92          }
93      }
94  
95      /***
96       * provide wrapped exception details 
97       */
98      public void printStackTrace() {
99          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 }