001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * --------------------------
028 * CategorySeriesHandler.java
029 * --------------------------
030 * (C) Copyright 2003-2022, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 23-Jan-2003 : Version 1 (DG);
038 *
039 */
040
041package org.jfree.data.xml;
042
043import org.jfree.data.DefaultKeyedValues;
044import org.xml.sax.Attributes;
045import org.xml.sax.SAXException;
046import org.xml.sax.helpers.DefaultHandler;
047
048/**
049 * A handler for reading a series for a category dataset.
050 */
051public class CategorySeriesHandler extends DefaultHandler
052        implements DatasetTags {
053
054    /** The root handler. */
055    private RootHandler root;
056
057    /** The series key. */
058    private Comparable seriesKey;
059
060    /** The values. */
061    private DefaultKeyedValues values;
062
063    /**
064     * Creates a new item handler.
065     *
066     * @param root  the root handler.
067     */
068    public CategorySeriesHandler(RootHandler root) {
069        this.root = root;
070        this.values = new DefaultKeyedValues();
071    }
072
073    /**
074     * Sets the series key.
075     *
076     * @param key  the key.
077     */
078    public void setSeriesKey(Comparable key) {
079        this.seriesKey = key;
080    }
081
082    /**
083     * Adds an item to the temporary storage for the series.
084     *
085     * @param key  the key.
086     * @param value  the value.
087     */
088    public void addItem(Comparable key, Number value) {
089        this.values.addValue(key, value);
090    }
091
092    /**
093     * The start of an element.
094     *
095     * @param namespaceURI  the namespace.
096     * @param localName  the element name.
097     * @param qName  the element name.
098     * @param atts  the attributes.
099     *
100     * @throws SAXException for errors.
101     */
102    @Override
103    public void startElement(String namespaceURI,
104                             String localName,
105                             String qName,
106                             Attributes atts) throws SAXException {
107
108        if (qName.equals(SERIES_TAG)) {
109            setSeriesKey(atts.getValue("name"));
110            ItemHandler subhandler = new ItemHandler(this.root, this);
111            this.root.pushSubHandler(subhandler);
112        }
113        else if (qName.equals(ITEM_TAG)) {
114            ItemHandler subhandler = new ItemHandler(this.root, this);
115            this.root.pushSubHandler(subhandler);
116            subhandler.startElement(namespaceURI, localName, qName, atts);
117        }
118
119        else {
120            throw new SAXException(
121                "Expecting <Series> or <Item> tag...found " + qName
122            );
123        }
124    }
125
126    /**
127     * The end of an element.
128     *
129     * @param namespaceURI  the namespace.
130     * @param localName  the element name.
131     * @param qName  the element name.
132     */
133    @Override
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            for (Object o : this.values.getKeys()) {
142                Comparable key = (Comparable) o;
143                Number value = this.values.getValue(key);
144                handler.addItem(this.seriesKey, key, value);
145            }
146
147            this.root.popSubHandler();
148        }
149
150    }
151
152}