12005
|
1 |
/*
|
|
2 |
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.xml.transform;
|
|
27 |
|
|
28 |
import java.lang.reflect.Method;
|
|
29 |
import java.lang.reflect.InvocationTargetException;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* This class specifies an exceptional condition that occured
|
|
33 |
* during the transformation process.
|
25262
|
34 |
*
|
|
35 |
* @since 1.4
|
12005
|
36 |
*/
|
|
37 |
public class TransformerException extends Exception {
|
|
38 |
|
|
39 |
/** Field locator specifies where the error occured */
|
|
40 |
SourceLocator locator;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* Method getLocator retrieves an instance of a SourceLocator
|
|
44 |
* object that specifies where an error occured.
|
|
45 |
*
|
|
46 |
* @return A SourceLocator object, or null if none was specified.
|
|
47 |
*/
|
|
48 |
public SourceLocator getLocator() {
|
|
49 |
return locator;
|
|
50 |
}
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Method setLocator sets an instance of a SourceLocator
|
|
54 |
* object that specifies where an error occured.
|
|
55 |
*
|
|
56 |
* @param location A SourceLocator object, or null to clear the location.
|
|
57 |
*/
|
|
58 |
public void setLocator(SourceLocator location) {
|
|
59 |
locator = location;
|
|
60 |
}
|
|
61 |
|
|
62 |
/** Field containedException specifies a wrapped exception. May be null. */
|
|
63 |
Throwable containedException;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* This method retrieves an exception that this exception wraps.
|
|
67 |
*
|
|
68 |
* @return An Throwable object, or null.
|
|
69 |
* @see #getCause
|
|
70 |
*/
|
|
71 |
public Throwable getException() {
|
|
72 |
return containedException;
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Returns the cause of this throwable or <code>null</code> if the
|
|
77 |
* cause is nonexistent or unknown. (The cause is the throwable that
|
|
78 |
* caused this throwable to get thrown.)
|
|
79 |
*/
|
|
80 |
public Throwable getCause() {
|
|
81 |
|
|
82 |
return ((containedException == this)
|
|
83 |
? null
|
|
84 |
: containedException);
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Initializes the <i>cause</i> of this throwable to the specified value.
|
|
89 |
* (The cause is the throwable that caused this throwable to get thrown.)
|
|
90 |
*
|
|
91 |
* <p>This method can be called at most once. It is generally called from
|
|
92 |
* within the constructor, or immediately after creating the
|
|
93 |
* throwable. If this throwable was created
|
|
94 |
* with {@link #TransformerException(Throwable)} or
|
|
95 |
* {@link #TransformerException(String,Throwable)}, this method cannot be called
|
|
96 |
* even once.
|
|
97 |
*
|
|
98 |
* @param cause the cause (which is saved for later retrieval by the
|
|
99 |
* {@link #getCause()} method). (A <code>null</code> value is
|
|
100 |
* permitted, and indicates that the cause is nonexistent or
|
|
101 |
* unknown.)
|
|
102 |
* @return a reference to this <code>Throwable</code> instance.
|
|
103 |
* @throws IllegalArgumentException if <code>cause</code> is this
|
|
104 |
* throwable. (A throwable cannot
|
|
105 |
* be its own cause.)
|
|
106 |
* @throws IllegalStateException if this throwable was
|
|
107 |
* created with {@link #TransformerException(Throwable)} or
|
|
108 |
* {@link #TransformerException(String,Throwable)}, or this method has already
|
|
109 |
* been called on this throwable.
|
|
110 |
*/
|
|
111 |
public synchronized Throwable initCause(Throwable cause) {
|
|
112 |
|
31497
|
113 |
// TransformerException doesn't set its cause (probably
|
|
114 |
// because it predates initCause()) - and we may not want
|
|
115 |
// to change this since Exceptions are serializable...
|
|
116 |
// But this also leads to the broken code in printStackTrace
|
|
117 |
// below...
|
|
118 |
|
12005
|
119 |
if (this.containedException != null) {
|
|
120 |
throw new IllegalStateException("Can't overwrite cause");
|
|
121 |
}
|
|
122 |
|
|
123 |
if (cause == this) {
|
|
124 |
throw new IllegalArgumentException(
|
|
125 |
"Self-causation not permitted");
|
|
126 |
}
|
|
127 |
|
|
128 |
this.containedException = cause;
|
|
129 |
|
|
130 |
return this;
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Create a new TransformerException.
|
|
135 |
*
|
|
136 |
* @param message The error or warning message.
|
|
137 |
*/
|
|
138 |
public TransformerException(String message) {
|
|
139 |
|
|
140 |
super(message);
|
|
141 |
|
|
142 |
this.containedException = null;
|
|
143 |
this.locator = null;
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Create a new TransformerException wrapping an existing exception.
|
|
148 |
*
|
|
149 |
* @param e The exception to be wrapped.
|
|
150 |
*/
|
|
151 |
public TransformerException(Throwable e) {
|
|
152 |
|
|
153 |
super(e.toString());
|
|
154 |
|
|
155 |
this.containedException = e;
|
|
156 |
this.locator = null;
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Wrap an existing exception in a TransformerException.
|
|
161 |
*
|
|
162 |
* <p>This is used for throwing processor exceptions before
|
|
163 |
* the processing has started.</p>
|
|
164 |
*
|
|
165 |
* @param message The error or warning message, or null to
|
|
166 |
* use the message from the embedded exception.
|
|
167 |
* @param e Any exception
|
|
168 |
*/
|
|
169 |
public TransformerException(String message, Throwable e) {
|
|
170 |
|
|
171 |
super(((message == null) || (message.length() == 0))
|
|
172 |
? e.toString()
|
|
173 |
: message);
|
|
174 |
|
|
175 |
this.containedException = e;
|
|
176 |
this.locator = null;
|
|
177 |
}
|
|
178 |
|
|
179 |
/**
|
|
180 |
* Create a new TransformerException from a message and a Locator.
|
|
181 |
*
|
|
182 |
* <p>This constructor is especially useful when an application is
|
|
183 |
* creating its own exception from within a DocumentHandler
|
|
184 |
* callback.</p>
|
|
185 |
*
|
|
186 |
* @param message The error or warning message.
|
|
187 |
* @param locator The locator object for the error or warning.
|
|
188 |
*/
|
|
189 |
public TransformerException(String message, SourceLocator locator) {
|
|
190 |
|
|
191 |
super(message);
|
|
192 |
|
|
193 |
this.containedException = null;
|
|
194 |
this.locator = locator;
|
|
195 |
}
|
|
196 |
|
|
197 |
/**
|
|
198 |
* Wrap an existing exception in a TransformerException.
|
|
199 |
*
|
|
200 |
* @param message The error or warning message, or null to
|
|
201 |
* use the message from the embedded exception.
|
|
202 |
* @param locator The locator object for the error or warning.
|
|
203 |
* @param e Any exception
|
|
204 |
*/
|
|
205 |
public TransformerException(String message, SourceLocator locator,
|
|
206 |
Throwable e) {
|
|
207 |
|
|
208 |
super(message);
|
|
209 |
|
|
210 |
this.containedException = e;
|
|
211 |
this.locator = locator;
|
|
212 |
}
|
|
213 |
|
|
214 |
/**
|
|
215 |
* Get the error message with location information
|
|
216 |
* appended.
|
|
217 |
*
|
|
218 |
* @return A <code>String</code> representing the error message with
|
|
219 |
* location information appended.
|
|
220 |
*/
|
|
221 |
public String getMessageAndLocation() {
|
|
222 |
|
|
223 |
StringBuffer sbuffer = new StringBuffer();
|
|
224 |
String message = super.getMessage();
|
|
225 |
|
|
226 |
if (null != message) {
|
|
227 |
sbuffer.append(message);
|
|
228 |
}
|
|
229 |
|
|
230 |
if (null != locator) {
|
|
231 |
String systemID = locator.getSystemId();
|
|
232 |
int line = locator.getLineNumber();
|
|
233 |
int column = locator.getColumnNumber();
|
|
234 |
|
|
235 |
if (null != systemID) {
|
|
236 |
sbuffer.append("; SystemID: ");
|
|
237 |
sbuffer.append(systemID);
|
|
238 |
}
|
|
239 |
|
|
240 |
if (0 != line) {
|
|
241 |
sbuffer.append("; Line#: ");
|
|
242 |
sbuffer.append(line);
|
|
243 |
}
|
|
244 |
|
|
245 |
if (0 != column) {
|
|
246 |
sbuffer.append("; Column#: ");
|
|
247 |
sbuffer.append(column);
|
|
248 |
}
|
|
249 |
}
|
|
250 |
|
|
251 |
return sbuffer.toString();
|
|
252 |
}
|
|
253 |
|
|
254 |
/**
|
|
255 |
* Get the location information as a string.
|
|
256 |
*
|
|
257 |
* @return A string with location info, or null
|
|
258 |
* if there is no location information.
|
|
259 |
*/
|
|
260 |
public String getLocationAsString() {
|
|
261 |
|
|
262 |
if (null != locator) {
|
|
263 |
StringBuffer sbuffer = new StringBuffer();
|
|
264 |
String systemID = locator.getSystemId();
|
|
265 |
int line = locator.getLineNumber();
|
|
266 |
int column = locator.getColumnNumber();
|
|
267 |
|
|
268 |
if (null != systemID) {
|
|
269 |
sbuffer.append("; SystemID: ");
|
|
270 |
sbuffer.append(systemID);
|
|
271 |
}
|
|
272 |
|
|
273 |
if (0 != line) {
|
|
274 |
sbuffer.append("; Line#: ");
|
|
275 |
sbuffer.append(line);
|
|
276 |
}
|
|
277 |
|
|
278 |
if (0 != column) {
|
|
279 |
sbuffer.append("; Column#: ");
|
|
280 |
sbuffer.append(column);
|
|
281 |
}
|
|
282 |
|
|
283 |
return sbuffer.toString();
|
|
284 |
} else {
|
|
285 |
return null;
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
|
289 |
/**
|
|
290 |
* Print the the trace of methods from where the error
|
|
291 |
* originated. This will trace all nested exception
|
|
292 |
* objects, as well as this object.
|
|
293 |
*/
|
|
294 |
public void printStackTrace() {
|
|
295 |
printStackTrace(new java.io.PrintWriter(System.err, true));
|
|
296 |
}
|
|
297 |
|
|
298 |
/**
|
|
299 |
* Print the the trace of methods from where the error
|
|
300 |
* originated. This will trace all nested exception
|
|
301 |
* objects, as well as this object.
|
|
302 |
* @param s The stream where the dump will be sent to.
|
|
303 |
*/
|
|
304 |
public void printStackTrace(java.io.PrintStream s) {
|
|
305 |
printStackTrace(new java.io.PrintWriter(s));
|
|
306 |
}
|
|
307 |
|
|
308 |
/**
|
|
309 |
* Print the the trace of methods from where the error
|
|
310 |
* originated. This will trace all nested exception
|
|
311 |
* objects, as well as this object.
|
|
312 |
* @param s The writer where the dump will be sent to.
|
|
313 |
*/
|
|
314 |
public void printStackTrace(java.io.PrintWriter s) {
|
|
315 |
|
|
316 |
if (s == null) {
|
|
317 |
s = new java.io.PrintWriter(System.err, true);
|
|
318 |
}
|
|
319 |
|
|
320 |
try {
|
31497
|
321 |
try {
|
|
322 |
String locInfo = getLocationAsString();
|
12005
|
323 |
|
31497
|
324 |
if (null != locInfo) {
|
|
325 |
s.println(locInfo);
|
|
326 |
}
|
12005
|
327 |
|
31497
|
328 |
super.printStackTrace(s);
|
|
329 |
} catch (Throwable e) {}
|
12005
|
330 |
|
31497
|
331 |
Throwable exception = getException();
|
12005
|
332 |
|
31497
|
333 |
for (int i = 0; (i < 10) && (null != exception); i++) {
|
|
334 |
s.println("---------");
|
12005
|
335 |
|
31497
|
336 |
try {
|
|
337 |
exception.printStackTrace(s);
|
|
338 |
// if exception is a TransformerException it will print
|
|
339 |
// its contained exception, so we don't need to redo it here,
|
|
340 |
// and we can exit the loop now.
|
|
341 |
if (exception instanceof TransformerException) break;
|
|
342 |
} catch (Throwable e) {
|
|
343 |
s.println("Could not print stack trace...");
|
12005
|
344 |
}
|
|
345 |
|
31497
|
346 |
try {
|
|
347 |
// Is this still needed?
|
|
348 |
Method meth = exception.getClass().getMethod("getException");
|
12005
|
349 |
|
31497
|
350 |
if (null != meth) {
|
|
351 |
Throwable prev = exception;
|
|
352 |
|
|
353 |
exception = (Throwable) meth.invoke(exception, (Object[]) null);
|
12005
|
354 |
|
31497
|
355 |
if (prev == exception) {
|
|
356 |
break;
|
|
357 |
}
|
|
358 |
} else {
|
|
359 |
exception = null;
|
12005
|
360 |
}
|
31497
|
361 |
} catch (InvocationTargetException ite) {
|
|
362 |
exception = null;
|
|
363 |
} catch (IllegalAccessException iae) {
|
|
364 |
exception = null;
|
|
365 |
} catch (NoSuchMethodException nsme) {
|
12005
|
366 |
exception = null;
|
|
367 |
}
|
|
368 |
}
|
31497
|
369 |
} finally {
|
|
370 |
// ensure output is written
|
|
371 |
s.flush();
|
12005
|
372 |
}
|
|
373 |
}
|
|
374 |
}
|