/* $Id: SpecificationHandler.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.Specification;
import org.qedeq.kernel.dto.module.LocationListVo;
import org.qedeq.kernel.dto.module.LocationVo;
import org.qedeq.kernel.dto.module.SpecificationVo;
import org.qedeq.kernel.xml.parser.AbstractSimpleHandler;
import org.qedeq.kernel.xml.parser.SimpleAttributes;
import org.qedeq.kernel.xml.parser.SyntaxException;


/**
 * Parse specification informations.
 *
 * @version $Revision: 1.10 $
 * @author  Michael Meyling
 */
public final class SpecificationHandler extends AbstractSimpleHandler {

    /** Specification value object. */
    private Specification specification;

    /** Module name. */
    private String moduleName;

    /** Rule version which is at least needed to verify this module. */
    private String ruleVersion;

    /** List of locations for module. */
    private LocationListVo locations;


    /**
     * Constructor.
     *
     * @param   handler     Parent handler.
     * @param   startTag    XML part start with this tag.
     */
    public SpecificationHandler(final AbstractSimpleHandler handler, final String startTag) {
        super(handler, startTag);
    }

    public final void init() {
        specification = null;
        moduleName = null;
        ruleVersion = null;
        locations = null;
    }

    /**
     * Get specification.
     *
     * @return  Module specification.
     */
    public final Specification getSpecification() {
        return specification;
    }

    public final void startElement(final String name, final SimpleAttributes attributes)
            throws SyntaxException {
        if (getStartTag().equals(name)) {
            locations = new LocationListVo();
            moduleName = attributes.getString("name");
            ruleVersion = attributes.getString("ruleVersion");
        } else if ("SPECIFICATION".equals(name)) {
            // ignore
        } else if ("LOCATIONS".equals(name)) {
            // ignore
        } else if ("LOCATION".equals(name)) {
            locations.add(new LocationVo(attributes.getString("value")));
        } else {
            throw SyntaxException.createUnexpectedTagException(name);
        }
    }

    public final void endElement(final String name) throws SyntaxException {
        if (getStartTag().equals(name)) {
            specification = new SpecificationVo(moduleName, ruleVersion, locations);
        } else if ("SPECIFICATION".equals(name)) {
            // ignore
        } else if ("LOCATIONS".equals(name)) {
            // ignore
        } else if ("LOCATION".equals(name)) {
            // ignore
        } else {
            throw SyntaxException.createUnexpectedTagException(name);
        }
    }

}
