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. * CategorySeriesHandler.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: CategorySeriesHandler.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 java.util.Iterator;
  41. import org.jfree.data.DefaultKeyedValues;
  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 a series for a category dataset.
  47. */
  48. public class CategorySeriesHandler extends DefaultHandler implements DatasetTags {
  49. /** The root handler. */
  50. private RootHandler root;
  51. /** The series name. */
  52. private String seriesName;
  53. /** The values. */
  54. private DefaultKeyedValues values;
  55. /**
  56. * Creates a new item handler.
  57. *
  58. * @param root the root handler.
  59. */
  60. public CategorySeriesHandler(RootHandler root) {
  61. this.root = root;
  62. this.values = new DefaultKeyedValues();
  63. }
  64. /**
  65. * Sets the series name.
  66. *
  67. * @param name the name.
  68. */
  69. public void setSeriesName(String name) {
  70. this.seriesName = name;
  71. }
  72. /**
  73. * Adds an item to the temporary storage for the series.
  74. *
  75. * @param key the key.
  76. * @param value the value.
  77. */
  78. public void addItem(Comparable key, final Number value) {
  79. this.values.addValue(key, value);
  80. }
  81. /**
  82. * The start of an element.
  83. *
  84. * @param namespaceURI the namespace.
  85. * @param localName the element name.
  86. * @param qName the element name.
  87. * @param atts the attributes.
  88. *
  89. * @throws SAXException for errors.
  90. */
  91. public void startElement(String namespaceURI,
  92. String localName,
  93. String qName,
  94. Attributes atts) throws SAXException {
  95. if (qName.equals(SERIES_TAG)) {
  96. setSeriesName(atts.getValue("name"));
  97. ItemHandler subhandler = new ItemHandler(this.root, this);
  98. this.root.pushSubHandler(subhandler);
  99. }
  100. else if (qName.equals(ITEM_TAG)) {
  101. ItemHandler subhandler = new ItemHandler(this.root, this);
  102. this.root.pushSubHandler(subhandler);
  103. subhandler.startElement(namespaceURI, localName, qName, atts);
  104. }
  105. else {
  106. throw new SAXException("Expecting <Series> or <Item> tag...found " + qName);
  107. }
  108. }
  109. /**
  110. * The end of an element.
  111. *
  112. * @param namespaceURI the namespace.
  113. * @param localName the element name.
  114. * @param qName the element name.
  115. */
  116. public void endElement(String namespaceURI,
  117. String localName,
  118. String qName) {
  119. if (this.root instanceof CategoryDatasetHandler) {
  120. CategoryDatasetHandler handler = (CategoryDatasetHandler) this.root;
  121. Iterator iterator = this.values.getKeys().iterator();
  122. while (iterator.hasNext()) {
  123. Comparable key = (Comparable) iterator.next();
  124. Number value = this.values.getValue(key);
  125. handler.addItem(this.seriesName, key, value);
  126. }
  127. this.root.popSubHandler();
  128. }
  129. }
  130. }