KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > xml > XMLException


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

18 package org.apache.batik.xml;
19
20 /**
21  * This class encapsulates a general XML error or warning.
22  *
23  * <p>This class can contain basic error or warning information from
24  * either the parser or the application.
25  *
26  * <p>If the application needs to pass through other types of
27  * exceptions, it must wrap those exceptions in a XMLException.
28  *
29  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
30  * @version $Id: XMLException.java,v 1.3 2004/08/18 07:15:59 vhardy Exp $
31  */

32 public class XMLException extends RuntimeException JavaDoc {
33
34     /**
35      * @serial The embedded exception if tunnelling, or null.
36      */

37     protected Exception JavaDoc exception;
38
39     /**
40      * Creates a new XMLException.
41      * @param message The error or warning message.
42      */

43     public XMLException (String JavaDoc message) {
44     super(message);
45     exception = null;
46     }
47     
48     /**
49      * Creates a new XMLException wrapping an existing exception.
50      *
51      * <p>The existing exception will be embedded in the new
52      * one, and its message will become the default message for
53      * the XMLException.
54      * @param e The exception to be wrapped in a XMLException.
55      */

56     public XMLException (Exception JavaDoc e) {
57     exception = e;
58     }
59     
60     /**
61      * Creates a new XMLException from an existing exception.
62      *
63      * <p>The existing exception will be embedded in the new
64      * one, but the new exception will have its own message.
65      * @param message The detail message.
66      * @param e The exception to be wrapped in a SAXException.
67      */

68     public XMLException (String JavaDoc message, Exception JavaDoc e) {
69     super(message);
70     exception = e;
71     }
72     
73     /**
74      * Return a detail message for this exception.
75      *
76      * <p>If there is a embedded exception, and if the XMLException
77      * has no detail message of its own, this method will return
78      * the detail message from the embedded exception.
79      * @return The error or warning message.
80      */

81     public String JavaDoc getMessage () {
82     String JavaDoc message = super.getMessage();
83     
84     if (message == null && exception != null) {
85         return exception.getMessage();
86     } else {
87         return message;
88     }
89     }
90     
91     /**
92      * Return the embedded exception, if any.
93      * @return The embedded exception, or null if there is none.
94      */

95     public Exception JavaDoc getException () {
96     return exception;
97     }
98
99     /**
100      * Prints this <code>Exception</code> and its backtrace to the
101      * standard error stream.
102      */

103     public void printStackTrace() {
104         if (exception == null) {
105             super.printStackTrace();
106         } else {
107             synchronized (System.err) {
108                 System.err.println(this);
109                 super.printStackTrace();
110             }
111         }
112     }
113
114     /**
115      * Prints this <code>Exception</code> and its backtrace to the
116      * specified print stream.
117      *
118      * @param s <code>PrintStream</code> to use for output
119      */

120     public void printStackTrace(java.io.PrintStream JavaDoc s) {
121         if (exception == null) {
122             super.printStackTrace(s);
123         } else {
124             synchronized (s) {
125                 s.println(this);
126                 super.printStackTrace();
127             }
128         }
129     }
130
131     /**
132      * Prints this <code>Exception</code> and its backtrace to the specified
133      * print writer.
134      *
135      * @param s <code>PrintWriter</code> to use for output
136      */

137     public void printStackTrace(java.io.PrintWriter JavaDoc s) {
138         if (exception == null) {
139             super.printStackTrace(s);
140         } else {
141             synchronized (s) {
142                 s.println(this);
143                 super.printStackTrace(s);
144             }
145         }
146     }
147 }
148
Popular Tags