    001/* ===========================================================
    002 * JFreeChart : a free chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C) Copyright 2000-2011, by Object Refinery Limited and Contributors.
    006 *
    007 * Project Info:  http://www.jfree.org/jfreechart/index.html
    008 *
    009 * This library is free software; you can redistribute it and/or modify it
    010 * under the terms of the GNU Lesser General Public License as published by
    011 * the Free Software Foundation; either version 2.1 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This library is distributed in the hope that it will be useful, but
    015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017 * License for more details.
    018 *
    019 * You should have received a copy of the GNU Lesser General Public
    020 * License along with this library; if not, write to the Free Software
    021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022 * USA.
    023 *
    024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    025 * Other names may be trademarks of their respective owners.]
    026 *
    027 * ----------------
    028 * RootHandler.java
    029 * ----------------
    030 * (C) Copyright 2003-2008, by Object Refinery Limited and Contributors.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   -;
    034 *
    035 * Changes
    036 * -------
    037 * 23-Jan-2003 : Version 1 (DG);
    038 *
    039 */
    040
    041package org.jfree.data.xml;
    042
    043import java.util.Stack;
    044
    045import org.xml.sax.SAXException;
    046import org.xml.sax.helpers.DefaultHandler;
    047
    048/**
    049 * A SAX handler that delegates work to sub-handlers.
    050 */
    051public class RootHandler extends DefaultHandler implements DatasetTags {
    052
    053    /** The sub-handlers. */
    054    private Stack subHandlers;
    055
    056    /**
    057     * Creates a new handler.
    058     */
    059    public RootHandler() {
    060        this.subHandlers = new Stack();
    061    }
    062
    063    /**
    064     * Returns the stack of sub handlers.
    065     *
    066     * @return The sub-handler stack.
    067     */
    068    public Stack getSubHandlers() {
    069        return this.subHandlers;
    070    }
    071
    072    /**
    073     * Receives some (or all) of the text in the current element.
    074     *
    075     * @param ch  character buffer.
    076     * @param start  the start index.
    077     * @param length  the length of the valid character data.
    078     *
    079     * @throws SAXException for errors.
    080     */
    081    public void characters(char[] ch, int start, int length)
    082        throws SAXException {
    083        DefaultHandler handler = getCurrentHandler();
    084        if (handler != this) {
    085            handler.characters(ch, start, length);
    086        }
    087    }
    088
    089    /**
    090     * Returns the handler at the top of the stack.
    091     *
    092     * @return The handler.
    093     */
    094    public DefaultHandler getCurrentHandler() {
    095        DefaultHandler result = this;
    096        if (this.subHandlers != null) {
    097            if (this.subHandlers.size() > 0) {
    098                Object top = this.subHandlers.peek();
    099                if (top != null) {
    100                    result = (DefaultHandler) top;
    101                }
    102            }
    103        }
    104        return result;
    105    }
    106
    107    /**
    108     * Pushes a sub-handler onto the stack.
    109     *
    110     * @param subhandler  the sub-handler.
    111     */
    112    public void pushSubHandler(DefaultHandler subhandler) {
    113        this.subHandlers.push(subhandler);
    114    }
    115
    116    /**
    117     * Pops a sub-handler from the stack.
    118     *
    119     * @return The sub-handler.
    120     */
    121    public DefaultHandler popSubHandler() {
    122        return (DefaultHandler) this.subHandlers.pop();
    123    }
    124
    125}