001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, 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     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     *
027     * ----------------
028     * RootHandler.java
029     * ----------------
030     * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: RootHandler.java,v 1.3.2.1 2005/10/25 21:36:10 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 23-Jan-2003 : Version 1 (DG);
040     *
041     */
042    
043    package org.jfree.data.xml;
044    
045    import java.util.Stack;
046    
047    import org.xml.sax.SAXException;
048    import org.xml.sax.helpers.DefaultHandler;
049    
050    /**
051     * A SAX handler that delegates work to sub-handlers.
052     */
053    public class RootHandler extends DefaultHandler implements DatasetTags {
054    
055        /** The sub-handlers. */
056        private Stack subHandlers;
057    
058        /**
059         * Creates a new handler.
060         */
061        public RootHandler() {
062            this.subHandlers = new Stack();
063        }
064    
065        /**
066         * Returns the stack of sub handlers.
067         *
068         * @return The sub-handler stack.
069         */
070        public Stack getSubHandlers() {
071            return this.subHandlers;
072        }
073    
074        /**
075         * Receives some (or all) of the text in the current element.
076         *
077         * @param ch  character buffer.
078         * @param start  the start index.
079         * @param length  the length of the valid character data.
080         *
081         * @throws SAXException for errors.
082         */
083        public void characters(char[] ch, int start, int length) 
084            throws SAXException {
085            DefaultHandler handler = getCurrentHandler();
086            if (handler != this) {
087                handler.characters(ch, start, length);
088            }
089        }
090    
091        /**
092         * Returns the handler at the top of the stack.
093         *
094         * @return The handler.
095         */
096        public DefaultHandler getCurrentHandler() {
097            DefaultHandler result = this;
098            if (this.subHandlers != null) {
099                if (this.subHandlers.size() > 0) {
100                    Object top = this.subHandlers.peek();
101                    if (top != null) {
102                        result = (DefaultHandler) top;
103                    }
104                }
105            }
106            return result;
107        }
108    
109        /**
110         * Pushes a sub-handler onto the stack.
111         *
112         * @param subhandler  the sub-handler.
113         */
114        public void pushSubHandler(DefaultHandler subhandler) {
115            this.subHandlers.push(subhandler);
116        }
117    
118        /**
119         * Pops a sub-handler from the stack.
120         *
121         * @return The sub-handler.
122         */
123        public DefaultHandler popSubHandler() {
124            return (DefaultHandler) this.subHandlers.pop();
125        }
126    
127    }