KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ValueHandler.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): Luke Quinane;
33  *
34  * $Id: ValueHandler.java,v 1.3 2005/02/13 22:08:39 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 23-Jan-2003 : Version 1 (DG);
39  * 25-Nov-2003 : Patch to handle 'NaN' values (DG);
40  *
41  */

42
43 package org.jfree.data.xml;
44
45 import org.xml.sax.Attributes JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47 import org.xml.sax.helpers.DefaultHandler JavaDoc;
48
49 /**
50  * A handler for reading a 'Value' element.
51  */

52 public class ValueHandler extends DefaultHandler JavaDoc implements DatasetTags {
53
54     /** The root handler. */
55     private RootHandler rootHandler;
56
57     /** The item handler. */
58     private ItemHandler itemHandler;
59
60     /** Storage for the current CDATA */
61     private StringBuffer JavaDoc currentText;
62
63     /**
64      * Creates a new value handler.
65      *
66      * @param rootHandler the root handler.
67      * @param itemHandler the item handler.
68      */

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

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

109     public void endElement(String JavaDoc namespaceURI,
110                            String JavaDoc localName,
111                            String JavaDoc qName) throws SAXException JavaDoc {
112
113         if (qName.equals(VALUE_TAG)) {
114             Number JavaDoc value;
115             try {
116                 value = Double.valueOf(this.currentText.toString());
117                 if (((Double JavaDoc) value).isNaN()) {
118                     value = null;
119                 }
120             }
121             catch (NumberFormatException JavaDoc e1) {
122                 value = null;
123             }
124             this.itemHandler.setValue(value);
125             this.rootHandler.popSubHandler();
126         }
127         else {
128             throw new SAXException JavaDoc("Expecting </Value> but found " + qName);
129         }
130
131     }
132
133     /**
134      * Receives some (or all) of the text in the current element.
135      *
136      * @param ch character buffer.
137      * @param start the start index.
138      * @param length the length of the valid character data.
139      */

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

151     protected String JavaDoc getCurrentText() {
152         return this.currentText.toString();
153     }
154
155     /**
156      * Removes all text from the textbuffer at the end of a CDATA section.
157      */

158     protected void clearCurrentText() {
159         this.currentText.delete(0, this.currentText.length());
160     }
161
162 }
163
Popular Tags