1
/* Copyright 2002-2004 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
26
* The generic superclass for all the
27
* checked exceptions thrown in XOM. The general
28
* principle followed is that anything that could
29
* plausibly be detected by testing such as
30
* using spaces in an element name is a runtime exception.
31
* Exceptions that depend on environmental conditions,
32
* such as might occur when parsing an external file,
33
* are checked exceptions, because these depend on variable input,
34
* and thus problems may not all be detected during testing.
37
* @author Elliotte Rusty Harold
41
public class ParsingException extends Exception {
44
private static final long serialVersionUID = -5185450480590174490L;
46
private Throwable cause;
47
private int lineNumber = -1;
48
private int columnNumber = -1;
54
* Creates a new <code>ParsingException</code> with a detail message
55
* and an underlying root cause.
58
* @param message a string indicating the specific problem
59
* @param cause the original cause of this exception
61
public ParsingException(String message, Throwable cause) {
63
this.initCause(cause);
69
* Creates a new <code>ParsingException</code> with a detail message
70
* and an underlying root cause.
73
* @param message a string indicating the specific problem
74
* @param uri the URI of the document that caused this exception
75
* @param cause the original cause of this exception
77
public ParsingException(String message, String uri, Throwable cause) {
80
this.initCause(cause);
86
* Creates a new <code>ParsingException</code> with a detail message
87
* and line and column numbers.
90
* @param message a string indicating the specific problem
91
* @param lineNumber the approximate line number
92
* where the problem occurs
93
* @param columnNumber the approximate column number
94
* where the problem occurs
96
public ParsingException(String message,
97
int lineNumber, int columnNumber) {
98
this(message, null, lineNumber, columnNumber, null);
104
* Creates a new <code>ParsingException</code> with a detail message
105
* and line and column numbers.
108
* @param message a string indicating the specific problem
109
* @param uri the URI of the document that caused this exception
110
* @param lineNumber the approximate line number
111
* where the problem occurs
112
* @param columnNumber the approximate column number
113
* where the problem occurs
115
public ParsingException(String message, String uri,
116
int lineNumber, int columnNumber) {
117
this(message, uri, lineNumber, columnNumber, null);
123
* Creates a new <code>ParsingException</code> with a detail
124
* message, line and column numbers, and an underlying exception.
127
* @param message a string indicating the specific problem
128
* @param uri the URI of the document that caused this exception
129
* @param lineNumber the approximate line number
130
* where the problem occurs
131
* @param columnNumber the approximate column number
132
* where the problem occurs
133
* @param cause the original cause of this exception
135
public ParsingException(String message, String uri, int lineNumber,
136
int columnNumber, Throwable cause) {
138
this.lineNumber = lineNumber;
139
this.columnNumber = columnNumber;
141
this.initCause(cause);
147
* Creates a new <code>ParsingException</code> with a detail
148
* message, line and column numbers, and an underlying exception.
151
* @param message a string indicating the specific problem
152
* @param lineNumber the approximate line number
153
* where the problem occurs
154
* @param columnNumber the approximate column number
155
* where the problem occurs
156
* @param cause the original cause of this exception
158
public ParsingException(String message, int lineNumber,
159
int columnNumber, Throwable cause) {
161
this.lineNumber = lineNumber;
162
this.columnNumber = columnNumber;
163
this.initCause(cause);
169
* Creates a new <code>ParsingException</code> with a detail message.
172
* @param message a string indicating the specific problem
174
public ParsingException(String message) {
181
* Returns the approximate row number of the construct that
182
* caused this exception. If the row number is not known,
186
* @return row number where the exception occurred
188
public int getLineNumber() {
189
return this.lineNumber;
194
* Returns the approximate column number of the construct that
195
* caused this exception. If the column number is not known,
199
* @return column number where the exception occurred
201
public int getColumnNumber() {
202
return this.columnNumber;
208
* Returns the system ID (generally a URL) of the document that
209
* caused this exception. If this is not known, for instance
210
* because the document was parsed from a raw input stream or from
211
* a string, it returns null.
214
* @return the URI of the document that caused this exception
216
public String getURI() {
221
// null is insufficient for determining unset cause.
222
// The cause may be set to null which may not then be reset.
223
private boolean causeSet = false;
227
* Sets the root cause of this exception. This may
228
* only be called once. Subsequent calls throw an
229
* <code>IllegalStateException</code>.
233
* This method is unnecessary in Java 1.4 where it could easily be
234
* inherited from the superclass. However, including it here
235
* allows this method to be used in Java 1.3 and earlier.
238
* @param cause the root cause of this exception
240
* @return this <code>XMLException</code>
242
* @throws IllegalArgumentException if the cause is this exception
243
* (An exception cannot be its own cause.)
244
* @throws IllegalStateException if this method is called twice
246
public final Throwable initCause(Throwable cause) {
249
throw new IllegalStateException("Can't overwrite cause");
251
else if (cause == this) {
252
throw new IllegalArgumentException("Self-causation not permitted");
254
else this.cause = cause;
263
* Returns the underlying exception that caused this exception.
266
* @return the root exception that caused this exception
269
public Throwable getCause() {
276
* Returns a string suitable for display to the developer
277
* summarizing what went wrong where.
280
* @return an exception message suitable for display to a developer
282
public String toString() {
283
StringBuffer result = new StringBuffer(super.toString());
284
if (lineNumber >= 0) {
285
result.append(" at line ");
286
result.append(lineNumber);
287
result.append(", column ");
288
result.append(columnNumber);
291
if (this.uri != null) {
292
result.append(" in ");
295
return result.toString();
b'\\ No newline at end of file'