KickJava   Java API By Example, From Geeks To Geeks.

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


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  * KeyHandler.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: KeyHandler.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.xml.sax.Attributes JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46 import org.xml.sax.helpers.DefaultHandler JavaDoc;
47
48 /**
49  * A SAX handler for reading a key.
50  */

51 public class KeyHandler extends DefaultHandler JavaDoc implements DatasetTags {
52
53     /** The root handler. */
54     private RootHandler rootHandler;
55
56     /** The item handler. */
57     private ItemHandler itemHandler;
58
59     /** Storage for the current CDATA */
60     private StringBuffer JavaDoc currentText;
61
62     /** The key. */
63     //private Comparable key;
64

65     /**
66      * Creates a new handler.
67      *
68      * @param rootHandler the root handler.
69      * @param itemHandler the item handler.
70      */

71     public KeyHandler(RootHandler rootHandler, ItemHandler itemHandler) {
72         this.rootHandler = rootHandler;
73         this.itemHandler = itemHandler;
74         this.currentText = new StringBuffer JavaDoc();
75         //this.key = null;
76
}
77
78     /**
79      * The start of an element.
80      *
81      * @param namespaceURI the namespace.
82      * @param localName the element name.
83      * @param qName the element name.
84      * @param atts the attributes.
85      *
86      * @throws SAXException for errors.
87      */

88     public void startElement(String JavaDoc namespaceURI,
89                              String JavaDoc localName,
90                              String JavaDoc qName,
91                              Attributes JavaDoc atts) throws SAXException JavaDoc {
92
93         if (qName.equals(KEY_TAG)) {
94             clearCurrentText();
95         }
96         else {
97             throw new SAXException JavaDoc("Expecting <Key> but found " + qName);
98         }
99
100     }
101
102     /**
103      * The end of an element.
104      *
105      * @param namespaceURI the namespace.
106      * @param localName the element name.
107      * @param qName the element name.
108      *
109      * @throws SAXException for errors.
110      */

111     public void endElement(String JavaDoc namespaceURI,
112                            String JavaDoc localName,
113                            String JavaDoc qName) throws SAXException JavaDoc {
114
115         if (qName.equals(KEY_TAG)) {
116             this.itemHandler.setKey(getCurrentText());
117             this.rootHandler.popSubHandler();
118             this.rootHandler.pushSubHandler(
119                 new ValueHandler(this.rootHandler, this.itemHandler)
120             );
121         }
122         else {
123             throw new SAXException JavaDoc("Expecting </Key> but found " + qName);
124         }
125
126     }
127
128     /**
129      * Receives some (or all) of the text in the current element.
130      *
131      * @param ch character buffer.
132      * @param start the start index.
133      * @param length the length of the valid character data.
134      */

135     public void characters(char[] ch, int start, int length) {
136         if (this.currentText != null) {
137             this.currentText.append(String.copyValueOf(ch, start, length));
138         }
139     }
140
141     /**
142      * Returns the current text of the textbuffer.
143      *
144      * @return The current text.
145      */

146     protected String JavaDoc getCurrentText() {
147         return this.currentText.toString();
148     }
149
150     /**
151      * Removes all text from the textbuffer at the end of a CDATA section.
152      */

153     protected void clearCurrentText() {
154         this.currentText.delete(0, this.currentText.length());
155     }
156
157 }
158
Popular Tags