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