author | aefimov |
Fri, 09 Feb 2018 14:49:38 +0000 | |
changeset 48837 | a262b919311a |
parent 47216 | 71c04702a3d5 |
child 58247 | 3aef3bccfae3 |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. |
12005 | 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 |
// SAX exception class. |
|
27 |
// http://www.saxproject.org |
|
28 |
// No warranty; no copyright -- use this as you will. |
|
29 |
// $Id: SAXException.java,v 1.3 2004/11/03 22:55:32 jsuttor Exp $ |
|
30 |
||
31 |
package org.xml.sax; |
|
32 |
||
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
33 |
import java.io.IOException; |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
34 |
import java.io.InvalidClassException; |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
35 |
import java.io.ObjectInputStream; |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
36 |
import java.io.ObjectOutputStream; |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
37 |
import java.io.ObjectStreamField; |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
38 |
|
12005 | 39 |
/** |
40 |
* Encapsulate a general SAX error or warning. |
|
41 |
* |
|
42 |
* <blockquote> |
|
43 |
* <em>This module, both source code and documentation, is in the |
|
44 |
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> |
|
45 |
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a> |
|
46 |
* for further information. |
|
47 |
* </blockquote> |
|
48 |
* |
|
49 |
* <p>This class can contain basic error or warning information from |
|
50 |
* either the XML parser or the application: a parser writer or |
|
51 |
* application writer can subclass it to provide additional |
|
52 |
* functionality. SAX handlers may throw this exception or |
|
53 |
* any exception subclassed from it.</p> |
|
54 |
* |
|
55 |
* <p>If the application needs to pass through other types of |
|
56 |
* exceptions, it must wrap those exceptions in a SAXException |
|
57 |
* or an exception derived from a SAXException.</p> |
|
58 |
* |
|
59 |
* <p>If the parser or application needs to include information about a |
|
60 |
* specific location in an XML document, it should use the |
|
61 |
* {@link org.xml.sax.SAXParseException SAXParseException} subclass.</p> |
|
62 |
* |
|
25262 | 63 |
* @since 1.4, SAX 1.0 |
12005 | 64 |
* @author David Megginson |
65 |
* @version 2.0.1 (sax2r2) |
|
66 |
* @see org.xml.sax.SAXParseException |
|
67 |
*/ |
|
68 |
public class SAXException extends Exception { |
|
69 |
||
70 |
||
71 |
/** |
|
72 |
* Create a new SAXException. |
|
73 |
*/ |
|
74 |
public SAXException () |
|
75 |
{ |
|
76 |
super(); |
|
77 |
} |
|
78 |
||
79 |
||
80 |
/** |
|
81 |
* Create a new SAXException. |
|
82 |
* |
|
83 |
* @param message The error or warning message. |
|
84 |
*/ |
|
85 |
public SAXException (String message) { |
|
86 |
super(message); |
|
87 |
} |
|
88 |
||
89 |
||
90 |
/** |
|
91 |
* Create a new SAXException wrapping an existing exception. |
|
92 |
* |
|
93 |
* <p>The existing exception will be embedded in the new |
|
94 |
* one, and its message will become the default message for |
|
95 |
* the SAXException.</p> |
|
96 |
* |
|
97 |
* @param e The exception to be wrapped in a SAXException. |
|
98 |
*/ |
|
99 |
public SAXException (Exception e) |
|
100 |
{ |
|
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
101 |
super(e); |
12005 | 102 |
} |
103 |
||
104 |
||
105 |
/** |
|
106 |
* Create a new SAXException from an existing exception. |
|
107 |
* |
|
108 |
* <p>The existing exception will be embedded in the new |
|
109 |
* one, but the new exception will have its own message.</p> |
|
110 |
* |
|
111 |
* @param message The detail message. |
|
112 |
* @param e The exception to be wrapped in a SAXException. |
|
113 |
*/ |
|
114 |
public SAXException (String message, Exception e) |
|
115 |
{ |
|
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
116 |
super(message, e); |
12005 | 117 |
} |
118 |
||
119 |
||
120 |
/** |
|
121 |
* Return a detail message for this exception. |
|
122 |
* |
|
123 |
* <p>If there is an embedded exception, and if the SAXException |
|
124 |
* has no detail message of its own, this method will return |
|
125 |
* the detail message from the embedded exception.</p> |
|
126 |
* |
|
127 |
* @return The error or warning message. |
|
128 |
*/ |
|
129 |
public String getMessage () |
|
130 |
{ |
|
131 |
String message = super.getMessage(); |
|
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
132 |
Throwable cause = super.getCause(); |
12005 | 133 |
|
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
134 |
if (message == null && cause != null) { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
135 |
return cause.getMessage(); |
12005 | 136 |
} else { |
137 |
return message; |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Return the embedded exception, if any. |
|
143 |
* |
|
144 |
* @return The embedded exception, or null if there is none. |
|
145 |
*/ |
|
146 |
public Exception getException () |
|
147 |
{ |
|
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
148 |
return getExceptionInternal(); |
12005 | 149 |
} |
150 |
||
151 |
/** |
|
152 |
* Return the cause of the exception |
|
153 |
* |
|
154 |
* @return Return the cause of the exception |
|
155 |
*/ |
|
156 |
public Throwable getCause() { |
|
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
157 |
return super.getCause(); |
12005 | 158 |
} |
159 |
||
160 |
/** |
|
161 |
* Override toString to pick up any embedded exception. |
|
162 |
* |
|
163 |
* @return A string representation of this exception. |
|
164 |
*/ |
|
165 |
public String toString () |
|
166 |
{ |
|
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
167 |
Throwable exception = super.getCause(); |
12005 | 168 |
if (exception != null) { |
169 |
return super.toString() + "\n" + exception.toString(); |
|
170 |
} else { |
|
171 |
return super.toString(); |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
||
176 |
||
177 |
////////////////////////////////////////////////////////////////////// |
|
178 |
// Internal state. |
|
179 |
////////////////////////////////////////////////////////////////////// |
|
180 |
||
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
181 |
private static final ObjectStreamField[] serialPersistentFields = { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
182 |
new ObjectStreamField( "exception", Exception.class ) |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
183 |
}; |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
184 |
|
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
185 |
/** |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
186 |
* Writes "exception" field to the stream. |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
187 |
* |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
188 |
* @param out stream used for serialization. |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
189 |
* @throws IOException thrown by <code>ObjectOutputStream</code> |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
190 |
*/ |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
191 |
private void writeObject(ObjectOutputStream out) |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
192 |
throws IOException { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
193 |
ObjectOutputStream.PutField fields = out.putFields(); |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
194 |
fields.put("exception", getExceptionInternal()); |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
195 |
out.writeFields(); |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
196 |
} |
12005 | 197 |
|
198 |
/** |
|
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
199 |
* Reads the "exception" field from the stream. |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
200 |
* And initializes the "exception" if it wasn't |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
201 |
* done before. |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
202 |
* |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
203 |
* @param in stream used for deserialization |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
204 |
* @throws IOException thrown by <code>ObjectInputStream</code> |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
205 |
* @throws ClassNotFoundException thrown by <code>ObjectInputStream</code> |
12005 | 206 |
*/ |
48837
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
207 |
private void readObject(ObjectInputStream in) |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
208 |
throws IOException, ClassNotFoundException { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
209 |
ObjectInputStream.GetField fields = in.readFields(); |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
210 |
Exception exception = (Exception) fields.get("exception", null); |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
211 |
Throwable superCause = super.getCause(); |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
212 |
|
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
213 |
// if super.getCause() and 'exception' fields present then always use |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
214 |
// getCause() value. Otherwise, use 'exception' to initialize cause |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
215 |
if (superCause == null && exception != null) { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
216 |
try { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
217 |
super.initCause(exception); |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
218 |
} catch (IllegalStateException e) { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
219 |
throw new InvalidClassException("Inconsistent state: two causes"); |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
220 |
} |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
221 |
} |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
222 |
} |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
223 |
|
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
224 |
// Internal method to guard against overriding of public getException |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
225 |
// method by SAXException subclasses |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
226 |
private Exception getExceptionInternal() { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
227 |
Throwable cause = super.getCause(); |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
228 |
if (cause instanceof Exception) { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
229 |
return (Exception) cause; |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
230 |
} else { |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
231 |
return null; |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
232 |
} |
a262b919311a
6857903: SAXException.initCause() does not correctly set Exception
aefimov
parents:
47216
diff
changeset
|
233 |
} |
12005 | 234 |
|
235 |
// Added serialVersionUID to preserve binary compatibility |
|
236 |
static final long serialVersionUID = 583241635256073760L; |
|
237 |
} |
|
238 |
||
239 |
// end of SAXException.java |