    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     * CategorySeriesHandler.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: CategorySeriesHandler.java,v 1.4.2.2 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.Iterator;
    046
    047    import org.jfree.data.DefaultKeyedValues;
    048    import org.xml.sax.Attributes;
    049    import org.xml.sax.SAXException;
    050    import org.xml.sax.helpers.DefaultHandler;
    051
    052    /**
    053     * A handler for reading a series for a category dataset.
    054     */
    055    public class CategorySeriesHandler extends DefaultHandler
    056                                       implements DatasetTags {
    057
    058        /** The root handler. */
    059        private RootHandler root;
    060
    061        /** The series key. */
    062        private Comparable seriesKey;
    063
    064        /** The values. */
    065        private DefaultKeyedValues values;
    066
    067        /**
    068         * Creates a new item handler.
    069         *
    070         * @param root  the root handler.
    071         */
    072        public CategorySeriesHandler(RootHandler root) {
    073            this.root = root;
    074            this.values = new DefaultKeyedValues();
    075        }
    076
    077        /**
    078         * Sets the series key.
    079         *
    080         * @param key  the key.
    081         */
    082        public void setSeriesKey(Comparable key) {
    083            this.seriesKey = key;
    084        }
    085
    086        /**
    087         * Adds an item to the temporary storage for the series.
    088         *
    089         * @param key  the key.
    090         * @param value  the value.
    091         */
    092        public void addItem(Comparable key, final Number value) {
    093            this.values.addValue(key, value);
    094        }
    095
    096        /**
    097         * The start of an element.
    098         *
    099         * @param namespaceURI  the namespace.
    100         * @param localName  the element name.
    101         * @param qName  the element name.
    102         * @param atts  the attributes.
    103         *
    104         * @throws SAXException for errors.
    105         */
    106        public void startElement(String namespaceURI,
    107                                 String localName,
    108                                 String qName,
    109                                 Attributes atts) throws SAXException {
    110
    111            if (qName.equals(SERIES_TAG)) {
    112                setSeriesKey(atts.getValue("name"));
    113                ItemHandler subhandler = new ItemHandler(this.root, this);
    114                this.root.pushSubHandler(subhandler);
    115            }
    116            else if (qName.equals(ITEM_TAG)) {
    117                ItemHandler subhandler = new ItemHandler(this.root, this);
    118                this.root.pushSubHandler(subhandler);
    119                subhandler.startElement(namespaceURI, localName, qName, atts);
    120            }
    121
    122            else {
    123                throw new SAXException(
    124                    "Expecting <Series> or <Item> tag...found " + qName
    125                );
    126            }
    127        }
    128
    129        /**
    130         * The end of an element.
    131         *
    132         * @param namespaceURI  the namespace.
    133         * @param localName  the element name.
    134         * @param qName  the element name.
    135         */
    136        public void endElement(String namespaceURI,
    137                               String localName,
    138                               String qName) {
    139
    140            if (this.root instanceof CategoryDatasetHandler) {
    141                CategoryDatasetHandler handler = (CategoryDatasetHandler) this.root;
    142
    143                Iterator iterator = this.values.getKeys().iterator();
    144                while (iterator.hasNext()) {
    145                    Comparable key = (Comparable) iterator.next();
    146                    Number value = this.values.getValue(key);
    147                    handler.addItem(this.seriesKey, key, value);
    148                }
    149
    150                this.root.popSubHandler();
    151            }
    152
    153        }
    154
    155    }