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