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