summaryrefslogtreecommitdiff
path: root/external/jaxp/source/javax/xml/transform/TransformerException.java
blob: 58fcf77e87c3179a6699d2d924024dfa73bcc8c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * $Id: TransformerException.java,v 1.1.1.1 2003-02-01 02:10:28 cbj Exp $
 * Copyright (C) 2001 Andrew Selkirk
 * Copyright (C) 2001 David Brownell
 * 
 * This file is part of GNU JAXP, a library.
 *
 * GNU JAXP is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * GNU JAXP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * As a special exception, if you link this library with other files to
 * produce an executable, this library does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * This exception does not however invalidate any other reasons why the
 * executable file might be covered by the GNU General Public License. 
 */
package javax.xml.transform;

// Imports
import java.io.*;

/**
 * Encapsulates a problem exposed during a transformation.
 * @author	Andrew Selkirk, David Brownell
 * @version	1.0
 */
public class TransformerException extends Exception {

	//-------------------------------------------------------------
	// Variables --------------------------------------------------
	//-------------------------------------------------------------

	private	SourceLocator	locator			= null;
	private Throwable	containedException	= null;
	private boolean		causeKnown;


	//-------------------------------------------------------------
	// Initialization ---------------------------------------------
	//-------------------------------------------------------------

	public TransformerException(String msg) {
		super(msg);
	}

	public TransformerException(Throwable cause) {
		super();
		initCause (cause);
	}

	public TransformerException(String msg, Throwable cause) {
		super(msg);
		initCause (cause);
	}

	public TransformerException(String msg, SourceLocator locator) {
		super(msg);
		setLocator (locator);
	}

	public TransformerException(String msg, SourceLocator locator, 
				Throwable cause) {
		super(msg);
		setLocator (locator);
		initCause (cause);
	}


	//-------------------------------------------------------------
	// Methods ----------------------------------------------------
	//-------------------------------------------------------------

	/**
	 * Returns the root cause of this exception,
	 * or null if none is known.
	 */
	public Throwable getCause() {
		return containedException;
	}

	/**
	 * Synonym for {@link #getCause}.
	 */
	public Throwable getException() {
		return containedException;
	}

	/**
	 * Returns a readable version of the locator info, or null
	 * if there is no locator.
	 */
	public String getLocationAsString() {
	    if (locator == null)
		return null;

	    StringBuffer	retval = new StringBuffer ();

	    if (locator.getPublicId () != null) {
		retval.append ("public='");
		retval.append (locator.getPublicId ());
		retval.append ("' ");
	    }
	    if (locator.getSystemId () != null) {
		retval.append ("uri='");
		retval.append (locator.getSystemId ());
		retval.append ("' ");
	    }
	    if (locator.getLineNumber () != -1) {
		retval.append ("line=");
		retval.append (locator.getLineNumber ());
		retval.append (" ");
	    }
	    if (locator.getColumnNumber () != -1) {
		retval.append ("column=");
		retval.append (locator.getColumnNumber ());
		//retval.append (" ");
	    }
	    return retval.toString ();
	}

	public SourceLocator getLocator() {
		return locator;
	}

	/**
	 * Returns this exception's message, with readable location
	 * information appended if it is available.
	 */
	public String getMessageAndLocation() {
	    if (locator == null)
		return getMessage ();
	    return getMessage () + ": " + getLocationAsString ();
	}

	/**
	 * Records the root cause of this exception; may be
	 * called only once, normally during initialization.
	 */
	public synchronized Throwable initCause(Throwable cause) {
	    if (cause == this)
		throw new IllegalArgumentException ();
	    if (containedException != null)
		throw new IllegalStateException ();
	    containedException = cause;
	    causeKnown = true;
// FIXME: spec implies "this" may be the right value; another bug?
	    return cause;
	}


	public void printStackTrace() {
		printStackTrace(System.out);
	}

	public void printStackTrace(PrintStream stream) {
		PrintWriter out = new PrintWriter(
			new OutputStreamWriter(stream));
		printStackTrace(out);
		out.flush ();
	}

	public void printStackTrace(PrintWriter writer) {
		if (containedException != null) {
			containedException.printStackTrace(writer);
		}
		super.printStackTrace(writer);
	}

	public void setLocator (SourceLocator location) {
		locator = location;
	}
}