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