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,
087 String localName,
088 String qName,
089 Attributes atts) throws SAXException {
090
091 if (qName.equals(VALUE_TAG)) {
092 // no attributes to read
093 clearCurrentText();
094 }
095 else {
096 throw new SAXException("Expecting <Value> but found " + qName);
097 }
098
099 }
100
101 /**
102 * The end of an element.
103 *
104 * @param namespaceURI the namespace.
105 * @param localName the element name.
106 * @param qName the element name.
107 *
108 * @throws SAXException for errors.
109 */
110 public void endElement(String namespaceURI,
111 String localName,
112 String qName) throws SAXException {
113
114 if (qName.equals(VALUE_TAG)) {
115 Number value;
116 try {
117 value = Double.valueOf(this.currentText.toString());
118 if (((Double) value).isNaN()) {
119 value = null;
120 }
121 }
122 catch (NumberFormatException e1) {
123 value = null;
124 }
125 this.itemHandler.setValue(value);
126 this.rootHandler.popSubHandler();
127 }
128 else {
129 throw new SAXException("Expecting </Value> but found " + qName);
130 }
131
132 }
133
134 /**
135 * Receives some (or all) of the text in the current element.
136 *
137 * @param ch character buffer.
138 * @param start the start index.
139 * @param length the length of the valid character data.
140 */
141 public void characters(char[] ch, int start, int length) {
142 if (this.currentText != null) {
143 this.currentText.append(String.copyValueOf(ch, start, length));
144 }
145 }
146
147 /**
148 * Returns the current text of the textbuffer.
149 *
150 * @return The current text.
151 */
152 protected String getCurrentText() {
153 return this.currentText.toString();
154 }
155
156 /**
157 * Removes all text from the textbuffer at the end of a CDATA section.
158 */
159 protected void clearCurrentText() {
160 this.currentText.delete(0, this.currentText.length());
161 }
162
163 }