/* $Id: XmlFilePositionException.java,v 1.3 2005/10/13 03:39:56 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2005,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 */

package org.qedeq.kernel.rel.test.text;

import java.io.File;
import java.io.IOException;

import org.qedeq.kernel.bo.control.IllegalModuleDataException;
import org.qedeq.kernel.utility.IoUtility;
import org.qedeq.kernel.utility.ReplaceUtility;
import org.qedeq.kernel.utility.TextInput;
import org.qedeq.kernel.xml.mapper.Context2SimpleXPath;
import org.qedeq.kernel.xml.parser.SyntaxException;
import org.qedeq.kernel.xml.tracker.SourcePosition;
import org.xml.sax.SAXException;



/**
 * Data validation error. Occurs if a set or add method leads to wrong or inconsistent data.
 *
 * @version $Revision: 1.3 $
 * @author  Michael Meyling
 */
public final class XmlFilePositionException extends Exception {

    /** Serialization information. */
    private static final long serialVersionUID = -4109767904038020052L;

    /** Error code of this Exception. */
    private final int errorCode;

    /** Error location. */
    private final SourcePosition context;

    /** Reference location to explain the error. */
    private final SourcePosition referenceContext;

    /** Referenced line with marker. */
    private String line;

    /**
     * Constructor.
     * 
     * @param   exception   Exception to wrap. 
     */
    public XmlFilePositionException(final IllegalModuleDataException exception) {
        super(exception);
        this.errorCode = exception.getErrorCode();
        context = Context2SimpleXPath.getXPath(exception.getContext()).getStartLocation();
        referenceContext = Context2SimpleXPath.getXPath(exception.getContext())
            .getEndLocation();
    }

    
    /**
     * Constructor.
     * 
     * @param   exception   Exception to wrap. 
     */
    public XmlFilePositionException(final SyntaxException exception) {
        super(exception);
        this.errorCode = exception.getErrorCode();
        context = exception.getErrorPosition();
        referenceContext = null;
    }

    /**
     * Constructor.
     * 
     * @param   file        Parsed file. 
     * @param   exception   Exception to wrap. 
     */
    public XmlFilePositionException(final String file, final Exception exception) {
        super(exception);
        this.errorCode = 9998;
        context = new SourcePosition(file, 1, 1);
        referenceContext = null;
    }

    /**
     * Constructor.
     * 
     * @param   exception   Exception to wrap. 
     */
    public XmlFilePositionException(final Exception exception) {
        super(exception);
        this.errorCode = 9999;
        context = null;
        referenceContext = null;
    }

    /**
     * Constructor.
     * 
     * @param   exception   Exception to wrap. 
     */
    public XmlFilePositionException(final IOException exception) {
        super(exception);
        this.errorCode = 9997;
        context = null;
        referenceContext = null;
    }

    /**
     * Constructor.
     * 
     * @param   exception   Exception to wrap. 
     */
    public XmlFilePositionException(final SAXException exception) {
        super(exception);
        this.errorCode = SyntaxException.SAX_PARSER_EXCEPTION;
        context = null;
        referenceContext = null;
    }

    /**
     * Get context information about error location.
     *
     * @return  Error location context.
     */
    public final SourcePosition getContext() {
        return context;
    }

    /**
     * Get additional context information about another associated location.
     *
     * @return  Additional error location context.
     */
    public final SourcePosition getReferenceContext() {
        return referenceContext;
    }

    /**
     * Get error code.
     *
     * @return  Error code.
     */
    public final int getErrorCode() {
        return errorCode;
    }
    
    /**
     * Get line that is referenced by {@link #getContext()}.
     * 
     * @return  Referenced line.
     */
    public final String getLine() {
        if (line == null) {
            line = "";
            try {
                final TextInput input = new TextInput(new File(context.getLocalAddress()), 
                    context.getLocalAddress());
                input.setRow(context.getLine());
                input.setColumn(context.getColumn());
                line = input.getLine();
            } catch (IOException e) {
                // ignore
            }
        }
        return line;
    }

    public final String getMessage() {
        if (getCause() != null) {
            if (getCause().getCause() != null) {
                return getCause().getCause().getMessage();
            }
            return getCause().getMessage();
        }
        return "";
    }

    /**
     * Get detailed error description.
     * The first line contains {@link #getErrorCode()} and {@link #getMessage()}.
     * The second line contains the local address, the line and column.
     * Third line is the result or {@link #getLine()}.
     * In the fourth line the row position for the third line is marked.
     * 
     * @return  Error description.
     */
    public final String getDescription() {
        final StringBuffer buffer = new StringBuffer();
        buffer.append(getErrorCode() + ": " + getMessage());
        if (context != null) {
            buffer.append("\n");
            buffer.append(context.getLocalAddress() + ":" + context.getLine() + ":" 
                + context.getColumn());
            buffer.append("\n");
            buffer.append(ReplaceUtility.replace(getLine(), "\t", " "));
            buffer.append("\n");
            final StringBuffer whitespace = IoUtility.getSpaces(context.getColumn() - 1);
            buffer.append(whitespace);
            buffer.append("^");
        }
        return buffer.toString();
    }

}
