KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PieDatasetHandler.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: PieDatasetHandler.java,v 1.3 2005/02/13 22:07:05 mungady Exp $
35  *
36  * Changes (from 21-Jun-2001)
37  * --------------------------
38  * 23-Jan-2003 : Version 1 (DG);
39  *
40  */

41
42 package org.jfree.data.xml;
43
44 import org.jfree.data.general.DefaultPieDataset;
45 import org.jfree.data.general.PieDataset;
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 PieDataset} from an XML file.
52  */

53 public class PieDatasetHandler extends RootHandler implements DatasetTags {
54
55     /** The pie dataset under construction. */
56     private DefaultPieDataset dataset;
57
58     /**
59      * Default constructor.
60      */

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

70     public PieDataset getDataset() {
71         return this.dataset;
72     }
73
74     /**
75      * Adds an item to the dataset under construction.
76      *
77      * @param key the key.
78      * @param value the value.
79      */

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

94     public void startElement(String JavaDoc namespaceURI,
95                              String JavaDoc localName,
96                              String JavaDoc qName,
97                              Attributes JavaDoc atts) throws SAXException JavaDoc {
98
99         DefaultHandler JavaDoc current = getCurrentHandler();
100         if (current != this) {
101             current.startElement(namespaceURI, localName, qName, atts);
102         }
103         else if (qName.equals(PIEDATASET_TAG)) {
104             this.dataset = new DefaultPieDataset();
105         }
106         else if (qName.equals(ITEM_TAG)) {
107             ItemHandler subhandler = new ItemHandler(this, this);
108             getSubHandlers().push(subhandler);
109             subhandler.startElement(namespaceURI, localName, qName, atts);
110         }
111
112     }
113
114     /**
115      * The end of an element.
116      *
117      * @param namespaceURI the namespace.
118      * @param localName the element name.
119      * @param qName the element name.
120      *
121      * @throws SAXException for errors.
122      */

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