View Javadoc

1   package org.jaxen;
2   
3   import java.io.PrintStream;
4   import java.io.PrintWriter;
5   
6   /*
7    * $Header: $
8    * $Revision: $
9    * $Date: $
10   *
11   * ====================================================================
12   *
13   * Copyright 2000-2005 bob mcwhirter & James Strachan.
14   * All rights reserved.
15   *
16   *
17   * Redistribution and use in source and binary forms, with or without
18   * modification, are permitted provided that the following conditions are
19   * met:
20   * 
21   *   * Redistributions of source code must retain the above copyright
22   *     notice, this list of conditions and the following disclaimer.
23   * 
24   *   * Redistributions in binary form must reproduce the above copyright
25   *     notice, this list of conditions and the following disclaimer in the
26   *     documentation and/or other materials provided with the distribution.
27   * 
28   *   * Neither the name of the Jaxen Project nor the names of its
29   *     contributors may be used to endorse or promote products derived 
30   *     from this software without specific prior written permission.
31   * 
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
33   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
34   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
35   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
36   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
39   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   *
44   * ====================================================================
45   * This software consists of voluntary contributions made by many
46   * individuals on behalf of the Jaxen Project and was originally
47   * created by bob mcwhirter <bob@werken.com> and
48   * James Strachan <jstrachan@apache.org>.  For more information on the
49   * Jaxen Project, please see <http://www.jaxen.org/>.
50   *
51   * $Id: $
52  */
53  
54  /***
55   * This class exists to wrap Jaxen exceptions that otherwise wouldn't be propagated
56   * up through the axis iterators.
57   */
58  public class JaxenRuntimeException extends RuntimeException
59  {
60      /***
61       * 
62       */
63      private static final long serialVersionUID = -930309761511911193L;
64      
65      private Throwable cause;
66      private boolean causeSet = false;
67  
68      /***
69       * Create a new JaxenRuntimeException.
70       * 
71       * @param cause the nested exception that's wrapped 
72       *     inside this exception
73       */
74      public JaxenRuntimeException(Throwable cause)
75      {
76          super(cause.getMessage());
77          initCause(cause);
78      }
79  
80      /***
81       * Create a new JaxenRuntimeException.
82       * 
83       * @param message the detail message
84       */
85      public JaxenRuntimeException(String message) {
86          super(message);
87      }
88  
89      /***
90       * Returns the exception that caused this exception.
91       * This is necessary to implement Java 1.4 chained exception 
92       * functionality in a Java 1.3-compatible way.
93       * 
94       * @return the exception that caused this exception
95       */
96      public Throwable getCause() {
97          return cause;
98      }
99      
100 
101     /***
102      * Sets the exception that caused this exception.
103      * This is necessary to implement Java 1.4 chained exception 
104      * functionality in a Java 1.3-compatible way.
105      * 
106      * @param cause the exception wrapped in this runtime exception
107      * 
108      * @return this exception
109      */
110     public Throwable initCause(Throwable cause) {
111         if (causeSet) throw new IllegalStateException("Cause cannot be reset");
112         if (cause == this) throw new IllegalArgumentException("Exception cannot be its own cause");
113         causeSet = true;
114         this.cause = cause;
115         return this;
116     }
117 
118     /*** Print this exception's stack trace, followed by the
119      *  source exception's trace, if any.
120      *
121      * @param s the stream on which to print the stack trace
122      */
123     public void printStackTrace ( PrintStream s )
124     {
125         super.printStackTrace ( s );
126         if (JaxenException.javaVersion < 1.4 && getCause() != null) {
127             s.print( "Caused by: " );
128             getCause().printStackTrace( s );
129         }
130     }
131 
132     /*** Print this exception's stack trace, followed by the
133      *  source exception's stack trace, if any.
134      *
135      * @param s the writer on which to print the stack trace
136      */
137     public void printStackTrace ( PrintWriter s )
138     {
139         super.printStackTrace( s );
140         if (JaxenException.javaVersion < 1.4 && getCause() != null) {
141             s.print( "Caused by: " );
142             getCause().printStackTrace( s );
143         }
144     }
145     
146 }