1 package org.jaxen;
2
3 import java.io.PrintStream;
4 import java.io.PrintWriter;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 }