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