001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -----------------
028: * ValueHandler.java
029: * -----------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): Luke Quinane;
034: *
035: * $Id: ValueHandler.java,v 1.3.2.1 2005/10/25 21:36:10 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 23-Jan-2003 : Version 1 (DG);
040: * 25-Nov-2003 : Patch to handle 'NaN' values (DG);
041: *
042: */
043:
044: package org.jfree.data.xml;
045:
046: import org.xml.sax.Attributes;
047: import org.xml.sax.SAXException;
048: import org.xml.sax.helpers.DefaultHandler;
049:
050: /**
051: * A handler for reading a 'Value' element.
052: */
053: public class ValueHandler extends DefaultHandler implements DatasetTags {
054:
055: /** The root handler. */
056: private RootHandler rootHandler;
057:
058: /** The item handler. */
059: private ItemHandler itemHandler;
060:
061: /** Storage for the current CDATA */
062: private StringBuffer currentText;
063:
064: /**
065: * Creates a new value handler.
066: *
067: * @param rootHandler the root handler.
068: * @param itemHandler the item handler.
069: */
070: public ValueHandler(RootHandler rootHandler, ItemHandler itemHandler) {
071: this .rootHandler = rootHandler;
072: this .itemHandler = itemHandler;
073: this .currentText = new StringBuffer();
074: }
075:
076: /**
077: * The start of an element.
078: *
079: * @param namespaceURI the namespace.
080: * @param localName the element name.
081: * @param qName the element name.
082: * @param atts the attributes.
083: *
084: * @throws SAXException for errors.
085: */
086: public void startElement(String namespaceURI, String localName,
087: String qName, Attributes atts) throws SAXException {
088:
089: if (qName.equals(VALUE_TAG)) {
090: // no attributes to read
091: clearCurrentText();
092: } else {
093: throw new SAXException("Expecting <Value> but found "
094: + qName);
095: }
096:
097: }
098:
099: /**
100: * The end of an element.
101: *
102: * @param namespaceURI the namespace.
103: * @param localName the element name.
104: * @param qName the element name.
105: *
106: * @throws SAXException for errors.
107: */
108: public void endElement(String namespaceURI, String localName,
109: String qName) throws SAXException {
110:
111: if (qName.equals(VALUE_TAG)) {
112: Number value;
113: try {
114: value = Double.valueOf(this .currentText.toString());
115: if (((Double) value).isNaN()) {
116: value = null;
117: }
118: } catch (NumberFormatException e1) {
119: value = null;
120: }
121: this .itemHandler.setValue(value);
122: this .rootHandler.popSubHandler();
123: } else {
124: throw new SAXException("Expecting </Value> but found "
125: + qName);
126: }
127:
128: }
129:
130: /**
131: * Receives some (or all) of the text in the current element.
132: *
133: * @param ch character buffer.
134: * @param start the start index.
135: * @param length the length of the valid character data.
136: */
137: public void characters(char[] ch, int start, int length) {
138: if (this .currentText != null) {
139: this .currentText.append(String.copyValueOf(ch, start,
140: length));
141: }
142: }
143:
144: /**
145: * Returns the current text of the textbuffer.
146: *
147: * @return The current text.
148: */
149: protected String getCurrentText() {
150: return this .currentText.toString();
151: }
152:
153: /**
154: * Removes all text from the textbuffer at the end of a CDATA section.
155: */
156: protected void clearCurrentText() {
157: this .currentText.delete(0, this.currentText.length());
158: }
159:
160: }
|