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