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. * ItemHandler.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): -;
  33. *
  34. * $Id: ItemHandler.java,v 1.3 2005/02/13 22:06:48 mungady Exp $
  35. *
  36. * Changes (from 21-Jun-2001)
  37. * --------------------------
  38. * 23-Jan-2003 : Version 1 (DG);
  39. *
  40. */
  41. package org.jfree.data.xml;
  42. import org.xml.sax.Attributes;
  43. import org.xml.sax.SAXException;
  44. import org.xml.sax.helpers.DefaultHandler;
  45. /**
  46. * A handler for reading key-value items.
  47. */
  48. public class ItemHandler extends DefaultHandler implements DatasetTags {
  49. /** The root handler. */
  50. private RootHandler root;
  51. /** The parent handler (can be the same as root, but not always). */
  52. private DefaultHandler parent;
  53. /** The key. */
  54. private Comparable key;
  55. /** The value. */
  56. private Number value;
  57. /**
  58. * Creates a new item handler.
  59. *
  60. * @param root the root handler.
  61. * @param parent the parent handler.
  62. */
  63. public ItemHandler(RootHandler root, DefaultHandler parent) {
  64. this.root = root;
  65. this.parent = parent;
  66. this.key = null;
  67. this.value = null;
  68. }
  69. /**
  70. * Returns the key that has been read by the handler, or <code>null</code>.
  71. *
  72. * @return The key.
  73. */
  74. public Comparable getKey() {
  75. return this.key;
  76. }
  77. /**
  78. * Sets the key.
  79. *
  80. * @param key the key.
  81. */
  82. public void setKey(Comparable key) {
  83. this.key = key;
  84. }
  85. /**
  86. * Returns the key that has been read by the handler, or <code>null</code>.
  87. *
  88. * @return The value.
  89. */
  90. public Number getValue() {
  91. return this.value;
  92. }
  93. /**
  94. * Sets the value.
  95. *
  96. * @param value the value.
  97. */
  98. public void setValue(Number value) {
  99. this.value = value;
  100. }
  101. /**
  102. * The start of an element.
  103. *
  104. * @param namespaceURI the namespace.
  105. * @param localName the element name.
  106. * @param qName the element name.
  107. * @param atts the attributes.
  108. *
  109. * @throws SAXException for errors.
  110. */
  111. public void startElement(String namespaceURI,
  112. String localName,
  113. String qName,
  114. Attributes atts) throws SAXException {
  115. if (qName.equals(ITEM_TAG)) {
  116. KeyHandler subhandler = new KeyHandler(this.root, this);
  117. this.root.pushSubHandler(subhandler);
  118. }
  119. else if (qName.equals(VALUE_TAG)) {
  120. ValueHandler subhandler = new ValueHandler(this.root, this);
  121. this.root.pushSubHandler(subhandler);
  122. }
  123. else {
  124. throw new SAXException(
  125. "Expected <Item> or <Value>...found " + qName
  126. );
  127. }
  128. }
  129. /**
  130. * The end of an element.
  131. *
  132. * @param namespaceURI the namespace.
  133. * @param localName the element name.
  134. * @param qName the element name.
  135. */
  136. public void endElement(String namespaceURI,
  137. String localName,
  138. String qName) {
  139. if (this.parent instanceof PieDatasetHandler) {
  140. PieDatasetHandler handler = (PieDatasetHandler) this.parent;
  141. handler.addItem(this.key, this.value);
  142. this.root.popSubHandler();
  143. }
  144. else if (this.parent instanceof CategorySeriesHandler) {
  145. CategorySeriesHandler handler = (CategorySeriesHandler) this.parent;
  146. handler.addItem(this.key, this.value);
  147. this.root.popSubHandler();
  148. }
  149. }
  150. }