001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: KeyHandler.java,v 1.3.2.1 2005/10/25 21:36:10 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 23-Jan-2003 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.xml;
044:
045: import org.xml.sax.Attributes;
046: import org.xml.sax.SAXException;
047: import org.xml.sax.helpers.DefaultHandler;
048:
049: /**
050: * A SAX handler for reading a key.
051: */
052: public class KeyHandler extends DefaultHandler implements DatasetTags {
053:
054: /** The root handler. */
055: private RootHandler rootHandler;
056:
057: /** The item handler. */
058: private ItemHandler itemHandler;
059:
060: /** Storage for the current CDATA */
061: private StringBuffer currentText;
062:
063: /** The key. */
064: //private Comparable key;
065: /**
066: * Creates a new handler.
067: *
068: * @param rootHandler the root handler.
069: * @param itemHandler the item handler.
070: */
071: public KeyHandler(RootHandler rootHandler, ItemHandler itemHandler) {
072: this .rootHandler = rootHandler;
073: this .itemHandler = itemHandler;
074: this .currentText = new StringBuffer();
075: //this.key = null;
076: }
077:
078: /**
079: * The start of an element.
080: *
081: * @param namespaceURI the namespace.
082: * @param localName the element name.
083: * @param qName the element name.
084: * @param atts the attributes.
085: *
086: * @throws SAXException for errors.
087: */
088: public void startElement(String namespaceURI, String localName,
089: String qName, Attributes atts) throws SAXException {
090:
091: if (qName.equals(KEY_TAG)) {
092: clearCurrentText();
093: } else {
094: throw new SAXException("Expecting <Key> but found " + qName);
095: }
096:
097: }
098:
099: /**
100: * The end of an element.
101: *
102: * @param namespaceURI the namespace.
103: * @param localName the element name.
104: * @param qName the element name.
105: *
106: * @throws SAXException for errors.
107: */
108: public void endElement(String namespaceURI, String localName,
109: String qName) throws SAXException {
110:
111: if (qName.equals(KEY_TAG)) {
112: this .itemHandler.setKey(getCurrentText());
113: this .rootHandler.popSubHandler();
114: this .rootHandler.pushSubHandler(new ValueHandler(
115: this .rootHandler, this .itemHandler));
116: } else {
117: throw new SAXException("Expecting </Key> but found "
118: + qName);
119: }
120:
121: }
122:
123: /**
124: * Receives some (or all) of the text in the current element.
125: *
126: * @param ch character buffer.
127: * @param start the start index.
128: * @param length the length of the valid character data.
129: */
130: public void characters(char[] ch, int start, int length) {
131: if (this .currentText != null) {
132: this .currentText.append(String.copyValueOf(ch, start,
133: length));
134: }
135: }
136:
137: /**
138: * Returns the current text of the textbuffer.
139: *
140: * @return The current text.
141: */
142: protected String getCurrentText() {
143: return this .currentText.toString();
144: }
145:
146: /**
147: * Removes all text from the textbuffer at the end of a CDATA section.
148: */
149: protected void clearCurrentText() {
150: this .currentText.delete(0, this.currentText.length());
151: }
152:
153: }
|