001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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    @Override
082    public void characters(char[] ch, int start, int length)
083        throws SAXException {
084        DefaultHandler handler = getCurrentHandler();
085        if (handler != this) {
086            handler.characters(ch, start, length);
087        }
088    }
089
090    /**
091     * Returns the handler at the top of the stack.
092     *
093     * @return The handler.
094     */
095    public DefaultHandler getCurrentHandler() {
096        DefaultHandler result = this;
097        if (this.subHandlers != null) {
098            if (this.subHandlers.size() > 0) {
099                Object top = this.subHandlers.peek();
100                if (top != null) {
101                    result = (DefaultHandler) top;
102                }
103            }
104        }
105        return result;
106    }
107
108    /**
109     * Pushes a sub-handler onto the stack.
110     *
111     * @param subhandler  the sub-handler.
112     */
113    public void pushSubHandler(DefaultHandler subhandler) {
114        this.subHandlers.push(subhandler);
115    }
116
117    /**
118     * Pops a sub-handler from the stack.
119     *
120     * @return The sub-handler.
121     */
122    public DefaultHandler popSubHandler() {
123        return (DefaultHandler) this.subHandlers.pop();
124    }
125
126}