/* $Id: AxiomHandler.java,v 1.10 2006/10/20 20:23:02 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2006,  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.handler.module;

import org.qedeq.kernel.base.module.Axiom;
import org.qedeq.kernel.dto.module.AxiomVo;
import org.qedeq.kernel.xml.parser.AbstractSimpleHandler;
import org.qedeq.kernel.xml.parser.SyntaxException;
import org.qedeq.kernel.xml.parser.SimpleAttributes;


/**
 * Parse an axiom.
 *
 * @version $Revision: 1.10 $
 * @author  Michael Meyling
 */
public final class AxiomHandler extends AbstractSimpleHandler {

    /** Handler for axiom formula. */
    private final FormulaOrTermHandler formulaHandler;

    /** Handler for rule description. */
    private final LatexListHandler descriptionHandler;

    /** Axiom value object. */
    private AxiomVo axiom;

    /**
     * Deals with axioms.
     *
     * @param   handler Parent handler.
     */
    public AxiomHandler(final AbstractSimpleHandler handler) {
        super(handler, "AXIOM");
        formulaHandler = new FormulaOrTermHandler(this, "FORMULA");
        descriptionHandler = new LatexListHandler(this, "DESCRIPTION");
    }

    public final void init() {
        axiom = null;
    }

    /**
     * Get axiom.
     *
     * @return  Axiom.
     */
    public final Axiom getAxiom() {
        return axiom;
    }

    public final void startElement(final String name, final SimpleAttributes attributes)
            throws SyntaxException {
        if (getStartTag().equals(name)) {
            axiom = new AxiomVo();
        } else if (formulaHandler.getStartTag().equals(name)) {
            changeHandler(formulaHandler, name, attributes);
        } else if (descriptionHandler.getStartTag().equals(name)) {
            changeHandler(descriptionHandler, name, attributes);
        } else {
            throw SyntaxException.createUnexpectedTagException(name);
        }
    }

    public final void endElement(final String name) throws SyntaxException {
        if (getStartTag().equals(name)) {
            // nothing to do
        } else if (formulaHandler.getStartTag().equals(name)) {
            axiom.setFormula(formulaHandler.getFormulaOrTerm());
        } else if (descriptionHandler.getStartTag().equals(name)) {
            axiom.setDescription(descriptionHandler.getLatexList());
        } else {
            throw SyntaxException.createUnexpectedTagException(name);
        }
    }

}
