author | thartmann |
Fri, 24 Aug 2018 08:17:23 +0200 | |
changeset 51514 | 1e332d63bd96 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
46005 | 2 |
* Copyright (c) 2000, 2017, 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 |
package javax.xml.transform; |
|
27 |
||
28 |
import java.lang.reflect.Method; |
|
29 |
import java.lang.reflect.InvocationTargetException; |
|
46005 | 30 |
import java.security.AccessControlContext; |
31 |
import java.security.AccessController; |
|
32 |
import java.security.CodeSigner; |
|
33 |
import java.security.CodeSource; |
|
34 |
import java.security.PermissionCollection; |
|
35 |
import java.security.Permissions; |
|
36 |
import java.security.PrivilegedAction; |
|
37 |
import java.security.ProtectionDomain; |
|
38 |
import java.util.Objects; |
|
12005 | 39 |
|
40 |
/** |
|
46005 | 41 |
* This class specifies an exceptional condition that occurred |
12005 | 42 |
* during the transformation process. |
25262 | 43 |
* |
44 |
* @since 1.4 |
|
12005 | 45 |
*/ |
46 |
public class TransformerException extends Exception { |
|
47 |
||
46006
20a06a196ed5
8176731: JCK tests in api/javax_xml/transform/ spec conformance started failing after 8172469
aefimov
parents:
46005
diff
changeset
|
48 |
private static final long serialVersionUID = 975798773772956428L; |
20a06a196ed5
8176731: JCK tests in api/javax_xml/transform/ spec conformance started failing after 8172469
aefimov
parents:
46005
diff
changeset
|
49 |
|
46005 | 50 |
/** Field locator specifies where the error occurred */ |
12005 | 51 |
SourceLocator locator; |
52 |
||
53 |
/** |
|
54 |
* Method getLocator retrieves an instance of a SourceLocator |
|
46005 | 55 |
* object that specifies where an error occurred. |
12005 | 56 |
* |
57 |
* @return A SourceLocator object, or null if none was specified. |
|
58 |
*/ |
|
59 |
public SourceLocator getLocator() { |
|
46005 | 60 |
return this.locator; |
12005 | 61 |
} |
62 |
||
63 |
/** |
|
64 |
* Method setLocator sets an instance of a SourceLocator |
|
46005 | 65 |
* object that specifies where an error occurred. |
12005 | 66 |
* |
67 |
* @param location A SourceLocator object, or null to clear the location. |
|
68 |
*/ |
|
69 |
public void setLocator(SourceLocator location) { |
|
46005 | 70 |
this.locator = location; |
12005 | 71 |
} |
72 |
||
73 |
/** Field containedException specifies a wrapped exception. May be null. */ |
|
74 |
Throwable containedException; |
|
75 |
||
76 |
/** |
|
77 |
* This method retrieves an exception that this exception wraps. |
|
78 |
* |
|
79 |
* @return An Throwable object, or null. |
|
80 |
* @see #getCause |
|
81 |
*/ |
|
82 |
public Throwable getException() { |
|
83 |
return containedException; |
|
84 |
} |
|
85 |
||
86 |
/** |
|
87 |
* Returns the cause of this throwable or <code>null</code> if the |
|
88 |
* cause is nonexistent or unknown. (The cause is the throwable that |
|
89 |
* caused this throwable to get thrown.) |
|
46005 | 90 |
* @return the cause, or null if unknown |
12005 | 91 |
*/ |
46005 | 92 |
@Override |
12005 | 93 |
public Throwable getCause() { |
94 |
||
95 |
return ((containedException == this) |
|
96 |
? null |
|
97 |
: containedException); |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Initializes the <i>cause</i> of this throwable to the specified value. |
|
102 |
* (The cause is the throwable that caused this throwable to get thrown.) |
|
103 |
* |
|
104 |
* <p>This method can be called at most once. It is generally called from |
|
105 |
* within the constructor, or immediately after creating the |
|
106 |
* throwable. If this throwable was created |
|
107 |
* with {@link #TransformerException(Throwable)} or |
|
108 |
* {@link #TransformerException(String,Throwable)}, this method cannot be called |
|
109 |
* even once. |
|
110 |
* |
|
111 |
* @param cause the cause (which is saved for later retrieval by the |
|
112 |
* {@link #getCause()} method). (A <code>null</code> value is |
|
113 |
* permitted, and indicates that the cause is nonexistent or |
|
114 |
* unknown.) |
|
115 |
* @return a reference to this <code>Throwable</code> instance. |
|
116 |
* @throws IllegalArgumentException if <code>cause</code> is this |
|
117 |
* throwable. (A throwable cannot |
|
118 |
* be its own cause.) |
|
119 |
* @throws IllegalStateException if this throwable was |
|
120 |
* created with {@link #TransformerException(Throwable)} or |
|
121 |
* {@link #TransformerException(String,Throwable)}, or this method has already |
|
122 |
* been called on this throwable. |
|
123 |
*/ |
|
46005 | 124 |
@Override |
12005 | 125 |
public synchronized Throwable initCause(Throwable cause) { |
126 |
||
31497 | 127 |
// TransformerException doesn't set its cause (probably |
128 |
// because it predates initCause()) - and we may not want |
|
129 |
// to change this since Exceptions are serializable... |
|
130 |
// But this also leads to the broken code in printStackTrace |
|
131 |
// below... |
|
132 |
||
12005 | 133 |
if (this.containedException != null) { |
134 |
throw new IllegalStateException("Can't overwrite cause"); |
|
135 |
} |
|
136 |
||
137 |
if (cause == this) { |
|
138 |
throw new IllegalArgumentException( |
|
139 |
"Self-causation not permitted"); |
|
140 |
} |
|
141 |
||
142 |
this.containedException = cause; |
|
143 |
||
144 |
return this; |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Create a new TransformerException. |
|
149 |
* |
|
150 |
* @param message The error or warning message. |
|
151 |
*/ |
|
152 |
public TransformerException(String message) { |
|
46005 | 153 |
this(message, null, null); |
12005 | 154 |
} |
155 |
||
156 |
/** |
|
157 |
* Create a new TransformerException wrapping an existing exception. |
|
158 |
* |
|
159 |
* @param e The exception to be wrapped. |
|
160 |
*/ |
|
161 |
public TransformerException(Throwable e) { |
|
46005 | 162 |
this(null, null, e); |
12005 | 163 |
} |
164 |
||
165 |
/** |
|
166 |
* Wrap an existing exception in a TransformerException. |
|
167 |
* |
|
168 |
* <p>This is used for throwing processor exceptions before |
|
169 |
* the processing has started.</p> |
|
170 |
* |
|
171 |
* @param message The error or warning message, or null to |
|
172 |
* use the message from the embedded exception. |
|
173 |
* @param e Any exception |
|
174 |
*/ |
|
175 |
public TransformerException(String message, Throwable e) { |
|
46005 | 176 |
this(message, null, e); |
12005 | 177 |
} |
178 |
||
179 |
/** |
|
180 |
* Create a new TransformerException from a message and a Locator. |
|
181 |
* |
|
182 |
* <p>This constructor is especially useful when an application is |
|
183 |
* creating its own exception from within a DocumentHandler |
|
184 |
* callback.</p> |
|
185 |
* |
|
186 |
* @param message The error or warning message. |
|
187 |
* @param locator The locator object for the error or warning. |
|
188 |
*/ |
|
189 |
public TransformerException(String message, SourceLocator locator) { |
|
46005 | 190 |
this(message, locator, null); |
12005 | 191 |
} |
192 |
||
193 |
/** |
|
194 |
* Wrap an existing exception in a TransformerException. |
|
195 |
* |
|
196 |
* @param message The error or warning message, or null to |
|
197 |
* use the message from the embedded exception. |
|
198 |
* @param locator The locator object for the error or warning. |
|
199 |
* @param e Any exception |
|
200 |
*/ |
|
201 |
public TransformerException(String message, SourceLocator locator, |
|
202 |
Throwable e) { |
|
46005 | 203 |
super(((message == null) || (message.length() == 0)) |
204 |
? ((e == null) ? "" : e.toString()) |
|
205 |
: message); |
|
12005 | 206 |
|
207 |
this.containedException = e; |
|
208 |
this.locator = locator; |
|
209 |
} |
|
210 |
||
211 |
/** |
|
212 |
* Get the error message with location information |
|
213 |
* appended. |
|
214 |
* |
|
215 |
* @return A <code>String</code> representing the error message with |
|
216 |
* location information appended. |
|
217 |
*/ |
|
218 |
public String getMessageAndLocation() { |
|
46005 | 219 |
StringBuilder sbuffer = new StringBuilder(); |
220 |
sbuffer.append(Objects.toString(super.getMessage(), "")); |
|
221 |
sbuffer.append(Objects.toString(getLocationAsString(), "")); |
|
12005 | 222 |
|
223 |
return sbuffer.toString(); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Get the location information as a string. |
|
228 |
* |
|
229 |
* @return A string with location info, or null |
|
230 |
* if there is no location information. |
|
231 |
*/ |
|
232 |
public String getLocationAsString() { |
|
46005 | 233 |
if (locator == null) { |
234 |
return null; |
|
235 |
} |
|
12005 | 236 |
|
46005 | 237 |
if (System.getSecurityManager() == null) { |
238 |
return getLocationString(); |
|
239 |
} else { |
|
240 |
return AccessController.doPrivileged((PrivilegedAction<String>) () -> |
|
241 |
getLocationString(), |
|
242 |
new AccessControlContext(new ProtectionDomain[] {getNonPrivDomain()})); |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Constructs the location string. |
|
248 |
* @return the location string |
|
249 |
*/ |
|
250 |
private String getLocationString() { |
|
251 |
if (locator == null) { |
|
252 |
return null; |
|
253 |
} |
|
254 |
||
255 |
StringBuilder sbuffer = new StringBuilder(); |
|
12005 | 256 |
String systemID = locator.getSystemId(); |
257 |
int line = locator.getLineNumber(); |
|
258 |
int column = locator.getColumnNumber(); |
|
259 |
||
260 |
if (null != systemID) { |
|
261 |
sbuffer.append("; SystemID: "); |
|
262 |
sbuffer.append(systemID); |
|
263 |
} |
|
264 |
||
265 |
if (0 != line) { |
|
266 |
sbuffer.append("; Line#: "); |
|
267 |
sbuffer.append(line); |
|
268 |
} |
|
269 |
||
270 |
if (0 != column) { |
|
271 |
sbuffer.append("; Column#: "); |
|
272 |
sbuffer.append(column); |
|
273 |
} |
|
274 |
||
275 |
return sbuffer.toString(); |
|
276 |
} |
|
277 |
||
278 |
/** |
|
279 |
* Print the the trace of methods from where the error |
|
280 |
* originated. This will trace all nested exception |
|
281 |
* objects, as well as this object. |
|
282 |
*/ |
|
46005 | 283 |
@Override |
12005 | 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 |
*/ |
|
46005 | 294 |
@Override |
12005 | 295 |
public void printStackTrace(java.io.PrintStream s) { |
296 |
printStackTrace(new java.io.PrintWriter(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 |
*/ |
|
46005 | 305 |
@Override |
12005 | 306 |
public void printStackTrace(java.io.PrintWriter s) { |
307 |
||
308 |
if (s == null) { |
|
309 |
s = new java.io.PrintWriter(System.err, true); |
|
310 |
} |
|
311 |
||
312 |
try { |
|
31497 | 313 |
try { |
314 |
String locInfo = getLocationAsString(); |
|
12005 | 315 |
|
31497 | 316 |
if (null != locInfo) { |
317 |
s.println(locInfo); |
|
318 |
} |
|
12005 | 319 |
|
31497 | 320 |
super.printStackTrace(s); |
321 |
} catch (Throwable e) {} |
|
12005 | 322 |
|
31497 | 323 |
Throwable exception = getException(); |
12005 | 324 |
|
31497 | 325 |
for (int i = 0; (i < 10) && (null != exception); i++) { |
326 |
s.println("---------"); |
|
12005 | 327 |
|
31497 | 328 |
try { |
329 |
exception.printStackTrace(s); |
|
330 |
// if exception is a TransformerException it will print |
|
331 |
// its contained exception, so we don't need to redo it here, |
|
332 |
// and we can exit the loop now. |
|
333 |
if (exception instanceof TransformerException) break; |
|
334 |
} catch (Throwable e) { |
|
335 |
s.println("Could not print stack trace..."); |
|
12005 | 336 |
} |
337 |
||
31497 | 338 |
try { |
339 |
// Is this still needed? |
|
340 |
Method meth = exception.getClass().getMethod("getException"); |
|
12005 | 341 |
|
31497 | 342 |
if (null != meth) { |
343 |
Throwable prev = exception; |
|
344 |
||
345 |
exception = (Throwable) meth.invoke(exception, (Object[]) null); |
|
12005 | 346 |
|
31497 | 347 |
if (prev == exception) { |
348 |
break; |
|
349 |
} |
|
350 |
} else { |
|
351 |
exception = null; |
|
12005 | 352 |
} |
46005 | 353 |
} catch (InvocationTargetException | IllegalAccessException |
354 |
| NoSuchMethodException e) { |
|
12005 | 355 |
exception = null; |
356 |
} |
|
357 |
} |
|
31497 | 358 |
} finally { |
359 |
// ensure output is written |
|
360 |
s.flush(); |
|
12005 | 361 |
} |
362 |
} |
|
46005 | 363 |
|
364 |
/** |
|
365 |
* Creates a ProtectionDomain that has no permission. |
|
366 |
* @return a ProtectionDomain |
|
367 |
*/ |
|
368 |
private ProtectionDomain getNonPrivDomain() { |
|
369 |
CodeSource nullSource = new CodeSource(null, (CodeSigner[]) null); |
|
370 |
PermissionCollection noPermission = new Permissions(); |
|
371 |
return new ProtectionDomain(nullSource, noPermission); |
|
12005 | 372 |
} |
46005 | 373 |
} |