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: * ItemHandler.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 handler for reading key-value items. 49: */ 50: public class ItemHandler extends DefaultHandler implements DatasetTags { 51: 52: /** The root handler. */ 53: private RootHandler root; 54: 55: /** The parent handler (can be the same as root, but not always). */ 56: private DefaultHandler parent; 57: 58: /** The key. */ 59: private Comparable key; 60: 61: /** The value. */ 62: private Number value; 63: 64: /** 65: * Creates a new item handler. 66: * 67: * @param root the root handler. 68: * @param parent the parent handler. 69: */ 70: public ItemHandler(RootHandler root, DefaultHandler parent) { 71: this.root = root; 72: this.parent = parent; 73: this.key = null; 74: this.value = null; 75: } 76: 77: /** 78: * Returns the key that has been read by the handler, or <code>null</code>. 79: * 80: * @return The key. 81: */ 82: public Comparable getKey() { 83: return this.key; 84: } 85: 86: /** 87: * Sets the key. 88: * 89: * @param key the key. 90: */ 91: public void setKey(Comparable key) { 92: this.key = key; 93: } 94: 95: /** 96: * Returns the key that has been read by the handler, or <code>null</code>. 97: * 98: * @return The value. 99: */ 100: public Number getValue() { 101: return this.value; 102: } 103: 104: /** 105: * Sets the value. 106: * 107: * @param value the value. 108: */ 109: public void setValue(Number value) { 110: this.value = value; 111: } 112: 113: /** 114: * The start of an element. 115: * 116: * @param namespaceURI the namespace. 117: * @param localName the element name. 118: * @param qName the element name. 119: * @param atts the attributes. 120: * 121: * @throws SAXException for errors. 122: */ 123: public void startElement(String namespaceURI, 124: String localName, 125: String qName, 126: Attributes atts) throws SAXException { 127: 128: if (qName.equals(ITEM_TAG)) { 129: KeyHandler subhandler = new KeyHandler(this.root, this); 130: this.root.pushSubHandler(subhandler); 131: } 132: else if (qName.equals(VALUE_TAG)) { 133: ValueHandler subhandler = new ValueHandler(this.root, this); 134: this.root.pushSubHandler(subhandler); 135: } 136: else { 137: throw new SAXException( 138: "Expected <Item> or <Value>...found " + qName 139: ); 140: } 141: 142: } 143: 144: /** 145: * The end of an element. 146: * 147: * @param namespaceURI the namespace. 148: * @param localName the element name. 149: * @param qName the element name. 150: */ 151: public void endElement(String namespaceURI, 152: String localName, 153: String qName) { 154: 155: if (this.parent instanceof PieDatasetHandler) { 156: PieDatasetHandler handler = (PieDatasetHandler) this.parent; 157: handler.addItem(this.key, this.value); 158: this.root.popSubHandler(); 159: } 160: else if (this.parent instanceof CategorySeriesHandler) { 161: CategorySeriesHandler handler = (CategorySeriesHandler) this.parent; 162: handler.addItem(this.key, this.value); 163: this.root.popSubHandler(); 164: } 165: 166: } 167: 168: }