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