001 /*
002 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.xml.transform;
027
028 import java.lang.reflect.Method;
029 import java.lang.reflect.InvocationTargetException;
030
031 /**
032 * This class specifies an exceptional condition that occured
033 * during the transformation process.
034 */
035 public class TransformerException extends Exception {
036
037 /** Field locator specifies where the error occured */
038 SourceLocator locator;
039
040 /**
041 * Method getLocator retrieves an instance of a SourceLocator
042 * object that specifies where an error occured.
043 *
044 * @return A SourceLocator object, or null if none was specified.
045 */
046 public SourceLocator getLocator() {
047 return locator;
048 }
049
050 /**
051 * Method setLocator sets an instance of a SourceLocator
052 * object that specifies where an error occured.
053 *
054 * @param location A SourceLocator object, or null to clear the location.
055 */
056 public void setLocator(SourceLocator location) {
057 locator = location;
058 }
059
060 /** Field containedException specifies a wrapped exception. May be null. */
061 Throwable containedException;
062
063 /**
064 * This method retrieves an exception that this exception wraps.
065 *
066 * @return An Throwable object, or null.
067 * @see #getCause
068 */
069 public Throwable getException() {
070 return containedException;
071 }
072
073 /**
074 * Returns the cause of this throwable or <code>null</code> if the
075 * cause is nonexistent or unknown. (The cause is the throwable that
076 * caused this throwable to get thrown.)
077 */
078 public Throwable getCause() {
079
080 return ((containedException == this ) ? null
081 : containedException);
082 }
083
084 /**
085 * Initializes the <i>cause</i> of this throwable to the specified value.
086 * (The cause is the throwable that caused this throwable to get thrown.)
087 *
088 * <p>This method can be called at most once. It is generally called from
089 * within the constructor, or immediately after creating the
090 * throwable. If this throwable was created
091 * with {@link #TransformerException(Throwable)} or
092 * {@link #TransformerException(String,Throwable)}, this method cannot be called
093 * even once.
094 *
095 * @param cause the cause (which is saved for later retrieval by the
096 * {@link #getCause()} method). (A <code>null</code> value is
097 * permitted, and indicates that the cause is nonexistent or
098 * unknown.)
099 * @return a reference to this <code>Throwable</code> instance.
100 * @throws IllegalArgumentException if <code>cause</code> is this
101 * throwable. (A throwable cannot
102 * be its own cause.)
103 * @throws IllegalStateException if this throwable was
104 * created with {@link #TransformerException(Throwable)} or
105 * {@link #TransformerException(String,Throwable)}, or this method has already
106 * been called on this throwable.
107 */
108 public synchronized Throwable initCause(Throwable cause) {
109
110 if (this .containedException != null) {
111 throw new IllegalStateException("Can't overwrite cause");
112 }
113
114 if (cause == this ) {
115 throw new IllegalArgumentException(
116 "Self-causation not permitted");
117 }
118
119 this .containedException = cause;
120
121 return this ;
122 }
123
124 /**
125 * Create a new TransformerException.
126 *
127 * @param message The error or warning message.
128 */
129 public TransformerException(String message) {
130
131 super (message);
132
133 this .containedException = null;
134 this .locator = null;
135 }
136
137 /**
138 * Create a new TransformerException wrapping an existing exception.
139 *
140 * @param e The exception to be wrapped.
141 */
142 public TransformerException(Throwable e) {
143
144 super (e.toString());
145
146 this .containedException = e;
147 this .locator = null;
148 }
149
150 /**
151 * Wrap an existing exception in a TransformerException.
152 *
153 * <p>This is used for throwing processor exceptions before
154 * the processing has started.</p>
155 *
156 * @param message The error or warning message, or null to
157 * use the message from the embedded exception.
158 * @param e Any exception
159 */
160 public TransformerException(String message, Throwable e) {
161
162 super (((message == null) || (message.length() == 0)) ? e
163 .toString() : message);
164
165 this .containedException = e;
166 this .locator = null;
167 }
168
169 /**
170 * Create a new TransformerException from a message and a Locator.
171 *
172 * <p>This constructor is especially useful when an application is
173 * creating its own exception from within a DocumentHandler
174 * callback.</p>
175 *
176 * @param message The error or warning message.
177 * @param locator The locator object for the error or warning.
178 */
179 public TransformerException(String message, SourceLocator locator) {
180
181 super (message);
182
183 this .containedException = null;
184 this .locator = locator;
185 }
186
187 /**
188 * Wrap an existing exception in a TransformerException.
189 *
190 * @param message The error or warning message, or null to
191 * use the message from the embedded exception.
192 * @param locator The locator object for the error or warning.
193 * @param e Any exception
194 */
195 public TransformerException(String message, SourceLocator locator,
196 Throwable e) {
197
198 super (message);
199
200 this .containedException = e;
201 this .locator = locator;
202 }
203
204 /**
205 * Get the error message with location information
206 * appended.
207 *
208 * @return A <code>String</code> representing the error message with
209 * location information appended.
210 */
211 public String getMessageAndLocation() {
212
213 StringBuffer sbuffer = new StringBuffer();
214 String message = super .getMessage();
215
216 if (null != message) {
217 sbuffer.append(message);
218 }
219
220 if (null != locator) {
221 String systemID = locator.getSystemId();
222 int line = locator.getLineNumber();
223 int column = locator.getColumnNumber();
224
225 if (null != systemID) {
226 sbuffer.append("; SystemID: ");
227 sbuffer.append(systemID);
228 }
229
230 if (0 != line) {
231 sbuffer.append("; Line#: ");
232 sbuffer.append(line);
233 }
234
235 if (0 != column) {
236 sbuffer.append("; Column#: ");
237 sbuffer.append(column);
238 }
239 }
240
241 return sbuffer.toString();
242 }
243
244 /**
245 * Get the location information as a string.
246 *
247 * @return A string with location info, or null
248 * if there is no location information.
249 */
250 public String getLocationAsString() {
251
252 if (null != locator) {
253 StringBuffer sbuffer = new StringBuffer();
254 String systemID = locator.getSystemId();
255 int line = locator.getLineNumber();
256 int column = locator.getColumnNumber();
257
258 if (null != systemID) {
259 sbuffer.append("; SystemID: ");
260 sbuffer.append(systemID);
261 }
262
263 if (0 != line) {
264 sbuffer.append("; Line#: ");
265 sbuffer.append(line);
266 }
267
268 if (0 != column) {
269 sbuffer.append("; Column#: ");
270 sbuffer.append(column);
271 }
272
273 return sbuffer.toString();
274 } else {
275 return null;
276 }
277 }
278
279 /**
280 * Print the the trace of methods from where the error
281 * originated. This will trace all nested exception
282 * objects, as well as this object.
283 */
284 public void printStackTrace() {
285 printStackTrace(new java.io.PrintWriter(System.err, true));
286 }
287
288 /**
289 * Print the the trace of methods from where the error
290 * originated. This will trace all nested exception
291 * objects, as well as this object.
292 * @param s The stream where the dump will be sent to.
293 */
294 public void printStackTrace(java.io.PrintStream s) {
295 printStackTrace(new java.io.PrintWriter(s));
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 writer where the dump will be sent to.
303 */
304 public void printStackTrace(java.io.PrintWriter s) {
305
306 if (s == null) {
307 s = new java.io.PrintWriter(System.err, true);
308 }
309
310 try {
311 String locInfo = getLocationAsString();
312
313 if (null != locInfo) {
314 s.println(locInfo);
315 }
316
317 super .printStackTrace(s);
318 } catch (Throwable e) {
319 }
320
321 Throwable exception = getException();
322
323 for (int i = 0; (i < 10) && (null != exception); i++) {
324 s.println("---------");
325
326 try {
327 if (exception instanceof TransformerException) {
328 String locInfo = ((TransformerException) exception)
329 .getLocationAsString();
330
331 if (null != locInfo) {
332 s.println(locInfo);
333 }
334 }
335
336 exception.printStackTrace(s);
337 } catch (Throwable e) {
338 s.println("Could not print stack trace...");
339 }
340
341 try {
342 Method meth = ((Object) exception).getClass()
343 .getMethod("getException", (Class[]) null);
344
345 if (null != meth) {
346 Throwable prev = exception;
347
348 exception = (Throwable) meth.invoke(exception,
349 (Object[]) null);
350
351 if (prev == exception) {
352 break;
353 }
354 } else {
355 exception = null;
356 }
357 } catch (InvocationTargetException ite) {
358 exception = null;
359 } catch (IllegalAccessException iae) {
360 exception = null;
361 } catch (NoSuchMethodException nsme) {
362 exception = null;
363 }
364 }
365 // insure output is written
366 s.flush();
367 }
368 }
|