Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/transform/TransformerException.java
32285 views
/*1* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.transform;2627import java.lang.reflect.Method;28import java.lang.reflect.InvocationTargetException;29import java.security.AccessControlContext;30import java.security.AccessController;31import java.security.CodeSigner;32import java.security.CodeSource;33import java.security.PermissionCollection;34import java.security.Permissions;35import java.security.PrivilegedAction;36import java.security.ProtectionDomain;37import java.util.Objects;3839/**40* This class specifies an exceptional condition that occurred41* during the transformation process.42*/43public class TransformerException extends Exception {4445private static final long serialVersionUID = 975798773772956428L;4647/** Field locator specifies where the error occurred */48SourceLocator locator;4950/**51* Method getLocator retrieves an instance of a SourceLocator52* object that specifies where an error occurred.53*54* @return A SourceLocator object, or null if none was specified.55*/56public SourceLocator getLocator() {57return this.locator;58}5960/**61* Method setLocator sets an instance of a SourceLocator62* object that specifies where an error occurred.63*64* @param location A SourceLocator object, or null to clear the location.65*/66public void setLocator(SourceLocator location) {67this.locator = location;68}6970/** Field containedException specifies a wrapped exception. May be null. */71Throwable containedException;7273/**74* This method retrieves an exception that this exception wraps.75*76* @return An Throwable object, or null.77* @see #getCause78*/79public Throwable getException() {80return containedException;81}8283/**84* Returns the cause of this throwable or <code>null</code> if the85* cause is nonexistent or unknown. (The cause is the throwable that86* caused this throwable to get thrown.)87* @return the cause, or null if unknown88*/89@Override90public Throwable getCause() {9192return ((containedException == this)93? null94: containedException);95}9697/**98* Initializes the <i>cause</i> of this throwable to the specified value.99* (The cause is the throwable that caused this throwable to get thrown.)100*101* <p>This method can be called at most once. It is generally called from102* within the constructor, or immediately after creating the103* throwable. If this throwable was created104* with {@link #TransformerException(Throwable)} or105* {@link #TransformerException(String,Throwable)}, this method cannot be called106* even once.107*108* @param cause the cause (which is saved for later retrieval by the109* {@link #getCause()} method). (A <code>null</code> value is110* permitted, and indicates that the cause is nonexistent or111* unknown.)112* @return a reference to this <code>Throwable</code> instance.113* @throws IllegalArgumentException if <code>cause</code> is this114* throwable. (A throwable cannot115* be its own cause.)116* @throws IllegalStateException if this throwable was117* created with {@link #TransformerException(Throwable)} or118* {@link #TransformerException(String,Throwable)}, or this method has already119* been called on this throwable.120*/121@Override122public synchronized Throwable initCause(Throwable cause) {123124if (this.containedException != null) {125throw new IllegalStateException("Can't overwrite cause");126}127128if (cause == this) {129throw new IllegalArgumentException(130"Self-causation not permitted");131}132133this.containedException = cause;134135return this;136}137138/**139* Create a new TransformerException.140*141* @param message The error or warning message.142*/143public TransformerException(String message) {144this(message, null, null);145}146147/**148* Create a new TransformerException wrapping an existing exception.149*150* @param e The exception to be wrapped.151*/152public TransformerException(Throwable e) {153this(null, null, e);154}155156/**157* Wrap an existing exception in a TransformerException.158*159* <p>This is used for throwing processor exceptions before160* the processing has started.</p>161*162* @param message The error or warning message, or null to163* use the message from the embedded exception.164* @param e Any exception165*/166public TransformerException(String message, Throwable e) {167this(message, null, e);168}169170/**171* Create a new TransformerException from a message and a Locator.172*173* <p>This constructor is especially useful when an application is174* creating its own exception from within a DocumentHandler175* callback.</p>176*177* @param message The error or warning message.178* @param locator The locator object for the error or warning.179*/180public TransformerException(String message, SourceLocator locator) {181this(message, locator, null);182}183184/**185* Wrap an existing exception in a TransformerException.186*187* @param message The error or warning message, or null to188* use the message from the embedded exception.189* @param locator The locator object for the error or warning.190* @param e Any exception191*/192public TransformerException(String message, SourceLocator locator,193Throwable e) {194super(((message == null) || (message.length() == 0))195? ((e == null) ? "" : e.toString())196: message);197198this.containedException = e;199this.locator = locator;200}201202/**203* Get the error message with location information204* appended.205*206* @return A <code>String</code> representing the error message with207* location information appended.208*/209public String getMessageAndLocation() {210StringBuilder sbuffer = new StringBuilder();211sbuffer.append(Objects.toString(super.getMessage(), ""));212sbuffer.append(Objects.toString(getLocationAsString(), ""));213214return sbuffer.toString();215}216217/**218* Get the location information as a string.219*220* @return A string with location info, or null221* if there is no location information.222*/223public String getLocationAsString() {224if (locator == null) {225return null;226}227228if (System.getSecurityManager() == null) {229return getLocationString();230} else {231return (String) AccessController.doPrivileged(232new PrivilegedAction<String>() {233public String run() {234return getLocationString();235}236},237new AccessControlContext(new ProtectionDomain[] {getNonPrivDomain()})238);239}240}241242/**243* Constructs the location string.244* @return the location string245*/246private String getLocationString() {247if (locator == null) {248return null;249}250251StringBuilder sbuffer = new StringBuilder();252String systemID = locator.getSystemId();253int line = locator.getLineNumber();254int column = locator.getColumnNumber();255256if (null != systemID) {257sbuffer.append("; SystemID: ");258sbuffer.append(systemID);259}260261if (0 != line) {262sbuffer.append("; Line#: ");263sbuffer.append(line);264}265266if (0 != column) {267sbuffer.append("; Column#: ");268sbuffer.append(column);269}270271return sbuffer.toString();272}273274/**275* Print the the trace of methods from where the error276* originated. This will trace all nested exception277* objects, as well as this object.278*/279@Override280public void printStackTrace() {281printStackTrace(new java.io.PrintWriter(System.err, true));282}283284/**285* Print the the trace of methods from where the error286* originated. This will trace all nested exception287* objects, as well as this object.288* @param s The stream where the dump will be sent to.289*/290@Override291public void printStackTrace(java.io.PrintStream s) {292printStackTrace(new java.io.PrintWriter(s));293}294295/**296* Print the the trace of methods from where the error297* originated. This will trace all nested exception298* objects, as well as this object.299* @param s The writer where the dump will be sent to.300*/301@Override302public void printStackTrace(java.io.PrintWriter s) {303304if (s == null) {305s = new java.io.PrintWriter(System.err, true);306}307308try {309String locInfo = getLocationAsString();310311if (null != locInfo) {312s.println(locInfo);313}314315super.printStackTrace(s);316} catch (Throwable e) {}317318Throwable exception = getException();319320for (int i = 0; (i < 10) && (null != exception); i++) {321s.println("---------");322323try {324if (exception instanceof TransformerException) {325String locInfo =326((TransformerException) exception)327.getLocationAsString();328329if (null != locInfo) {330s.println(locInfo);331}332}333334exception.printStackTrace(s);335} catch (Throwable e) {336s.println("Could not print stack trace...");337}338339try {340Method meth =341((Object) exception).getClass().getMethod("getException",342(Class[]) null);343344if (null != meth) {345Throwable prev = exception;346347exception = (Throwable) meth.invoke(exception, (Object[]) null);348349if (prev == exception) {350break;351}352} else {353exception = null;354}355} catch (InvocationTargetException | IllegalAccessException356| NoSuchMethodException e) {357exception = null;358}359}360// insure output is written361s.flush();362}363364/**365* Creates a ProtectionDomain that has no permission.366* @return a ProtectionDomain367*/368private ProtectionDomain getNonPrivDomain() {369CodeSource nullSource = new CodeSource(null, (CodeSigner[]) null);370PermissionCollection noPermission = new Permissions();371return new ProtectionDomain(nullSource, noPermission);372}373}374375376