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. * PieDatasetHandler.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: PieDatasetHandler.java,v 1.3 2005/02/13 22:07:05 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.jfree.data.general.DefaultPieDataset;
  43. import org.jfree.data.general.PieDataset;
  44. import org.xml.sax.Attributes;
  45. import org.xml.sax.SAXException;
  46. import org.xml.sax.helpers.DefaultHandler;
  47. /**
  48. * A SAX handler for reading a {@link PieDataset} from an XML file.
  49. */
  50. public class PieDatasetHandler extends RootHandler implements DatasetTags {
  51. /** The pie dataset under construction. */
  52. private DefaultPieDataset dataset;
  53. /**
  54. * Default constructor.
  55. */
  56. public PieDatasetHandler() {
  57. this.dataset = null;
  58. }
  59. /**
  60. * Returns the dataset.
  61. *
  62. * @return The dataset.
  63. */
  64. public PieDataset getDataset() {
  65. return this.dataset;
  66. }
  67. /**
  68. * Adds an item to the dataset under construction.
  69. *
  70. * @param key the key.
  71. * @param value the value.
  72. */
  73. public void addItem(Comparable key, Number value) {
  74. this.dataset.setValue(key, value);
  75. }
  76. /**
  77. * Starts an element.
  78. *
  79. * @param namespaceURI the namespace.
  80. * @param localName the element name.
  81. * @param qName the element name.
  82. * @param atts the element attributes.
  83. *
  84. * @throws SAXException for errors.
  85. */
  86. public void startElement(String namespaceURI,
  87. String localName,
  88. String qName,
  89. Attributes atts) throws SAXException {
  90. DefaultHandler current = getCurrentHandler();
  91. if (current != this) {
  92. current.startElement(namespaceURI, localName, qName, atts);
  93. }
  94. else if (qName.equals(PIEDATASET_TAG)) {
  95. this.dataset = new DefaultPieDataset();
  96. }
  97. else if (qName.equals(ITEM_TAG)) {
  98. ItemHandler subhandler = new ItemHandler(this, this);
  99. getSubHandlers().push(subhandler);
  100. subhandler.startElement(namespaceURI, localName, qName, atts);
  101. }
  102. }
  103. /**
  104. * The end of an element.
  105. *
  106. * @param namespaceURI the namespace.
  107. * @param localName the element name.
  108. * @param qName the element name.
  109. *
  110. * @throws SAXException for errors.
  111. */
  112. public void endElement(String namespaceURI,
  113. String localName,
  114. String qName) throws SAXException {
  115. DefaultHandler current = getCurrentHandler();
  116. if (current != this) {
  117. current.endElement(namespaceURI, localName, qName);
  118. }
  119. }
  120. }