001: /* XMLException.java NanoXML/Java
002: *
003: * $Revision: 1.17 $
004: * $Date: 2005/01/05 17:20:04 $
005: * $Name: $
006: *
007: * This file is part of NanoXML 2 for Java.
008: * Copyright (C) 2000-2002 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
036: * XML data.
037: *
038: * @author Marc De Scheemaecker
039: * @version $Name: $, $Revision: 1.17 $
040: */
041: public class XMLException extends Exception {
042:
043: /**
044: * The message of the exception.
045: */
046: private String msg;
047:
048: /**
049: * The system ID of the XML data where the exception occurred.
050: */
051: private String systemID;
052:
053: /**
054: * The line number in the XML data where the exception occurred.
055: */
056: private int lineNr;
057:
058: /**
059: * Encapsulated exception.
060: */
061: private Exception encapsulatedException;
062:
063: /**
064: * Creates a new exception.
065: *
066: * @param msg the message of the exception.
067: */
068: public XMLException(String msg) {
069: this (null, -1, null, msg, false);
070: }
071:
072: /**
073: * Creates a new exception.
074: *
075: * @param e the encapsulated exception.
076: */
077: public XMLException(Exception e) {
078: this (null, -1, e, "Nested Exception", false);
079: }
080:
081: /**
082: * Creates a new exception.
083: *
084: * @param systemID the system ID of the XML data where the exception
085: * occurred
086: * @param lineNr the line number in the XML data where the exception
087: * occurred.
088: * @param e the encapsulated exception.
089: */
090: public XMLException(String systemID, int lineNr, Exception e) {
091: this (systemID, lineNr, e, "Nested Exception", true);
092: }
093:
094: /**
095: * Creates a new exception.
096: *
097: * @param systemID the system ID of the XML data where the exception
098: * occurred
099: * @param lineNr the line number in the XML data where the exception
100: * occurred.
101: * @param msg the message of the exception.
102: */
103: public XMLException(String systemID, int lineNr, String msg) {
104: this (systemID, lineNr, null, msg, true);
105: }
106:
107: /**
108: * Creates a new exception.
109: *
110: * @param systemID the system ID from where the data came
111: * @param lineNr the line number in the XML data where the exception
112: * occurred.
113: * @param e the encapsulated exception.
114: * @param msg the message of the exception.
115: * @param reportParams true if the systemID, lineNr and e params need to be
116: * appended to the message
117: */
118: public XMLException(String systemID, int lineNr, Exception e,
119: String msg, boolean reportParams) {
120: super (XMLException.buildMessage(systemID, lineNr, e, msg,
121: reportParams));
122: this .systemID = systemID;
123: this .lineNr = lineNr;
124: this .encapsulatedException = e;
125: this .msg = XMLException.buildMessage(systemID, lineNr, e, msg,
126: reportParams);
127: }
128:
129: /**
130: * Builds the exception message
131: *
132: * @param systemID the system ID from where the data came
133: * @param lineNr the line number in the XML data where the exception
134: * occurred.
135: * @param e the encapsulated exception.
136: * @param msg the message of the exception.
137: * @param reportParams true if the systemID, lineNr and e params need to be
138: * appended to the message
139: */
140: private static String buildMessage(String systemID, int lineNr,
141: Exception e, String msg, boolean reportParams) {
142: String str = msg;
143:
144: if (reportParams) {
145: if (systemID != null) {
146: str += ", SystemID='" + systemID + "'";
147: }
148:
149: if (lineNr >= 0) {
150: str += ", Line=" + lineNr;
151: }
152:
153: if (e != null) {
154: str += ", Exception: " + e;
155: }
156: }
157:
158: return str;
159: }
160:
161: /**
162: * Cleans up the object when it's destroyed.
163: */
164: protected void finalize() throws Throwable {
165: this .systemID = null;
166: this .encapsulatedException = null;
167: super .finalize();
168: }
169:
170: /**
171: * Returns the system ID of the XML data where the exception occurred.
172: * If there is no system ID known, null is returned.
173: */
174: public String getSystemID() {
175: return this .systemID;
176: }
177:
178: /**
179: * Returns the line number in the XML data where the exception occurred.
180: * If there is no line number known, -1 is returned.
181: */
182: public int getLineNr() {
183: return this .lineNr;
184: }
185:
186: /**
187: * Returns the encapsulated exception, or null if no exception is
188: * encapsulated.
189: */
190: public Exception getException() {
191: return this .encapsulatedException;
192: }
193:
194: /**
195: * Dumps the exception stack to a print writer.
196: *
197: * @param writer the print writer
198: */
199: public void printStackTrace(PrintWriter writer) {
200: super .printStackTrace(writer);
201:
202: if (this .encapsulatedException != null) {
203: writer.println("*** Nested Exception:");
204: this .encapsulatedException.printStackTrace(writer);
205: }
206: }
207:
208: /**
209: * Dumps the exception stack to an output stream.
210: *
211: * @param stream the output stream
212: */
213: public void printStackTrace(PrintStream stream) {
214: super .printStackTrace(stream);
215:
216: if (this .encapsulatedException != null) {
217: stream.println("*** Nested Exception:");
218: this .encapsulatedException.printStackTrace(stream);
219: }
220: }
221:
222: /**
223: * Dumps the exception stack to System.err.
224: */
225: public void printStackTrace() {
226: super .printStackTrace();
227:
228: if (this .encapsulatedException != null) {
229: System.err.println("*** Nested Exception:");
230: this .encapsulatedException.printStackTrace();
231: }
232: }
233:
234: /**
235: * Returns a string representation of the exception.
236: */
237: public String toString() {
238: return this.msg;
239: }
240:
241: }
|