Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, 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 10: * under the terms of the GNU Lesser General Public License as published by 11: * the Free Software Foundation; either version 2.1 of the License, or 12: * (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but 15: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17: * License for more details. 18: * 19: * You should have received a copy of the GNU Lesser General Public 20: * License along with this library; if not, write to the Free Software 21: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22: * USA. 23: * 24: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * --------------- 28: * KeyHandler.java 29: * --------------- 30: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * Changes 36: * ------- 37: * 23-Jan-2003 : Version 1 (DG); 38: * 39: */ 40: 41: package org.jfree.data.xml; 42: 43: import org.xml.sax.Attributes; 44: import org.xml.sax.SAXException; 45: import org.xml.sax.helpers.DefaultHandler; 46: 47: /** 48: * A SAX handler for reading a key. 49: */ 50: public class KeyHandler extends DefaultHandler implements DatasetTags { 51: 52: /** The root handler. */ 53: private RootHandler rootHandler; 54: 55: /** The item handler. */ 56: private ItemHandler itemHandler; 57: 58: /** Storage for the current CDATA */ 59: private StringBuffer currentText; 60: 61: /** The key. */ 62: //private Comparable key; 63: 64: /** 65: * Creates a new handler. 66: * 67: * @param rootHandler the root handler. 68: * @param itemHandler the item handler. 69: */ 70: public KeyHandler(RootHandler rootHandler, ItemHandler itemHandler) { 71: this.rootHandler = rootHandler; 72: this.itemHandler = itemHandler; 73: this.currentText = new StringBuffer(); 74: //this.key = null; 75: } 76: 77: /** 78: * The start of an element. 79: * 80: * @param namespaceURI the namespace. 81: * @param localName the element name. 82: * @param qName the element name. 83: * @param atts the attributes. 84: * 85: * @throws SAXException for errors. 86: */ 87: public void startElement(String namespaceURI, 88: String localName, 89: String qName, 90: Attributes atts) throws SAXException { 91: 92: if (qName.equals(KEY_TAG)) { 93: clearCurrentText(); 94: } 95: else { 96: throw new SAXException("Expecting <Key> but found " + qName); 97: } 98: 99: } 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: }