/**
 * =========================================
 * LibXML : a free Java layouting library
 * =========================================
 *
 * Project Info:  http://reporting.pentaho.org/libxml/
 *
 * (C) Copyright 2006-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 *
 * ------------
 * $Id: ParseException.java 3518 2007-10-16 10:26:53Z tmorgner $
 * ------------
 * (C) Copyright 2006-2007, by Pentaho Corporation.
 */
package org.jfree.xmlns.parser;

import java.io.PrintStream;
import java.io.PrintWriter;

import org.xml.sax.Locator;
import org.xml.sax.SAXException;

/**
 * A parse exception. This does the same as the SAXParseException, but it
 * also prints the parent exception.
 *
 * @author Thomas Morgner
 */
public class ParseException extends SAXException
{

  /** The line, where the error occured. */
  private int line;

  /** The column, where the error occured. */
  private int column;

  private boolean noOwnMessage;

  /**
   * Creates a new ParseException with the given message.
   *
   * @param message the message
   */
  public ParseException(final String message)
  {
    super(message);
    fillLocation(null);
    noOwnMessage = (message == null);
  }

  /**
   * Creates a new ParseException with the given root exception.
   *
   * @param e the exception
   */
  public ParseException(final Exception e)
  {
    super(e);
    fillLocation(null);
    noOwnMessage = true;
  }

  /**
   * Creates a new ParseException with the given message and root exception.
   *
   * @param message the message
   * @param e the exception
   */
  public ParseException(final String message, final Exception e)
  {
    super(message, e);
    fillLocation(null);
    noOwnMessage = (message == null);
  }

  /**
   * Creates a new ParseException with the given message and the locator.
   *
   * @param message the message
   * @param locator the locator of the parser
   */
  public ParseException(final String message, final Locator locator)
  {
    super(message);
    fillLocation(locator);
    noOwnMessage = (message == null);
  }

  /**
   * Creates a new ParseException with the given root exception and the
   * locator.
   *
   * @param e       the exception
   * @param locator the locator of the parser
   */
  public ParseException(final Exception e, final Locator locator)
  {
    super(e);
    fillLocation(locator);
    noOwnMessage = true;
  }

  /**
   * Creates a new ParseException with the given message, root exception and
   * the locator.
   *
   * @param message       the message
   * @param e       the exception
   * @param locator the locator of the parser
   */
  public ParseException(final String message,
                        final Exception e,
                        final Locator locator)
  {
    super(message, e);
    fillLocation(locator);
    noOwnMessage = (message == null);
  }

  /**
   * Modifies the message to give more detailed location information.
   *
   * @return the modified exception message.
   */
  public String getMessage()
  {
    if (noOwnMessage)
    {
      final Exception parentEx = getException();
      if (parentEx instanceof ParseException)
      {
        return parentEx.getMessage();
      }
      else
      {
        final StringBuffer message = new StringBuffer
                (String.valueOf(parentEx.getMessage()));
        message.append(" [Location: Line=");
        message.append(this.line);
        message.append(" Column=");
        message.append(this.column);
        message.append("] ");
        return message.toString();
      }
    }
    else
    {
      final StringBuffer message = new StringBuffer
              (String.valueOf(super.getMessage()));
      message.append(" [Location: Line=");
      message.append(this.line);
      message.append(" Column=");
      message.append(this.column);
      message.append("] ");
      return message.toString();
    }
  }

  /**
   * Fills the location with the given locator.
   *
   * @param locator the locator or null.
   */
  protected void fillLocation(final Locator locator)
  {
    if (locator == null)
    {
      this.line = -1;
      this.column = -1;
    }
    else
    {
      this.line = locator.getLineNumber();
      this.column = locator.getColumnNumber();
    }
  }

  /**
   * Returns the line of the parse position where the error occured.
   *
   * @return the line number or -1 if not known.
   */
  public int getLine()
  {
    return this.line;
  }

  /**
   * Returns the column of the parse position where the error occured.
   *
   * @return the column number or -1 if not known.
   */
  public int getColumn()
  {
    return this.column;
  }


  /**
   * Prints the stack trace to the specified stream.
   *
   * @param stream the output stream.
   */
  public void printStackTrace(final PrintStream stream)
  {
    super.printStackTrace(stream);
    if (getException() != null)
    {
      stream.println("ParentException: ");
      getException().printStackTrace(stream);
    }
  }

  /**
   * Override toString to pick up any embedded exception.
   *
   * @return A string representation of this exception.
   */
  public String toString()
  {
    return getClass().getName() + ": " + getMessage();
  }

  /**
   * Prints the stack trace to the specified writer.
   *
   * @param writer the writer.
   */
  public void printStackTrace(final PrintWriter writer)
  {
    super.printStackTrace(writer);
    if (getException() != null)
    {
      writer.println("ParentException: ");
      getException().printStackTrace(writer);
    }
  }

}
