1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * -----------------
  27. * ValueHandler.java
  28. * -----------------
  29. * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  30. *
  31. * Original Author: David Gilbert (for Object Refinery Limited);
  32. * Contributor(s): Luke Quinane;
  33. *
  34. * $Id: ValueHandler.java,v 1.3 2005/02/13 22:08:39 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 23-Jan-2003 : Version 1 (DG);
  39. * 25-Nov-2003 : Patch to handle 'NaN' values (DG);
  40. *
  41. */
  42. package org.jfree.data.xml;
  43. import org.xml.sax.Attributes;
  44. import org.xml.sax.SAXException;
  45. import org.xml.sax.helpers.DefaultHandler;
  46. /**
  47. * A handler for reading a 'Value' element.
  48. */
  49. public class ValueHandler extends DefaultHandler implements DatasetTags {
  50. /** The root handler. */
  51. private RootHandler rootHandler;
  52. /** The item handler. */
  53. private ItemHandler itemHandler;
  54. /** Storage for the current CDATA */
  55. private StringBuffer currentText;
  56. /**
  57. * Creates a new value handler.
  58. *
  59. * @param rootHandler the root handler.
  60. * @param itemHandler the item handler.
  61. */
  62. public ValueHandler(RootHandler rootHandler, ItemHandler itemHandler) {
  63. this.rootHandler = rootHandler;
  64. this.itemHandler = itemHandler;
  65. this.currentText = new StringBuffer();
  66. }
  67. /**
  68. * The start of an element.
  69. *
  70. * @param namespaceURI the namespace.
  71. * @param localName the element name.
  72. * @param qName the element name.
  73. * @param atts the attributes.
  74. *
  75. * @throws SAXException for errors.
  76. */
  77. public void startElement(String namespaceURI,
  78. String localName,
  79. String qName,
  80. Attributes atts) throws SAXException {
  81. if (qName.equals(VALUE_TAG)) {
  82. // no attributes to read
  83. clearCurrentText();
  84. }
  85. else {
  86. throw new SAXException("Expecting <Value> but found " + qName);
  87. }
  88. }
  89. /**
  90. * The end of an element.
  91. *
  92. * @param namespaceURI the namespace.
  93. * @param localName the element name.
  94. * @param qName the element name.
  95. *
  96. * @throws SAXException for errors.
  97. */
  98. public void endElement(String namespaceURI,
  99. String localName,
  100. String qName) throws SAXException {
  101. if (qName.equals(VALUE_TAG)) {
  102. Number value;
  103. try {
  104. value = Double.valueOf(this.currentText.toString());
  105. if (((Double) value).isNaN()) {
  106. value = null;
  107. }
  108. }
  109. catch (NumberFormatException e1) {
  110. value = null;
  111. }
  112. this.itemHandler.setValue(value);
  113. this.rootHandler.popSubHandler();
  114. }
  115. else {
  116. throw new SAXException("Expecting </Value> but found " + qName);
  117. }
  118. }
  119. /**
  120. * Receives some (or all) of the text in the current element.
  121. *
  122. * @param ch character buffer.
  123. * @param start the start index.
  124. * @param length the length of the valid character data.
  125. */
  126. public void characters(char[] ch, int start, int length) {
  127. if (this.currentText != null) {
  128. this.currentText.append(String.copyValueOf(ch, start, length));
  129. }
  130. }
  131. /**
  132. * Returns the current text of the textbuffer.
  133. *
  134. * @return The current text.
  135. */
  136. protected String getCurrentText() {
  137. return this.currentText.toString();
  138. }
  139. /**
  140. * Removes all text from the textbuffer at the end of a CDATA section.
  141. */
  142. protected void clearCurrentText() {
  143. this.currentText.delete(0, this.currentText.length());
  144. }
  145. }