001: /* XMLException.java NanoXML/Java
002: *
003: * $Revision: 1421 $
004: * $Date: 2006-03-12 09:32:32 -0700 (Sun, 12 Mar 2006) $
005: * $Name$
006: *
007: * This file is part of NanoXML 2 for Java.
008: * Copyright (C) 2001 Marc De Scheemaecker, All Rights Reserved.
009: *
010: * This software is provided 'as-is', without any express or implied warranty.
011: * In no event will the authors be held liable for any damages arising from the
012: * use of this software.
013: *
014: * Permission is granted to anyone to use this software for any purpose,
015: * including commercial applications, and to alter it and redistribute it
016: * freely, subject to the following restrictions:
017: *
018: * 1. The origin of this software must not be misrepresented; you must not
019: * claim that you wrote the original software. If you use this software in
020: * a product, an acknowledgment in the product documentation would be
021: * appreciated but is not required.
022: *
023: * 2. Altered source versions must be plainly marked as such, and must not be
024: * misrepresented as being the original software.
025: *
026: * 3. This notice may not be removed or altered from any source distribution.
027: */
028:
029: package net.n3.nanoxml;
030:
031: import java.io.PrintStream;
032: import java.io.PrintWriter;
033:
034: /**
035: * An XMLException is thrown when an exception occurred while processing the XML data.
036: *
037: * @author Marc De Scheemaecker
038: * @version $Name$, $Revision: 1421 $
039: */
040: public class XMLException extends Exception {
041:
042: /**
043: *
044: */
045: private static final long serialVersionUID = 3256727298916299056L;
046:
047: /**
048: * The system ID of the XML data where the exception occurred.
049: */
050: private String systemID;
051:
052: /**
053: * The line number in the XML data where the exception occurred.
054: */
055: private int lineNr;
056:
057: /**
058: * Encapsulated exception.
059: */
060: private Exception encapsulatedException;
061:
062: /**
063: * Creates a new exception.
064: *
065: * @param msg the message of the exception.
066: */
067: public XMLException(String msg) {
068: this (null, -1, null, msg, false);
069: }
070:
071: /**
072: * Creates a new exception.
073: *
074: * @param e the encapsulated exception.
075: */
076: public XMLException(Exception e) {
077: this (null, -1, e, e.getMessage(), false);
078: }
079:
080: /**
081: * Creates a new exception.
082: *
083: * @param systemID the system ID of the XML data where the exception occurred
084: * @param lineNr the line number in the XML data where the exception occurred.
085: * @param e the encapsulated exception.
086: */
087: public XMLException(String systemID, int lineNr, Exception e) {
088: this (systemID, lineNr, e, "Nested Exception", true);
089: }
090:
091: /**
092: * Creates a new exception.
093: *
094: * @param systemID the system ID of the XML data where the exception occurred
095: * @param lineNr the line number in the XML data where the exception occurred.
096: * @param msg the message of the exception.
097: */
098: public XMLException(String systemID, int lineNr, String msg) {
099: this (systemID, lineNr, null, msg, true);
100: }
101:
102: /**
103: * Creates a new exception.
104: *
105: * @param systemID the system ID from where the data came
106: * @param lineNr the line number in the XML data where the exception occurred.
107: * @param e the encapsulated exception.
108: * @param msg the message of the exception.
109: * @param reportParams true if the systemID, lineNr and e params need to be appended to the
110: * message
111: */
112: public XMLException(String systemID, int lineNr, Exception e,
113: String msg, boolean reportParams) {
114: super (
115: msg
116: + (reportParams ? (((systemID == null) ? ""
117: : (", SystemID='" + systemID + "'"))
118: + ((lineNr == -1) ? ""
119: : (", Line=" + lineNr)) + ((e == null) ? ""
120: : (", Exception: " + e)))
121: : ""));
122: this .systemID = systemID;
123: this .lineNr = lineNr;
124: this .encapsulatedException = e;
125: }
126:
127: /**
128: * Cleans up the object when it's destroyed.
129: */
130: protected void finalize() throws Throwable {
131: this .systemID = null;
132: this .encapsulatedException = null;
133: super .finalize();
134: }
135:
136: /**
137: * Returns the system ID of the XML data where the exception occurred. If there is no system ID
138: * known, null is returned.
139: */
140: public String getSystemID() {
141: return this .systemID;
142: }
143:
144: /**
145: * Returns the line number in the XML data where the exception occurred. If there is no line
146: * number known, -1 is returned.
147: */
148: public int getLineNr() {
149: return this .lineNr;
150: }
151:
152: /**
153: * Returns the encapsulated exception, or null if no exception is encapsulated.
154: */
155: public Exception getException() {
156: return this .encapsulatedException;
157: }
158:
159: /**
160: * Dumps the exception stack to a print writer.
161: *
162: * @param writer the print writer
163: */
164: public void printStackTrace(PrintWriter writer) {
165: if (this .encapsulatedException != null) {
166: this .encapsulatedException.printStackTrace(writer);
167: }
168:
169: super .printStackTrace(writer);
170: }
171:
172: /**
173: * Dumps the exception stack to an output stream.
174: *
175: * @param stream the output stream
176: */
177: public void printStackTrace(PrintStream stream) {
178: if (this .encapsulatedException != null) {
179: this .encapsulatedException.printStackTrace(stream);
180: }
181:
182: super .printStackTrace(stream);
183: }
184:
185: /**
186: * Dumps the exception stack to System.err.
187: */
188: public void printStackTrace() {
189: if (this.encapsulatedException != null) {
190: this.encapsulatedException.printStackTrace();
191: }
192:
193: super.printStackTrace();
194: }
195:
196: }
|