KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ItemHandler.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: ItemHandler.java,v 1.3 2005/02/13 22:06:48 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.xml.sax.Attributes JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46 import org.xml.sax.helpers.DefaultHandler JavaDoc;
47
48 /**
49  * A handler for reading key-value items.
50  */

51 public class ItemHandler extends DefaultHandler JavaDoc implements DatasetTags {
52
53     /** The root handler. */
54     private RootHandler root;
55
56     /** The parent handler (can be the same as root, but not always). */
57     private DefaultHandler JavaDoc parent;
58
59     /** The key. */
60     private Comparable JavaDoc key;
61
62     /** The value. */
63     private Number JavaDoc value;
64
65     /**
66      * Creates a new item handler.
67      *
68      * @param root the root handler.
69      * @param parent the parent handler.
70      */

71     public ItemHandler(RootHandler root, DefaultHandler JavaDoc parent) {
72         this.root = root;
73         this.parent = parent;
74         this.key = null;
75         this.value = null;
76     }
77
78     /**
79      * Returns the key that has been read by the handler, or <code>null</code>.
80      *
81      * @return The key.
82      */

83     public Comparable JavaDoc getKey() {
84         return this.key;
85     }
86
87     /**
88      * Sets the key.
89      *
90      * @param key the key.
91      */

92     public void setKey(Comparable JavaDoc key) {
93         this.key = key;
94     }
95
96     /**
97      * Returns the key that has been read by the handler, or <code>null</code>.
98      *
99      * @return The value.
100      */

101     public Number JavaDoc getValue() {
102         return this.value;
103     }
104
105     /**
106      * Sets the value.
107      *
108      * @param value the value.
109      */

110     public void setValue(Number JavaDoc value) {
111         this.value = value;
112     }
113
114     /**
115      * The start of an element.
116      *
117      * @param namespaceURI the namespace.
118      * @param localName the element name.
119      * @param qName the element name.
120      * @param atts the attributes.
121      *
122      * @throws SAXException for errors.
123      */

124     public void startElement(String JavaDoc namespaceURI,
125                              String JavaDoc localName,
126                              String JavaDoc qName,
127                              Attributes JavaDoc atts) throws SAXException JavaDoc {
128
129         if (qName.equals(ITEM_TAG)) {
130             KeyHandler subhandler = new KeyHandler(this.root, this);
131             this.root.pushSubHandler(subhandler);
132         }
133         else if (qName.equals(VALUE_TAG)) {
134             ValueHandler subhandler = new ValueHandler(this.root, this);
135             this.root.pushSubHandler(subhandler);
136         }
137         else {
138             throw new SAXException JavaDoc(
139                 "Expected <Item> or <Value>...found " + qName
140             );
141         }
142
143     }
144
145     /**
146      * The end of an element.
147      *
148      * @param namespaceURI the namespace.
149      * @param localName the element name.
150      * @param qName the element name.
151      */

152     public void endElement(String JavaDoc namespaceURI,
153                            String JavaDoc localName,
154                            String JavaDoc qName) {
155
156         if (this.parent instanceof PieDatasetHandler) {
157             PieDatasetHandler handler = (PieDatasetHandler) this.parent;
158             handler.addItem(this.key, this.value);
159             this.root.popSubHandler();
160         }
161         else if (this.parent instanceof CategorySeriesHandler) {
162             CategorySeriesHandler handler = (CategorySeriesHandler) this.parent;
163             handler.addItem(this.key, this.value);
164             this.root.popSubHandler();
165         }
166
167     }
168
169 }
170
Popular Tags