001: /*
002: * $Id: TransformerException.java,v 1.4 2001/11/02 22:07:45 db Exp $
003: * Copyright (C) 2001 Andrew Selkirk
004: * Copyright (C) 2001 David Brownell
005: *
006: * This file is part of GNU JAXP, a library.
007: *
008: * GNU JAXP is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNU JAXP is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: * As a special exception, if you link this library with other files to
023: * produce an executable, this library does not by itself cause the
024: * resulting executable to be covered by the GNU General Public License.
025: * This exception does not however invalidate any other reasons why the
026: * executable file might be covered by the GNU General Public License.
027: */
028: package javax.xml.transform;
029:
030: // Imports
031: import java.io.*;
032:
033: /**
034: * Encapsulates a problem exposed during a transformation.
035: * @author Andrew Selkirk, David Brownell
036: * @version 1.0
037: */
038: public class TransformerException extends Exception {
039:
040: //-------------------------------------------------------------
041: // Variables --------------------------------------------------
042: //-------------------------------------------------------------
043:
044: private SourceLocator locator = null;
045: private Throwable containedException = null;
046: private boolean causeKnown;
047:
048: //-------------------------------------------------------------
049: // Initialization ---------------------------------------------
050: //-------------------------------------------------------------
051:
052: public TransformerException(String msg) {
053: super (msg);
054: }
055:
056: public TransformerException(Throwable cause) {
057: super ();
058: initCause(cause);
059: }
060:
061: public TransformerException(String msg, Throwable cause) {
062: super (msg);
063: initCause(cause);
064: }
065:
066: public TransformerException(String msg, SourceLocator locator) {
067: super (msg);
068: setLocator(locator);
069: }
070:
071: public TransformerException(String msg, SourceLocator locator,
072: Throwable cause) {
073: super (msg);
074: setLocator(locator);
075: initCause(cause);
076: }
077:
078: //-------------------------------------------------------------
079: // Methods ----------------------------------------------------
080: //-------------------------------------------------------------
081:
082: /**
083: * Returns the root cause of this exception,
084: * or null if none is known.
085: */
086: public Throwable getCause() {
087: return containedException;
088: }
089:
090: /**
091: * Synonym for {@link #getCause}.
092: */
093: public Throwable getException() {
094: return containedException;
095: }
096:
097: /**
098: * Returns a readable version of the locator info, or null
099: * if there is no locator.
100: */
101: public String getLocationAsString() {
102: if (locator == null)
103: return null;
104:
105: StringBuffer retval = new StringBuffer();
106:
107: if (locator.getPublicId() != null) {
108: retval.append("public='");
109: retval.append(locator.getPublicId());
110: retval.append("' ");
111: }
112: if (locator.getSystemId() != null) {
113: retval.append("uri='");
114: retval.append(locator.getSystemId());
115: retval.append("' ");
116: }
117: if (locator.getLineNumber() != -1) {
118: retval.append("line=");
119: retval.append(locator.getLineNumber());
120: retval.append(" ");
121: }
122: if (locator.getColumnNumber() != -1) {
123: retval.append("column=");
124: retval.append(locator.getColumnNumber());
125: //retval.append (" ");
126: }
127: return retval.toString();
128: }
129:
130: public SourceLocator getLocator() {
131: return locator;
132: }
133:
134: /**
135: * Returns this exception's message, with readable location
136: * information appended if it is available.
137: */
138: public String getMessageAndLocation() {
139: if (locator == null)
140: return getMessage();
141: return getMessage() + ": " + getLocationAsString();
142: }
143:
144: /**
145: * Records the root cause of this exception; may be
146: * called only once, normally during initialization.
147: */
148: public synchronized Throwable initCause(Throwable cause) {
149: if (cause == this )
150: throw new IllegalArgumentException();
151: if (containedException != null)
152: throw new IllegalStateException();
153: containedException = cause;
154: causeKnown = true;
155: // FIXME: spec implies "this" may be the right value; another bug?
156: return cause;
157: }
158:
159: public void printStackTrace() {
160: printStackTrace(System.out);
161: }
162:
163: public void printStackTrace(PrintStream stream) {
164: PrintWriter out = new PrintWriter(
165: new OutputStreamWriter(stream));
166: printStackTrace(out);
167: out.flush();
168: }
169:
170: public void printStackTrace(PrintWriter writer) {
171: if (containedException != null) {
172: containedException.printStackTrace(writer);
173: }
174: super .printStackTrace(writer);
175: }
176:
177: public void setLocator(SourceLocator location) {
178: locator = location;
179: }
180: }
|