    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, final Locator locator) {
    127            super(s, e);
    128            fillLocation(locator);
    129        }
    130
    131        /**
    132         * Modifies the message to give more detailed location information.
    133         *
    134         * @return the modified exception message.
    135         */
    136        public String getMessage() {
    137            final StringBuffer message = new StringBuffer(String.valueOf(super.getMessage()));
    138            message.append(" [Location: Line=");
    139            message.append(this.line);
    140            message.append(" Column=");
    141            message.append(this.column);
    142            message.append("] ");
    143            return message.toString();
    144        }
    145
    146        /**
    147         * Fills the location with the given locator.
    148         *
    149         * @param locator the locator or null.
    150         */
    151        protected void fillLocation (final Locator locator) {
    152            if (locator == null) {
    153                this.line = -1;
    154                this.column = -1;
    155            }
    156            else {
    157                this.line = locator.getLineNumber();
    158                this.column = locator.getColumnNumber();
    159            }
    160        }
    161
    162        /**
    163         * Returns the line of the parse position where the error occured.
    164         *
    165         * @return the line number or -1 if not known.
    166         */
    167        public int getLine() {
    168            return this.line;
    169        }
    170
    171        /**
    172         * Returns the column of the parse position where the error occured.
    173         *
    174         * @return the column number or -1 if not known.
    175         */
    176        public int getColumn() {
    177            return this.column;
    178        }
    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    }
    217