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 * CategoryDatasetHandler.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 org.jfree.data.category.CategoryDataset;
040import org.jfree.data.category.DefaultCategoryDataset;
041import org.xml.sax.Attributes;
042import org.xml.sax.SAXException;
043import org.xml.sax.helpers.DefaultHandler;
044
045/**
046 * A SAX handler for reading a {@link CategoryDataset} from an XML file.
047 */
048public class CategoryDatasetHandler extends RootHandler implements DatasetTags {
049
050    /** The dataset under construction. */
051    private DefaultCategoryDataset dataset;
052
053    /**
054     * Creates a new handler.
055     */
056    public CategoryDatasetHandler() {
057        this.dataset = null;
058    }
059
060    /**
061     * Returns the dataset.
062     *
063     * @return The dataset.
064     */
065    public CategoryDataset getDataset() {
066        return this.dataset;
067    }
068
069    /**
070     * Adds an item to the dataset.
071     *
072     * @param rowKey  the row key.
073     * @param columnKey  the column key.
074     * @param value  the value.
075     */
076    public void addItem(Comparable rowKey, Comparable columnKey, Number value) {
077        this.dataset.addValue(value, rowKey, columnKey);
078    }
079
080    /**
081     * The start of an element.
082     *
083     * @param namespaceURI  the namespace.
084     * @param localName  the element name.
085     * @param qName  the element name.
086     * @param atts  the element attributes.
087     *
088     * @throws SAXException for errors.
089     */
090    @Override
091    public void startElement(String namespaceURI,
092                             String localName,
093                             String qName,
094                             Attributes atts) throws SAXException {
095
096        DefaultHandler current = getCurrentHandler();
097        if (current != this) {
098            current.startElement(namespaceURI, localName, qName, atts);
099        }
100        else if (qName.equals(CATEGORYDATASET_TAG)) {
101            this.dataset = new DefaultCategoryDataset();
102        }
103        else if (qName.equals(SERIES_TAG)) {
104            CategorySeriesHandler subhandler = new CategorySeriesHandler(this);
105            getSubHandlers().push(subhandler);
106            subhandler.startElement(namespaceURI, localName, qName, atts);
107        }
108        else {
109            throw new SAXException("Element not recognised: " + qName);
110        }
111
112    }
113
114    /**
115     * The end of an element.
116     *
117     * @param namespaceURI  the namespace.
118     * @param localName  the element name.
119     * @param qName  the element name.
120     *
121     * @throws SAXException for errors.
122     */
123    @Override
124    public void endElement(String namespaceURI,
125                           String localName,
126                           String qName) throws SAXException {
127
128        DefaultHandler current = getCurrentHandler();
129        if (current != this) {
130            current.endElement(namespaceURI, localName, qName);
131        }
132
133    }
134
135}