KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > transform > TransformerException


1 // $Id: TransformerException.java,v 1.2 2003/09/10 01:45:55 jsuttor Exp $
2
/*
3  * @(#)TransformerException.java 1.17 04/07/26
4  *
5  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
6  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
7  */

8
9 package javax.xml.transform;
10
11 import java.lang.reflect.Method JavaDoc;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13
14 /**
15  * This class specifies an exceptional condition that occured
16  * during the transformation process.
17  */

18 public class TransformerException extends Exception JavaDoc {
19
20     /** Field locator specifies where the error occured */
21     SourceLocator JavaDoc locator;
22
23     /**
24      * Method getLocator retrieves an instance of a SourceLocator
25      * object that specifies where an error occured.
26      *
27      * @return A SourceLocator object, or null if none was specified.
28      */

29     public SourceLocator JavaDoc getLocator() {
30         return locator;
31     }
32
33     /**
34      * Method setLocator sets an instance of a SourceLocator
35      * object that specifies where an error occured.
36      *
37      * @param location A SourceLocator object, or null to clear the location.
38      */

39     public void setLocator(SourceLocator JavaDoc location) {
40         locator = location;
41     }
42
43     /** Field containedException specifies a wrapped exception. May be null. */
44     Throwable JavaDoc containedException;
45
46     /**
47      * This method retrieves an exception that this exception wraps.
48      *
49      * @return An Throwable object, or null.
50      * @see #getCause
51      */

52     public Throwable JavaDoc getException() {
53         return containedException;
54     }
55
56     /**
57      * Returns the cause of this throwable or <code>null</code> if the
58      * cause is nonexistent or unknown. (The cause is the throwable that
59      * caused this throwable to get thrown.)
60      */

61     public Throwable JavaDoc getCause() {
62
63         return ((containedException == this)
64                 ? null
65                 : containedException);
66     }
67
68     /**
69      * Initializes the <i>cause</i> of this throwable to the specified value.
70      * (The cause is the throwable that caused this throwable to get thrown.)
71      *
72      * <p>This method can be called at most once. It is generally called from
73      * within the constructor, or immediately after creating the
74      * throwable. If this throwable was created
75      * with {@link #TransformerException(Throwable)} or
76      * {@link #TransformerException(String,Throwable)}, this method cannot be called
77      * even once.
78      *
79      * @param cause the cause (which is saved for later retrieval by the
80      * {@link #getCause()} method). (A <tt>null</tt> value is
81      * permitted, and indicates that the cause is nonexistent or
82      * unknown.)
83      * @return a reference to this <code>Throwable</code> instance.
84      * @throws IllegalArgumentException if <code>cause</code> is this
85      * throwable. (A throwable cannot
86      * be its own cause.)
87      * @throws IllegalStateException if this throwable was
88      * created with {@link #TransformerException(Throwable)} or
89      * {@link #TransformerException(String,Throwable)}, or this method has already
90      * been called on this throwable.
91      */

92     public synchronized Throwable JavaDoc initCause(Throwable JavaDoc cause) {
93
94         if (this.containedException != null) {
95             throw new IllegalStateException JavaDoc("Can't overwrite cause");
96         }
97
98         if (cause == this) {
99             throw new IllegalArgumentException JavaDoc(
100                 "Self-causation not permitted");
101         }
102
103         this.containedException = cause;
104
105         return this;
106     }
107
108     /**
109      * Create a new TransformerException.
110      *
111      * @param message The error or warning message.
112      */

113     public TransformerException(String JavaDoc message) {
114
115         super(message);
116
117         this.containedException = null;
118         this.locator = null;
119     }
120
121     /**
122      * Create a new TransformerException wrapping an existing exception.
123      *
124      * @param e The exception to be wrapped.
125      */

126     public TransformerException(Throwable JavaDoc e) {
127
128         super(e.toString());
129
130         this.containedException = e;
131         this.locator = null;
132     }
133
134     /**
135      * Wrap an existing exception in a TransformerException.
136      *
137      * <p>This is used for throwing processor exceptions before
138      * the processing has started.</p>
139      *
140      * @param message The error or warning message, or null to
141      * use the message from the embedded exception.
142      * @param e Any exception
143      */

144     public TransformerException(String JavaDoc message, Throwable JavaDoc e) {
145
146         super(((message == null) || (message.length() == 0))
147               ? e.toString()
148               : message);
149
150         this.containedException = e;
151         this.locator = null;
152     }
153
154     /**
155      * Create a new TransformerException from a message and a Locator.
156      *
157      * <p>This constructor is especially useful when an application is
158      * creating its own exception from within a DocumentHandler
159      * callback.</p>
160      *
161      * @param message The error or warning message.
162      * @param locator The locator object for the error or warning.
163      */

164     public TransformerException(String JavaDoc message, SourceLocator JavaDoc locator) {
165
166         super(message);
167
168         this.containedException = null;
169         this.locator = locator;
170     }
171
172     /**
173      * Wrap an existing exception in a TransformerException.
174      *
175      * @param message The error or warning message, or null to
176      * use the message from the embedded exception.
177      * @param locator The locator object for the error or warning.
178      * @param e Any exception
179      */

180     public TransformerException(String JavaDoc message, SourceLocator JavaDoc locator,
181                                 Throwable JavaDoc e) {
182
183         super(message);
184
185         this.containedException = e;
186         this.locator = locator;
187     }
188
189     /**
190      * Get the error message with location information
191      * appended.
192      *
193      * @return A <code>String</code> representing the error message with
194      * location information appended.
195      */

196     public String JavaDoc getMessageAndLocation() {
197
198         StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
199         String JavaDoc message = super.getMessage();
200
201         if (null != message) {
202             sbuffer.append(message);
203         }
204
205         if (null != locator) {
206             String JavaDoc systemID = locator.getSystemId();
207             int line = locator.getLineNumber();
208             int column = locator.getColumnNumber();
209
210             if (null != systemID) {
211                 sbuffer.append("; SystemID: ");
212                 sbuffer.append(systemID);
213             }
214
215             if (0 != line) {
216                 sbuffer.append("; Line#: ");
217                 sbuffer.append(line);
218             }
219
220             if (0 != column) {
221                 sbuffer.append("; Column#: ");
222                 sbuffer.append(column);
223             }
224         }
225
226         return sbuffer.toString();
227     }
228
229     /**
230      * Get the location information as a string.
231      *
232      * @return A string with location info, or null
233      * if there is no location information.
234      */

235     public String JavaDoc getLocationAsString() {
236
237         if (null != locator) {
238             StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
239             String JavaDoc systemID = locator.getSystemId();
240             int line = locator.getLineNumber();
241             int column = locator.getColumnNumber();
242
243             if (null != systemID) {
244                 sbuffer.append("; SystemID: ");
245                 sbuffer.append(systemID);
246             }
247
248             if (0 != line) {
249                 sbuffer.append("; Line#: ");
250                 sbuffer.append(line);
251             }
252
253             if (0 != column) {
254                 sbuffer.append("; Column#: ");
255                 sbuffer.append(column);
256             }
257
258             return sbuffer.toString();
259         } else {
260             return null;
261         }
262     }
263
264     /**
265      * Print the the trace of methods from where the error
266      * originated. This will trace all nested exception
267      * objects, as well as this object.
268      */

269     public void printStackTrace() {
270         printStackTrace(new java.io.PrintWriter JavaDoc(System.err, true));
271     }
272
273     /**
274      * Print the the trace of methods from where the error
275      * originated. This will trace all nested exception
276      * objects, as well as this object.
277      * @param s The stream where the dump will be sent to.
278      */

279     public void printStackTrace(java.io.PrintStream JavaDoc s) {
280         printStackTrace(new java.io.PrintWriter JavaDoc(s));
281     }
282
283     /**
284      * Print the the trace of methods from where the error
285      * originated. This will trace all nested exception
286      * objects, as well as this object.
287      * @param s The writer where the dump will be sent to.
288      */

289     public void printStackTrace(java.io.PrintWriter JavaDoc s) {
290
291         if (s == null) {
292             s = new java.io.PrintWriter JavaDoc(System.err, true);
293         }
294
295         try {
296             String JavaDoc locInfo = getLocationAsString();
297
298             if (null != locInfo) {
299                 s.println(locInfo);
300             }
301
302             super.printStackTrace(s);
303         } catch (Throwable JavaDoc e) {}
304
305         Throwable JavaDoc exception = getException();
306
307         for (int i = 0; (i < 10) && (null != exception); i++) {
308             s.println("---------");
309
310             try {
311                 if (exception instanceof TransformerException JavaDoc) {
312                     String JavaDoc locInfo =
313                         ((TransformerException JavaDoc) exception)
314                             .getLocationAsString();
315
316                     if (null != locInfo) {
317                         s.println(locInfo);
318                     }
319                 }
320
321                 exception.printStackTrace(s);
322             } catch (Throwable JavaDoc e) {
323                 s.println("Could not print stack trace...");
324             }
325
326             try {
327                 Method JavaDoc meth =
328                     ((Object JavaDoc) exception).getClass().getMethod("getException",
329                         null);
330
331                 if (null != meth) {
332                     Throwable JavaDoc prev = exception;
333
334                     exception = (Throwable JavaDoc) meth.invoke(exception, null);
335
336                     if (prev == exception) {
337                         break;
338                     }
339                 } else {
340                     exception = null;
341                 }
342             } catch (InvocationTargetException JavaDoc ite) {
343                 exception = null;
344             } catch (IllegalAccessException JavaDoc iae) {
345                 exception = null;
346             } catch (NoSuchMethodException JavaDoc nsme) {
347                 exception = null;
348             }
349         }
350         // insure output is written
351
s.flush();
352     }
353 }
354
Popular Tags