001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * [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-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
041package org.jfree.data.xml;
042
043import java.util.Iterator;
044
045import org.jfree.data.DefaultKeyedValues;
046import org.xml.sax.Attributes;
047import org.xml.sax.SAXException;
048import org.xml.sax.helpers.DefaultHandler;
049
050/**
051 * A handler for reading a series for a category dataset.
052 */
053public 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, 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    @Override
105    public void startElement(String namespaceURI,
106                             String localName,
107                             String qName,
108                             Attributes atts) throws SAXException {
109
110        if (qName.equals(SERIES_TAG)) {
111            setSeriesKey(atts.getValue("name"));
112            ItemHandler subhandler = new ItemHandler(this.root, this);
113            this.root.pushSubHandler(subhandler);
114        }
115        else if (qName.equals(ITEM_TAG)) {
116            ItemHandler subhandler = new ItemHandler(this.root, this);
117            this.root.pushSubHandler(subhandler);
118            subhandler.startElement(namespaceURI, localName, qName, atts);
119        }
120
121        else {
122            throw new SAXException(
123                "Expecting <Series> or <Item> tag...found " + qName
124            );
125        }
126    }
127
128    /**
129     * The end of an element.
130     *
131     * @param namespaceURI  the namespace.
132     * @param localName  the element name.
133     * @param qName  the element name.
134     */
135    @Override
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}