001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 * [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-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 042package org.jfree.data.xml; 043 044import org.xml.sax.Attributes; 045import org.xml.sax.SAXException; 046import org.xml.sax.helpers.DefaultHandler; 047 048/** 049 * A handler for reading a 'Value' element. 050 */ 051public 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 @Override 085 public void startElement(String namespaceURI, 086 String localName, 087 String qName, 088 Attributes atts) throws SAXException { 089 090 if (qName.equals(VALUE_TAG)) { 091 // no attributes to read 092 clearCurrentText(); 093 } 094 else { 095 throw new SAXException("Expecting <Value> but found " + qName); 096 } 097 098 } 099 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 @Override 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 @Override 142 public void characters(char[] ch, int start, int length) { 143 if (this.currentText != null) { 144 this.currentText.append(String.copyValueOf(ch, start, length)); 145 } 146 } 147 148 /** 149 * Returns the current text of the textbuffer. 150 * 151 * @return The current text. 152 */ 153 protected String getCurrentText() { 154 return this.currentText.toString(); 155 } 156 157 /** 158 * Removes all text from the textbuffer at the end of a CDATA section. 159 */ 160 protected void clearCurrentText() { 161 this.currentText.delete(0, this.currentText.length()); 162 } 163 164}