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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ---------------------------
  25. * CategoryDatasetHandler.java
  26. * ---------------------------
  27. * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: CategoryDatasetHandler.java,v 1.2 2005/01/14 17:28:40 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 23-Jan-2003 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.xml;
  40. import org.jfree.data.category.CategoryDataset;
  41. import org.jfree.data.category.DefaultCategoryDataset;
  42. import org.xml.sax.Attributes;
  43. import org.xml.sax.SAXException;
  44. import org.xml.sax.helpers.DefaultHandler;
  45. /**
  46. * A SAX handler for reading a {@link CategoryDataset} from an XML file.
  47. */
  48. public class CategoryDatasetHandler extends RootHandler implements DatasetTags {
  49. /** The dataset under construction. */
  50. private DefaultCategoryDataset dataset;
  51. /**
  52. * Creates a new handler.
  53. */
  54. public CategoryDatasetHandler() {
  55. this.dataset = null;
  56. }
  57. /**
  58. * Returns the dataset.
  59. *
  60. * @return The dataset.
  61. */
  62. public CategoryDataset getDataset() {
  63. return this.dataset;
  64. }
  65. /**
  66. * Adds an item to the dataset.
  67. *
  68. * @param rowKey the row key.
  69. * @param columnKey the column key.
  70. * @param value the value.
  71. */
  72. public void addItem(Comparable rowKey, Comparable columnKey, Number value) {
  73. this.dataset.addValue(value, rowKey, columnKey);
  74. }
  75. /**
  76. * The start of an element.
  77. *
  78. * @param namespaceURI the namespace.
  79. * @param localName the element name.
  80. * @param qName the element name.
  81. * @param atts the element attributes.
  82. *
  83. * @throws SAXException for errors.
  84. */
  85. public void startElement(String namespaceURI,
  86. String localName,
  87. String qName,
  88. Attributes atts) throws SAXException {
  89. DefaultHandler current = getCurrentHandler();
  90. if (current != this) {
  91. current.startElement(namespaceURI, localName, qName, atts);
  92. }
  93. else if (qName.equals(CATEGORYDATASET_TAG)) {
  94. this.dataset = new DefaultCategoryDataset();
  95. }
  96. else if (qName.equals(SERIES_TAG)) {
  97. CategorySeriesHandler subhandler = new CategorySeriesHandler(this);
  98. getSubHandlers().push(subhandler);
  99. subhandler.startElement(namespaceURI, localName, qName, atts);
  100. }
  101. else {
  102. throw new SAXException("Element not recognised: " + qName);
  103. }
  104. }
  105. /**
  106. * The end of an element.
  107. *
  108. * @param namespaceURI the namespace.
  109. * @param localName the element name.
  110. * @param qName the element name.
  111. *
  112. * @throws SAXException for errors.
  113. */
  114. public void endElement(String namespaceURI,
  115. String localName,
  116. String qName) throws SAXException {
  117. DefaultHandler current = getCurrentHandler();
  118. if (current != this) {
  119. current.endElement(namespaceURI, localName, qName);
  120. }
  121. }
  122. }