Source for org.jfree.xml.ParseException

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * -------------------
  28:  * ParseException.java
  29:  * -------------------
  30:  * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: ParseException.java,v 1.4 2005/10/18 13:25:44 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------------------------
  39:  * 10.06.2003 : Initial version
  40:  *
  41:  */
  42: 
  43: package org.jfree.xml;
  44: 
  45: import java.io.PrintStream;
  46: import java.io.PrintWriter;
  47: 
  48: import org.xml.sax.Locator;
  49: import org.xml.sax.SAXException;
  50: 
  51: /**
  52:  * A parse exception.
  53:  *
  54:  * @author Thomas Morgner
  55:  */
  56: public class ParseException extends SAXException {
  57: 
  58:     /** The line, where the error occured. */
  59:     private int line;
  60: 
  61:     /** The column, where the error occured. */
  62:     private int column;
  63: 
  64:     /**
  65:      * Creates a new ParseException with the given message.
  66:      *
  67:      * @param message the message
  68:      */
  69:     public ParseException(final String message) {
  70:         super(message);
  71:         fillLocation(null);
  72:     }
  73: 
  74:     /**
  75:      * Creates a new ParseException with the given root exception.
  76:      *
  77:      * @param e the exception
  78:      */
  79:     public ParseException(final Exception e) {
  80:         super(e);
  81:         fillLocation(null);
  82:     }
  83: 
  84:     /**
  85:      * Creates a new ParseException with the given message and root exception.
  86:      *
  87:      * @param s the message
  88:      * @param e the exception
  89:      */
  90:     public ParseException(final String s, final Exception e) {
  91:         super(s, e);
  92:         fillLocation(null);
  93:     }
  94: 
  95:     /**
  96:      * Creates a new ParseException with the given message and the locator.
  97:      *
  98:      * @param message the message
  99:      * @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: }