KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > dtm > DTMException


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: DTMException.java,v 1.7 2004/02/16 23:03:44 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.dtm;
20
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24
25 import javax.xml.transform.SourceLocator JavaDoc;
26
27 import com.sun.org.apache.xml.internal.res.XMLErrorResources;
28 import com.sun.org.apache.xml.internal.res.XMLMessages;
29
30
31 /**
32  * This class specifies an exceptional condition that occured
33  * in the DTM module.
34  */

35 public class DTMException extends RuntimeException JavaDoc {
36
37     /** Field locator specifies where the error occured.
38      * @serial */

39     SourceLocator JavaDoc locator;
40
41     /**
42      * Method getLocator retrieves an instance of a SourceLocator
43      * object that specifies where an error occured.
44      *
45      * @return A SourceLocator object, or null if none was specified.
46      */

47     public SourceLocator JavaDoc getLocator() {
48         return locator;
49     }
50
51     /**
52      * Method setLocator sets an instance of a SourceLocator
53      * object that specifies where an error occured.
54      *
55      * @param location A SourceLocator object, or null to clear the location.
56      */

57     public void setLocator(SourceLocator JavaDoc location) {
58         locator = location;
59     }
60
61     /** Field containedException specifies a wrapped exception. May be null.
62      * @serial */

63     Throwable JavaDoc 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 JavaDoc 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 JavaDoc 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 #DTMException(Throwable)} or
95      * {@link #DTMException(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 <tt>null</tt> 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 #DTMException(Throwable)} or
108      * {@link #DTMException(String,Throwable)}, or this method has already
109      * been called on this throwable.
110      */

111     public synchronized Throwable JavaDoc initCause(Throwable JavaDoc cause) {
112
113         if ((this.containedException == null) && (cause != null)) {
114             throw new IllegalStateException JavaDoc(XMLMessages.createXMLMessage(XMLErrorResources.ER_CANNOT_OVERWRITE_CAUSE, null)); //"Can't overwrite cause");
115
}
116
117         if (cause == this) {
118             throw new IllegalArgumentException JavaDoc(
119                 XMLMessages.createXMLMessage(XMLErrorResources.ER_SELF_CAUSATION_NOT_PERMITTED, null)); //"Self-causation not permitted");
120
}
121
122         this.containedException = cause;
123
124         return this;
125     }
126
127     /**
128      * Create a new DTMException.
129      *
130      * @param message The error or warning message.
131      */

132     public DTMException(String JavaDoc message) {
133
134         super(message);
135
136         this.containedException = null;
137         this.locator = null;
138     }
139
140     /**
141      * Create a new DTMException wrapping an existing exception.
142      *
143      * @param e The exception to be wrapped.
144      */

145     public DTMException(Throwable JavaDoc e) {
146
147         super(e.getMessage());
148
149         this.containedException = e;
150         this.locator = null;
151     }
152
153     /**
154      * Wrap an existing exception in a DTMException.
155      *
156      * <p>This is used for throwing processor exceptions before
157      * the processing has started.</p>
158      *
159      * @param message The error or warning message, or null to
160      * use the message from the embedded exception.
161      * @param e Any exception
162      */

163     public DTMException(String JavaDoc message, Throwable JavaDoc e) {
164
165         super(((message == null) || (message.length() == 0))
166               ? e.getMessage()
167               : message);
168
169         this.containedException = e;
170         this.locator = null;
171     }
172
173     /**
174      * Create a new DTMException from a message and a Locator.
175      *
176      * <p>This constructor is especially useful when an application is
177      * creating its own exception from within a DocumentHandler
178      * callback.</p>
179      *
180      * @param message The error or warning message.
181      * @param locator The locator object for the error or warning.
182      */

183     public DTMException(String JavaDoc message, SourceLocator JavaDoc locator) {
184
185         super(message);
186
187         this.containedException = null;
188         this.locator = locator;
189     }
190
191     /**
192      * Wrap an existing exception in a DTMException.
193      *
194      * @param message The error or warning message, or null to
195      * use the message from the embedded exception.
196      * @param locator The locator object for the error or warning.
197      * @param e Any exception
198      */

199     public DTMException(String JavaDoc message, SourceLocator JavaDoc locator,
200                                 Throwable JavaDoc e) {
201
202         super(message);
203
204         this.containedException = e;
205         this.locator = locator;
206     }
207
208     /**
209      * Get the error message with location information
210      * appended.
211      */

212     public String JavaDoc getMessageAndLocation() {
213
214         StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
215         String JavaDoc message = super.getMessage();
216
217         if (null != message) {
218             sbuffer.append(message);
219         }
220
221         if (null != locator) {
222             String JavaDoc systemID = locator.getSystemId();
223             int line = locator.getLineNumber();
224             int column = locator.getColumnNumber();
225
226             if (null != systemID) {
227                 sbuffer.append("; SystemID: ");
228                 sbuffer.append(systemID);
229             }
230
231             if (0 != line) {
232                 sbuffer.append("; Line#: ");
233                 sbuffer.append(line);
234             }
235
236             if (0 != column) {
237                 sbuffer.append("; Column#: ");
238                 sbuffer.append(column);
239             }
240         }
241
242         return sbuffer.toString();
243     }
244
245     /**
246      * Get the location information as a string.
247      *
248      * @return A string with location info, or null
249      * if there is no location information.
250      */

251     public String JavaDoc getLocationAsString() {
252
253         if (null != locator) {
254             StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
255             String JavaDoc systemID = locator.getSystemId();
256             int line = locator.getLineNumber();
257             int column = locator.getColumnNumber();
258
259             if (null != systemID) {
260                 sbuffer.append("; SystemID: ");
261                 sbuffer.append(systemID);
262             }
263
264             if (0 != line) {
265                 sbuffer.append("; Line#: ");
266                 sbuffer.append(line);
267             }
268
269             if (0 != column) {
270                 sbuffer.append("; Column#: ");
271                 sbuffer.append(column);
272             }
273
274             return sbuffer.toString();
275         } else {
276             return null;
277         }
278     }
279
280     /**
281      * Print the the trace of methods from where the error
282      * originated. This will trace all nested exception
283      * objects, as well as this object.
284      */

285     public void printStackTrace() {
286         printStackTrace(new java.io.PrintWriter JavaDoc(System.err, true));
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      * @param s The stream where the dump will be sent to.
294      */

295     public void printStackTrace(java.io.PrintStream JavaDoc s) {
296         printStackTrace(new java.io.PrintWriter JavaDoc(s));
297     }
298
299     /**
300      * Print the the trace of methods from where the error
301      * originated. This will trace all nested exception
302      * objects, as well as this object.
303      * @param s The writer where the dump will be sent to.
304      */

305     public void printStackTrace(java.io.PrintWriter JavaDoc s) {
306
307         if (s == null) {
308             s = new java.io.PrintWriter JavaDoc(System.err, true);
309         }
310
311         try {
312             String JavaDoc locInfo = getLocationAsString();
313
314             if (null != locInfo) {
315                 s.println(locInfo);
316             }
317
318             super.printStackTrace(s);
319         } catch (Throwable JavaDoc e) {}
320
321         Throwable JavaDoc exception = getException();
322
323         for (int i = 0; (i < 10) && (null != exception); i++) {
324             s.println("---------");
325
326             try {
327                 if (exception instanceof DTMException) {
328                     String JavaDoc locInfo =
329                         ((DTMException) exception)
330                             .getLocationAsString();
331
332                     if (null != locInfo) {
333                         s.println(locInfo);
334                     }
335                 }
336
337                 exception.printStackTrace(s);
338             } catch (Throwable JavaDoc e) {
339                 s.println("Could not print stack trace...");
340             }
341
342             try {
343                 Method JavaDoc meth =
344                     ((Object JavaDoc) exception).getClass().getMethod("getException",
345                         null);
346
347                 if (null != meth) {
348                     Throwable JavaDoc prev = exception;
349
350                     exception = (Throwable JavaDoc) meth.invoke(exception, null);
351
352                     if (prev == exception) {
353                         break;
354                     }
355                 } else {
356                     exception = null;
357                 }
358             } catch (InvocationTargetException JavaDoc ite) {
359                 exception = null;
360             } catch (IllegalAccessException JavaDoc iae) {
361                 exception = null;
362             } catch (NoSuchMethodException JavaDoc nsme) {
363                 exception = null;
364             }
365         }
366     }
367 }
368
Popular Tags