/* $Id: SubsectionListHandler.java,v 1.2 2005/08/19 04:13:28 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.xml.handler.module;

import org.qedeq.kernel.base.module.SubsectionList;
import org.qedeq.kernel.dto.module.SubsectionListVo;
import org.qedeq.kernel.xml.parser.AbstractSimpleHandler;
import org.qedeq.kernel.xml.parser.SyntaxException;
import org.qedeq.kernel.xml.parser.SimpleAttributes;

/**
 * Parse subsection list.
 *
 * @version $Revision: 1.2 $
 * @author  Michael Meyling
 */
public final class SubsectionListHandler extends AbstractSimpleHandler {

     /** List of subsections. */
    private SubsectionListVo list;

    /** Parses an subsection. */
    private final SubsectionHandler subsectionHandler;

    /** Handle single node of a section. */
    private final NodeHandler nodeHandler;


    /**
     * Handles list of subsections.
     *
     * @param   handler Parent handler.
     */
    public SubsectionListHandler(final AbstractSimpleHandler handler) {
        super(handler, "SUBSECTIONS");
        subsectionHandler = new SubsectionHandler(this);
        nodeHandler = new NodeHandler(this);
    }

    public final void init() {
        list = new SubsectionListVo();
    }

    /**
     * Get list of subsections.
     *
     * @return  Subsection list.
     */
    public final SubsectionList getSubsectionList() {
        return list;
    }

    public final void startElement(final String name, final SimpleAttributes attributes)
            throws SyntaxException {
        if (getStartTag().equals(name)) {
            // nothing to do
        } else if (subsectionHandler.getStartTag().equals(name)) {
            changeHandler(subsectionHandler, name, attributes);
        } else if (nodeHandler.getStartTag().equals(name)) {
            changeHandler(nodeHandler, 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 (subsectionHandler.getStartTag().equals(name)) {
            list.add(subsectionHandler.getSubsection());
        } else if (nodeHandler.getStartTag().equals(name)) {
            list.add(nodeHandler.getNode());
        } else {
            throw SyntaxException.createUnexpectedTagException(name);
        }
    }
}
