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 * ItemHandler.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.xml.sax.Attributes;
040import org.xml.sax.SAXException;
041import org.xml.sax.helpers.DefaultHandler;
042
043/**
044 * A handler for reading key-value items.
045 */
046public class ItemHandler extends DefaultHandler implements DatasetTags {
047
048    /** The root handler. */
049    private RootHandler root;
050
051    /** The parent handler (can be the same as root, but not always). */
052    private DefaultHandler parent;
053
054    /** The key. */
055    private Comparable key;
056
057    /** The value. */
058    private Number value;
059
060    /**
061     * Creates a new item handler.
062     *
063     * @param root  the root handler.
064     * @param parent  the parent handler.
065     */
066    public ItemHandler(RootHandler root, DefaultHandler parent) {
067        this.root = root;
068        this.parent = parent;
069        this.key = null;
070        this.value = null;
071    }
072
073    /**
074     * Returns the key that has been read by the handler, or {@code null}.
075     *
076     * @return The key.
077     */
078    public Comparable getKey() {
079        return this.key;
080    }
081
082    /**
083     * Sets the key.
084     *
085     * @param key  the key.
086     */
087    public void setKey(Comparable key) {
088        this.key = key;
089    }
090
091    /**
092     * Returns the key that has been read by the handler, or {@code null}.
093     *
094     * @return The value.
095     */
096    public Number getValue() {
097        return this.value;
098    }
099
100    /**
101     * Sets the value.
102     *
103     * @param value  the value.
104     */
105    public void setValue(Number value) {
106        this.value = value;
107    }
108
109    /**
110     * The start of an element.
111     *
112     * @param namespaceURI  the namespace.
113     * @param localName  the element name.
114     * @param qName  the element name.
115     * @param atts  the attributes.
116     *
117     * @throws SAXException for errors.
118     */
119    @Override
120    public void startElement(String namespaceURI,
121                             String localName,
122                             String qName,
123                             Attributes atts) throws SAXException {
124
125        if (qName.equals(ITEM_TAG)) {
126            KeyHandler subhandler = new KeyHandler(this.root, this);
127            this.root.pushSubHandler(subhandler);
128        }
129        else if (qName.equals(VALUE_TAG)) {
130            ValueHandler subhandler = new ValueHandler(this.root, this);
131            this.root.pushSubHandler(subhandler);
132        }
133        else {
134            throw new SAXException(
135                "Expected <Item> or <Value>...found " + qName
136            );
137        }
138
139    }
140
141    /**
142     * The end of an element.
143     *
144     * @param namespaceURI  the namespace.
145     * @param localName  the element name.
146     * @param qName  the element name.
147     */
148    @Override
149    public void endElement(String namespaceURI,
150                           String localName,
151                           String qName) {
152
153        if (this.parent instanceof PieDatasetHandler) {
154            PieDatasetHandler handler = (PieDatasetHandler) this.parent;
155            handler.addItem(this.key, this.value);
156            this.root.popSubHandler();
157        }
158        else if (this.parent instanceof CategorySeriesHandler) {
159            CategorySeriesHandler handler = (CategorySeriesHandler) this.parent;
160            handler.addItem(this.key, this.value);
161            this.root.popSubHandler();
162        }
163
164    }
165
166}