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 /** 067 * Creates a new handler. 068 * 069 * @param rootHandler the root handler. 070 * @param itemHandler the item handler. 071 */ 072 public KeyHandler(RootHandler rootHandler, ItemHandler itemHandler) { 073 this.rootHandler = rootHandler; 074 this.itemHandler = itemHandler; 075 this.currentText = new StringBuffer(); 076 //this.key = null; 077 } 078 079 /** 080 * The start of an element. 081 * 082 * @param namespaceURI the namespace. 083 * @param localName the element name. 084 * @param qName the element name. 085 * @param atts the attributes. 086 * 087 * @throws SAXException for errors. 088 */ 089 public void startElement(String namespaceURI, 090 String localName, 091 String qName, 092 Attributes atts) throws SAXException { 093 094 if (qName.equals(KEY_TAG)) { 095 clearCurrentText(); 096 } 097 else { 098 throw new SAXException("Expecting <Key> but found " + qName); 099 } 100 101 } 102 103 /** 104 * The end of an element. 105 * 106 * @param namespaceURI the namespace. 107 * @param localName the element name. 108 * @param qName the element name. 109 * 110 * @throws SAXException for errors. 111 */ 112 public void endElement(String namespaceURI, 113 String localName, 114 String qName) throws SAXException { 115 116 if (qName.equals(KEY_TAG)) { 117 this.itemHandler.setKey(getCurrentText()); 118 this.rootHandler.popSubHandler(); 119 this.rootHandler.pushSubHandler( 120 new ValueHandler(this.rootHandler, this.itemHandler) 121 ); 122 } 123 else { 124 throw new SAXException("Expecting </Key> but found " + qName); 125 } 126 127 } 128 129 /** 130 * Receives some (or all) of the text in the current element. 131 * 132 * @param ch character buffer. 133 * @param start the start index. 134 * @param length the length of the valid character data. 135 */ 136 public void characters(char[] ch, int start, int length) { 137 if (this.currentText != null) { 138 this.currentText.append(String.copyValueOf(ch, start, length)); 139 } 140 } 141 142 /** 143 * Returns the current text of the textbuffer. 144 * 145 * @return The current text. 146 */ 147 protected String getCurrentText() { 148 return this.currentText.toString(); 149 } 150 151 /** 152 * Removes all text from the textbuffer at the end of a CDATA section. 153 */ 154 protected void clearCurrentText() { 155 this.currentText.delete(0, this.currentText.length()); 156 } 157 158 }