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. * KeyHandler.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: KeyHandler.java,v 1.2 2005/01/14 17:28:41 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 23-Jan-2003 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.xml;
  40. import org.xml.sax.Attributes;
  41. import org.xml.sax.SAXException;
  42. import org.xml.sax.helpers.DefaultHandler;
  43. /**
  44. * A SAX handler for reading a key.
  45. */
  46. public class KeyHandler extends DefaultHandler implements DatasetTags {
  47. /** The root handler. */
  48. private RootHandler rootHandler;
  49. /** The item handler. */
  50. private ItemHandler itemHandler;
  51. /** Storage for the current CDATA */
  52. private StringBuffer currentText;
  53. /** The key. */
  54. //private Comparable key;
  55. /**
  56. * Creates a new handler.
  57. *
  58. * @param rootHandler the root handler.
  59. * @param itemHandler the item handler.
  60. */
  61. public KeyHandler(RootHandler rootHandler, ItemHandler itemHandler) {
  62. this.rootHandler = rootHandler;
  63. this.itemHandler = itemHandler;
  64. this.currentText = new StringBuffer();
  65. //this.key = null;
  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(KEY_TAG)) {
  82. clearCurrentText();
  83. }
  84. else {
  85. throw new SAXException("Expecting <Key> but found " + qName);
  86. }
  87. }
  88. /**
  89. * The end of an element.
  90. *
  91. * @param namespaceURI the namespace.
  92. * @param localName the element name.
  93. * @param qName the element name.
  94. *
  95. * @throws SAXException for errors.
  96. */
  97. public void endElement(String namespaceURI,
  98. String localName,
  99. String qName) throws SAXException {
  100. if (qName.equals(KEY_TAG)) {
  101. this.itemHandler.setKey(getCurrentText());
  102. this.rootHandler.popSubHandler();
  103. this.rootHandler.pushSubHandler(new ValueHandler(this.rootHandler, this.itemHandler));
  104. }
  105. else {
  106. throw new SAXException("Expecting </Key> but found " + qName);
  107. }
  108. }
  109. /**
  110. * Receives some (or all) of the text in the current element.
  111. *
  112. * @param ch character buffer.
  113. * @param start the start index.
  114. * @param length the length of the valid character data.
  115. */
  116. public void characters(char[] ch, int start, int length) {
  117. if (this.currentText != null) {
  118. this.currentText.append(String.copyValueOf(ch, start, length));
  119. }
  120. }
  121. /**
  122. * Returns the current text of the textbuffer.
  123. *
  124. * @return The current text.
  125. */
  126. protected String getCurrentText() {
  127. return this.currentText.toString();
  128. }
  129. /**
  130. * Removes all text from the textbuffer at the end of a CDATA section.
  131. */
  132. protected void clearCurrentText() {
  133. this.currentText.delete(0, this.currentText.length());
  134. }
  135. }