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