001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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-2008, by Object Refinery Limited and Contributors.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Luke Quinane;
034 *
035 * Changes
036 * -------
037 * 23-Jan-2003 : Version 1 (DG);
038 * 25-Nov-2003 : Patch to handle 'NaN' values (DG);
039 *
040 */
041
042 package org.jfree.data.xml;
043
044 import org.xml.sax.Attributes;
045 import org.xml.sax.SAXException;
046 import org.xml.sax.helpers.DefaultHandler;
047
048 /**
049 * A handler for reading a 'Value' element.
050 */
051 public class ValueHandler extends DefaultHandler implements DatasetTags {
052
053 /** The root handler. */
054 private RootHandler rootHandler;
055
056 /** The item handler. */
057 private ItemHandler itemHandler;
058
059 /** Storage for the current CDATA */
060 private StringBuffer currentText;
061
062 /**
063 * Creates a new value handler.
064 *
065 * @param rootHandler the root handler.
066 * @param itemHandler the item handler.
067 */
068 public ValueHandler(RootHandler rootHandler, ItemHandler itemHandler) {
069 this.rootHandler = rootHandler;
070 this.itemHandler = itemHandler;
071 this.currentText = new StringBuffer();
072 }
073
074 /**
075 * The start of an element.
076 *
077 * @param namespaceURI the namespace.
078 * @param localName the element name.
079 * @param qName the element name.
080 * @param atts the attributes.
081 *
082 * @throws SAXException for errors.
083 */
084 public void startElement(String namespaceURI,
085 String localName,
086 String qName,
087 Attributes atts) throws SAXException {
088
089 if (qName.equals(VALUE_TAG)) {
090 // no attributes to read
091 clearCurrentText();
092 }
093 else {
094 throw new SAXException("Expecting <Value> but found " + 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,
109 String localName,
110 String qName) throws SAXException {
111
112 if (qName.equals(VALUE_TAG)) {
113 Number value;
114 try {
115 value = Double.valueOf(this.currentText.toString());
116 if (((Double) value).isNaN()) {
117 value = null;
118 }
119 }
120 catch (NumberFormatException e1) {
121 value = null;
122 }
123 this.itemHandler.setValue(value);
124 this.rootHandler.popSubHandler();
125 }
126 else {
127 throw new SAXException("Expecting </Value> but found " + qName);
128 }
129
130 }
131
132 /**
133 * Receives some (or all) of the text in the current element.
134 *
135 * @param ch character buffer.
136 * @param start the start index.
137 * @param length the length of the valid character data.
138 */
139 public void characters(char[] ch, int start, int length) {
140 if (this.currentText != null) {
141 this.currentText.append(String.copyValueOf(ch, start, length));
142 }
143 }
144
145 /**
146 * Returns the current text of the textbuffer.
147 *
148 * @return The current text.
149 */
150 protected String getCurrentText() {
151 return this.currentText.toString();
152 }
153
154 /**
155 * Removes all text from the textbuffer at the end of a CDATA section.
156 */
157 protected void clearCurrentText() {
158 this.currentText.delete(0, this.currentText.length());
159 }
160
161 }