001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------
028: * ParseException.java
029: * -------------------
030: * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: ParseException.java,v 1.4 2005/10/18 13:25:44 mungady Exp $
036: *
037: * Changes
038: * -------------------------
039: * 10.06.2003 : Initial version
040: *
041: */
042:
043: package org.jfree.xml;
044:
045: import java.io.PrintStream;
046: import java.io.PrintWriter;
047:
048: import org.xml.sax.Locator;
049: import org.xml.sax.SAXException;
050:
051: /**
052: * A parse exception.
053: *
054: * @author Thomas Morgner
055: */
056: public class ParseException extends SAXException {
057:
058: /** The line, where the error occured. */
059: private int line;
060:
061: /** The column, where the error occured. */
062: private int column;
063:
064: /**
065: * Creates a new ParseException with the given message.
066: *
067: * @param message the message
068: */
069: public ParseException(final String message) {
070: super (message);
071: fillLocation(null);
072: }
073:
074: /**
075: * Creates a new ParseException with the given root exception.
076: *
077: * @param e the exception
078: */
079: public ParseException(final Exception e) {
080: super (e);
081: fillLocation(null);
082: }
083:
084: /**
085: * Creates a new ParseException with the given message and root exception.
086: *
087: * @param s the message
088: * @param e the exception
089: */
090: public ParseException(final String s, final Exception e) {
091: super (s, e);
092: fillLocation(null);
093: }
094:
095: /**
096: * Creates a new ParseException with the given message and the locator.
097: *
098: * @param message the message
099: * @param locator the locator of the parser
100: */
101: public ParseException(final String message, final Locator locator) {
102: super (message);
103: fillLocation(locator);
104: }
105:
106: /**
107: * Creates a new ParseException with the given root exception
108: * and the locator.
109: *
110: * @param e the exception
111: * @param locator the locator of the parser
112: */
113: public ParseException(final Exception e, final Locator locator) {
114: super (e);
115: fillLocation(locator);
116: }
117:
118: /**
119: * Creates a new ParseException with the given message, root exception
120: * and the locator.
121: *
122: * @param s the message
123: * @param e the exception
124: * @param locator the locator of the parser
125: */
126: public ParseException(final String s, final Exception e,
127: final Locator locator) {
128: super (s, e);
129: fillLocation(locator);
130: }
131:
132: /**
133: * Modifies the message to give more detailed location information.
134: *
135: * @return the modified exception message.
136: */
137: public String getMessage() {
138: final StringBuffer message = new StringBuffer(String
139: .valueOf(super .getMessage()));
140: message.append(" [Location: Line=");
141: message.append(this .line);
142: message.append(" Column=");
143: message.append(this .column);
144: message.append("] ");
145: return message.toString();
146: }
147:
148: /**
149: * Fills the location with the given locator.
150: *
151: * @param locator the locator or null.
152: */
153: protected void fillLocation(final Locator locator) {
154: if (locator == null) {
155: this .line = -1;
156: this .column = -1;
157: } else {
158: this .line = locator.getLineNumber();
159: this .column = locator.getColumnNumber();
160: }
161: }
162:
163: /**
164: * Returns the line of the parse position where the error occured.
165: *
166: * @return the line number or -1 if not known.
167: */
168: public int getLine() {
169: return this .line;
170: }
171:
172: /**
173: * Returns the column of the parse position where the error occured.
174: *
175: * @return the column number or -1 if not known.
176: */
177: public int getColumn() {
178: return this .column;
179: }
180:
181: /**
182: * Prints the stack trace to the specified stream.
183: *
184: * @param stream the output stream.
185: */
186: public void printStackTrace(final PrintStream stream) {
187: super .printStackTrace(stream);
188: if (getException() != null) {
189: stream.println("ParentException: ");
190: getException().printStackTrace(stream);
191: }
192: }
193:
194: /**
195: * Override toString to pick up any embedded exception.
196: *
197: * @return A string representation of this exception.
198: */
199: public String toString() {
200: return getClass().getName() + ": " + getMessage();
201: }
202:
203: /**
204: * Prints the stack trace to the specified writer.
205: *
206: * @param writer the writer.
207: */
208: public void printStackTrace(final PrintWriter writer) {
209: super .printStackTrace(writer);
210: if (getException() != null) {
211: writer.println("ParentException: ");
212: getException().printStackTrace(writer);
213: }
214: }
215:
216: }
|