1
2
3
4
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
56
57
58
59
60
61
62 package org.jaxen.saxpath;
63
64 import java.io.PrintStream;
65 import java.io.PrintWriter;
66
67 /*** Base of all SAXPath exceptions.
68 *
69 * @author bob mcwhirter (bob@werken.com)
70 */
71 public class SAXPathException extends Exception
72 {
73
74 static double javaVersion = 1.4;
75
76 static {
77 try {
78 String versionString = System.getProperty("java.version");
79 versionString = versionString.substring(0, 3);
80 javaVersion = Double.valueOf(versionString).doubleValue();
81 }
82 catch (Exception ex) {
83
84
85 }
86 }
87
88 /*** Create a new SAXPathException with a given message.
89 *
90 * @param message the error message
91 */
92 public SAXPathException(String message)
93 {
94 super( message );
95 }
96
97 /*** Create a new SAXPathException based on another exception
98 *
99 * @param cause the error source
100 */
101 public SAXPathException(Throwable cause)
102 {
103 super ( cause.getMessage() );
104 initCause(cause);
105 }
106
107 /***
108 * Create a new SAXPathException with the specified detail message
109 * and root cause.
110 *
111 * @param message the detail message
112 * @param cause the cause of this exception
113 */
114 public SAXPathException(String message, Throwable cause) {
115 super( message );
116 initCause(cause);
117 }
118
119
120 private Throwable cause;
121 private boolean causeSet = false;
122
123 /***
124 * Returns the exception that caused this exception.
125 * This is necessary to implement Java 1.4 chained exception
126 * functionality in a Java 1.3-compatible way.
127 *
128 * @return the exception that caused this exception
129 */
130 public Throwable getCause() {
131 return cause;
132 }
133
134
135 /***
136 * Sets the exception that caused this exception.
137 * This is necessary to implement Java 1.4 chained exception
138 * functionality in a Java 1.3-compatible way.
139 *
140 * @param cause the exception wrapped in this runtime exception
141 *
142 * @return this exception
143 */
144 public Throwable initCause(Throwable cause) {
145 if (causeSet) throw new IllegalStateException("Cause cannot be reset");
146 if (cause == this) throw new IllegalArgumentException("Exception cannot be its own cause");
147 causeSet = true;
148 this.cause = cause;
149 return this;
150 }
151
152 /*** Print this exception's stack trace, followed by the
153 * source exception's trace, if any.
154 *
155 * @param s the stream on which to print the stack trace
156 */
157 public void printStackTrace ( PrintStream s )
158 {
159 super.printStackTrace ( s );
160 if (javaVersion < 1.4 && getCause() != null) {
161 s.print( "Caused by: " );
162 getCause().printStackTrace( s );
163 }
164 }
165
166 /*** Print this exception's stack trace, followed by the
167 * source exception's stack trace, if any.
168 *
169 * @param s the writer on which to print the stack trace
170 */
171 public void printStackTrace ( PrintWriter s )
172 {
173 super.printStackTrace( s );
174 if (javaVersion < 1.4 && getCause() != null) {
175 s.print( "Caused by: " );
176 getCause().printStackTrace( s );
177 }
178 }
179
180 }