KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CategoryDatasetHandler.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: CategoryDatasetHandler.java,v 1.3 2005/03/15 17:23:43 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 org.jfree.data.category.CategoryDataset;
45 import org.jfree.data.category.DefaultCategoryDataset;
46 import org.xml.sax.Attributes JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48 import org.xml.sax.helpers.DefaultHandler JavaDoc;
49
50 /**
51  * A SAX handler for reading a {@link CategoryDataset} from an XML file.
52  */

53 public class CategoryDatasetHandler extends RootHandler implements DatasetTags {
54
55     /** The dataset under construction. */
56     private DefaultCategoryDataset dataset;
57
58     /**
59      * Creates a new handler.
60      */

61     public CategoryDatasetHandler() {
62         this.dataset = null;
63     }
64
65     /**
66      * Returns the dataset.
67      *
68      * @return The dataset.
69      */

70     public CategoryDataset getDataset() {
71         return this.dataset;
72     }
73
74     /**
75      * Adds an item to the dataset.
76      *
77      * @param rowKey the row key.
78      * @param columnKey the column key.
79      * @param value the value.
80      */

81     public void addItem(Comparable JavaDoc rowKey, Comparable JavaDoc columnKey, Number JavaDoc value) {
82         this.dataset.addValue(value, rowKey, columnKey);
83     }
84
85     /**
86      * The start of an element.
87      *
88      * @param namespaceURI the namespace.
89      * @param localName the element name.
90      * @param qName the element name.
91      * @param atts the element attributes.
92      *
93      * @throws SAXException for errors.
94      */

95     public void startElement(String JavaDoc namespaceURI,
96                              String JavaDoc localName,
97                              String JavaDoc qName,
98                              Attributes JavaDoc atts) throws SAXException JavaDoc {
99
100         DefaultHandler JavaDoc current = getCurrentHandler();
101         if (current != this) {
102             current.startElement(namespaceURI, localName, qName, atts);
103         }
104         else if (qName.equals(CATEGORYDATASET_TAG)) {
105             this.dataset = new DefaultCategoryDataset();
106         }
107         else if (qName.equals(SERIES_TAG)) {
108             CategorySeriesHandler subhandler = new CategorySeriesHandler(this);
109             getSubHandlers().push(subhandler);
110             subhandler.startElement(namespaceURI, localName, qName, atts);
111         }
112         else {
113             throw new SAXException JavaDoc("Element not recognised: " + qName);
114         }
115
116     }
117
118     /**
119      * The end of an element.
120      *
121      * @param namespaceURI the namespace.
122      * @param localName the element name.
123      * @param qName the element name.
124      *
125      * @throws SAXException for errors.
126      */

127     public void endElement(String JavaDoc namespaceURI,
128                            String JavaDoc localName,
129                            String JavaDoc qName) throws SAXException JavaDoc {
130
131         DefaultHandler JavaDoc current = getCurrentHandler();
132         if (current != this) {
133             current.endElement(namespaceURI, localName, qName);
134         }
135
136     }
137
138 }
139
Popular Tags