001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ---------------
028 * KeyHandler.java
029 * ---------------
030 * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * Changes
036 * -------
037 * 23-Jan-2003 : Version 1 (DG);
038 *
039 */
040
041 package org.jfree.data.xml;
042
043 import org.xml.sax.Attributes;
044 import org.xml.sax.SAXException;
045 import org.xml.sax.helpers.DefaultHandler;
046
047 /**
048 * A SAX handler for reading a key.
049 */
050 public class KeyHandler extends DefaultHandler implements DatasetTags {
051
052 /** The root handler. */
053 private RootHandler rootHandler;
054
055 /** The item handler. */
056 private ItemHandler itemHandler;
057
058 /** Storage for the current CDATA */
059 private StringBuffer currentText;
060
061 /** The key. */
062 //private Comparable key;
063
064 /**
065 * Creates a new handler.
066 *
067 * @param rootHandler the root handler.
068 * @param itemHandler the item handler.
069 */
070 public KeyHandler(RootHandler rootHandler, ItemHandler itemHandler) {
071 this.rootHandler = rootHandler;
072 this.itemHandler = itemHandler;
073 this.currentText = new StringBuffer();
074 //this.key = null;
075 }
076
077 /**
078 * The start of an element.
079 *
080 * @param namespaceURI the namespace.
081 * @param localName the element name.
082 * @param qName the element name.
083 * @param atts the attributes.
084 *
085 * @throws SAXException for errors.
086 */
087 public void startElement(String namespaceURI,
088 String localName,
089 String qName,
090 Attributes atts) throws SAXException {
091
092 if (qName.equals(KEY_TAG)) {
093 clearCurrentText();
094 }
095 else {
096 throw new SAXException("Expecting <Key> but found " + qName);
097 }
098
099 }
100
101 /**
102 * The end of an element.
103 *
104 * @param namespaceURI the namespace.
105 * @param localName the element name.
106 * @param qName the element name.
107 *
108 * @throws SAXException for errors.
109 */
110 public void endElement(String namespaceURI,
111 String localName,
112 String qName) throws SAXException {
113
114 if (qName.equals(KEY_TAG)) {
115 this.itemHandler.setKey(getCurrentText());
116 this.rootHandler.popSubHandler();
117 this.rootHandler.pushSubHandler(
118 new ValueHandler(this.rootHandler, this.itemHandler)
119 );
120 }
121 else {
122 throw new SAXException("Expecting </Key> but found " + qName);
123 }
124
125 }
126
127 /**
128 * Receives some (or all) of the text in the current element.
129 *
130 * @param ch character buffer.
131 * @param start the start index.
132 * @param length the length of the valid character data.
133 */
134 public void characters(char[] ch, int start, int length) {
135 if (this.currentText != null) {
136 this.currentText.append(String.copyValueOf(ch, start, length));
137 }
138 }
139
140 /**
141 * Returns the current text of the textbuffer.
142 *
143 * @return The current text.
144 */
145 protected String getCurrentText() {
146 return this.currentText.toString();
147 }
148
149 /**
150 * Removes all text from the textbuffer at the end of a CDATA section.
151 */
152 protected void clearCurrentText() {
153 this.currentText.delete(0, this.currentText.length());
154 }
155
156 }