    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     * PieDatasetHandler.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: PieDatasetHandler.java,v 1.3.2.1 2005/10/25 21:36:10 mungady Exp $
    036     *
    037     * Changes (from 21-Jun-2001)
    038     * --------------------------
    039     * 23-Jan-2003 : Version 1 (DG);
    040     *
    041     */
    042
    043    package org.jfree.data.xml;
    044
    045    import org.jfree.data.general.DefaultPieDataset;
    046    import org.jfree.data.general.PieDataset;
    047    import org.xml.sax.Attributes;
    048    import org.xml.sax.SAXException;
    049    import org.xml.sax.helpers.DefaultHandler;
    050
    051    /**
    052     * A SAX handler for reading a {@link PieDataset} from an XML file.
    053     */
    054    public class PieDatasetHandler extends RootHandler implements DatasetTags {
    055
    056        /** The pie dataset under construction. */
    057        private DefaultPieDataset dataset;
    058
    059        /**
    060         * Default constructor.
    061         */
    062        public PieDatasetHandler() {
    063            this.dataset = null;
    064        }
    065
    066        /**
    067         * Returns the dataset.
    068         *
    069         * @return The dataset.
    070         */
    071        public PieDataset getDataset() {
    072            return this.dataset;
    073        }
    074
    075        /**
    076         * Adds an item to the dataset under construction.
    077         *
    078         * @param key  the key.
    079         * @param value  the value.
    080         */
    081        public void addItem(Comparable key, Number value) {
    082            this.dataset.setValue(key, value);
    083        }
    084
    085        /**
    086         * Starts an element.
    087         *
    088         * @param namespaceURI  the namespace.
    089         * @param localName  the element name.
    090         * @param qName  the element name.
    091         * @param atts  the element attributes.
    092         *
    093         * @throws SAXException for errors.
    094         */
    095        public void startElement(String namespaceURI,
    096                                 String localName,
    097                                 String qName,
    098                                 Attributes atts) throws SAXException {
    099
    100            DefaultHandler current = getCurrentHandler();
    101            if (current != this) {
    102                current.startElement(namespaceURI, localName, qName, atts);
    103            }
    104            else if (qName.equals(PIEDATASET_TAG)) {
    105                this.dataset = new DefaultPieDataset();
    106            }
    107            else if (qName.equals(ITEM_TAG)) {
    108                ItemHandler subhandler = new ItemHandler(this, this);
    109                getSubHandlers().push(subhandler);
    110                subhandler.startElement(namespaceURI, localName, qName, atts);
    111            }
    112
    113        }
    114
    115        /**
    116         * The end of an element.
    117         *
    118         * @param namespaceURI  the namespace.
    119         * @param localName  the element name.
    120         * @param qName  the element name.
    121         *
    122         * @throws SAXException for errors.
    123         */
    124        public void endElement(String namespaceURI,
    125                               String localName,
    126                               String qName) throws SAXException {
    127
    128            DefaultHandler current = getCurrentHandler();
    129            if (current != this) {
    130                current.endElement(namespaceURI, localName, qName);
    131            }
    132
    133        }
    134
    135    }