KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > xml > CategorySeriesHandler


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * --------------------------
27  * CategorySeriesHandler.java
28  * --------------------------
29  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: CategorySeriesHandler.java,v 1.4 2005/05/20 08:20:04 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 23-Jan-2003 : Version 1 (DG);
39  *
40  */

41
42 package org.jfree.data.xml;
43
44 import java.util.Iterator JavaDoc;
45
46 import org.jfree.data.DefaultKeyedValues;
47 import org.xml.sax.Attributes JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49 import org.xml.sax.helpers.DefaultHandler JavaDoc;
50
51 /**
52  * A handler for reading a series for a category dataset.
53  */

54 public class CategorySeriesHandler extends DefaultHandler JavaDoc
55                                    implements DatasetTags {
56
57     /** The root handler. */
58     private RootHandler root;
59
60     /** The series key. */
61     private Comparable JavaDoc seriesKey;
62
63     /** The values. */
64     private DefaultKeyedValues values;
65
66     /**
67      * Creates a new item handler.
68      *
69      * @param root the root handler.
70      */

71     public CategorySeriesHandler(RootHandler root) {
72         this.root = root;
73         this.values = new DefaultKeyedValues();
74     }
75
76     /**
77      * Sets the series key.
78      *
79      * @param key the key.
80      */

81     public void setSeriesKey(Comparable JavaDoc key) {
82         this.seriesKey = key;
83     }
84
85     /**
86      * Adds an item to the temporary storage for the series.
87      *
88      * @param key the key.
89      * @param value the value.
90      */

91     public void addItem(Comparable JavaDoc key, final Number JavaDoc value) {
92         this.values.addValue(key, value);
93     }
94
95     /**
96      * The start of an element.
97      *
98      * @param namespaceURI the namespace.
99      * @param localName the element name.
100      * @param qName the element name.
101      * @param atts the attributes.
102      *
103      * @throws SAXException for errors.
104      */

105     public void startElement(String JavaDoc namespaceURI,
106                              String JavaDoc localName,
107                              String JavaDoc qName,
108                              Attributes JavaDoc atts) throws SAXException JavaDoc {
109
110         if (qName.equals(SERIES_TAG)) {
111             setSeriesKey(atts.getValue("key"));
112             ItemHandler subhandler = new ItemHandler(this.root, this);
113             this.root.pushSubHandler(subhandler);
114         }
115         else if (qName.equals(ITEM_TAG)) {
116             ItemHandler subhandler = new ItemHandler(this.root, this);
117             this.root.pushSubHandler(subhandler);
118             subhandler.startElement(namespaceURI, localName, qName, atts);
119         }
120
121         else {
122             throw new SAXException JavaDoc(
123                 "Expecting <Series> or <Item> tag...found " + qName
124             );
125         }
126     }
127
128     /**
129      * The end of an element.
130      *
131      * @param namespaceURI the namespace.
132      * @param localName the element name.
133      * @param qName the element name.
134      */

135     public void endElement(String JavaDoc namespaceURI,
136                            String JavaDoc localName,
137                            String JavaDoc qName) {
138
139         if (this.root instanceof CategoryDatasetHandler) {
140             CategoryDatasetHandler handler = (CategoryDatasetHandler) this.root;
141
142             Iterator JavaDoc iterator = this.values.getKeys().iterator();
143             while (iterator.hasNext()) {
144                 Comparable JavaDoc key = (Comparable JavaDoc) iterator.next();
145                 Number JavaDoc value = this.values.getValue(key);
146                 handler.addItem(this.seriesKey, key, value);
147             }
148
149             this.root.popSubHandler();
150         }
151
152     }
153
154 }
155
Popular Tags