/* $Id: SyntaxException.java,v 1.8 2007/02/25 20:05:37 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2007,  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.xml.parser;

import java.net.URL;

import org.xml.sax.SAXParseException;


/**
 * Exception that occurs during XML parsing. It specifies an syntactical error.
 * It can also mark a lack of inner consistence of something.
 *
 * @version $Revision: 1.8 $
 * @author    Michael Meyling
 */
public final class SyntaxException extends Exception {

    /** Error code for Exceptions thrown by the SAXParser. */
    public static final int SAX_PARSER_EXCEPTION = 9001;

    /** Error code for unexpected tag. */
    public static final int UNEXPECTED_TAG_CODE = 9002;

    /** Unexpected tag message text. */
    public static final String UNEXPECTED_TAG_TEXT = "Unexpected tag: ";

    /** Error code for unexpected character data. */
    public static final int UNEXPECTED_DATA_CODE = 9003;

    /** Unexpected tag message text, part one. */
    public static final String UNEXPECTED_DATA_TEXT = "Unexpected character data in tag: ";

    /** Error code for unexpected character data. */
    public static final int MISSING_ATTRIBUTE_CODE = 9004;

    /** Missing attribute text. */
    public static final String MISSING_ATTRIBUTE_TEXT_1 = "Missing attribute: ";

    /** Missing attribute, part two. */
    public static final String MISSING_ATTRIBUTE_TEXT_2 = " in tag: ";

    /** For serialization. */
    private static final long serialVersionUID = -1356389036916097293L;

    /** Error location. */
    private SourcePosition position;

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


    /**
     * Constructor.
     *
     * @param   e       Exception thrown by the {@link javax.xml.parsers.SAXParser}.
     * @param   url     Parsed file.
     */
    public SyntaxException(final SAXParseException e, final URL url) {
        super(e);
        position = new SourcePosition(url, e.getLineNumber(),
            e.getColumnNumber());
        errorCode = SAX_PARSER_EXCEPTION;
    }

    /**
     * Constructor.
     *
     * @param   code    Error code.
     * @param   message Error message.
     */
    public SyntaxException(final int code, final String message) {
        super(message);
        errorCode = code;
    }

    /**
     * Get error code.
     *
     * @return  Error code.
     */
    public final int getErrorCode() {
        return errorCode;
    }

    /**
     * Get error position.
     *
     * @return  Error position.
     */
    public final SourcePosition getErrorPosition() {
        return position;
    }


    /**
     * Set error position.
     *
     * @param   position    Error position.
     */
    public final void setErrorPosition(final SourcePosition position) {
        this.position = position;
    }

    /**
     * Create exception for unexpected tag.
     *
     * @param   name    Tag name.
     * @return  Exception.
     */
    public static final SyntaxException createUnexpectedTagException(final String name) {
        return new SyntaxException(UNEXPECTED_TAG_CODE, UNEXPECTED_TAG_TEXT + name);
    }

    /**
     * Create exception for unexpected text data within a tag.
     *
     * @param   name    Tag name.
     * @param   value   Data found.
     * @return  Exception.
     */
    public static final SyntaxException createUnexpectedTextDataException(final String name,
            final String value) {
        return new SyntaxException(UNEXPECTED_DATA_CODE, UNEXPECTED_DATA_TEXT + name);
    }

    /**
     * Create exception for missing attribute within a tag.
     *
     * @param   name        Tag name.
     * @param   attribute   Attribute name.
     * @return  Exception.
     */
    public static final SyntaxException createMissingAttributeException(final String name,
            final String attribute) {
        return new SyntaxException(MISSING_ATTRIBUTE_CODE, MISSING_ATTRIBUTE_TEXT_1 + attribute
            + MISSING_ATTRIBUTE_TEXT_2 + name);
    }

}
