001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ---------------- 028 * ItemHandler.java 029 * ---------------- 030 * (C) Copyright 2003-2008, 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 041package org.jfree.data.xml; 042 043import org.xml.sax.Attributes; 044import org.xml.sax.SAXException; 045import org.xml.sax.helpers.DefaultHandler; 046 047/** 048 * A handler for reading key-value items. 049 */ 050public class ItemHandler extends DefaultHandler implements DatasetTags { 051 052 /** The root handler. */ 053 private RootHandler root; 054 055 /** The parent handler (can be the same as root, but not always). */ 056 private DefaultHandler parent; 057 058 /** The key. */ 059 private Comparable key; 060 061 /** The value. */ 062 private Number value; 063 064 /** 065 * Creates a new item handler. 066 * 067 * @param root the root handler. 068 * @param parent the parent handler. 069 */ 070 public ItemHandler(RootHandler root, DefaultHandler parent) { 071 this.root = root; 072 this.parent = parent; 073 this.key = null; 074 this.value = null; 075 } 076 077 /** 078 * Returns the key that has been read by the handler, or <code>null</code>. 079 * 080 * @return The key. 081 */ 082 public Comparable getKey() { 083 return this.key; 084 } 085 086 /** 087 * Sets the key. 088 * 089 * @param key the key. 090 */ 091 public void setKey(Comparable key) { 092 this.key = key; 093 } 094 095 /** 096 * Returns the key that has been read by the handler, or <code>null</code>. 097 * 098 * @return The value. 099 */ 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 @Override 124 public void startElement(String namespaceURI, 125 String localName, 126 String qName, 127 Attributes atts) throws SAXException { 128 129 if (qName.equals(ITEM_TAG)) { 130 KeyHandler subhandler = new KeyHandler(this.root, this); 131 this.root.pushSubHandler(subhandler); 132 } 133 else if (qName.equals(VALUE_TAG)) { 134 ValueHandler subhandler = new ValueHandler(this.root, this); 135 this.root.pushSubHandler(subhandler); 136 } 137 else { 138 throw new SAXException( 139 "Expected <Item> or <Value>...found " + qName 140 ); 141 } 142 143 } 144 145 /** 146 * The end of an element. 147 * 148 * @param namespaceURI the namespace. 149 * @param localName the element name. 150 * @param qName the element name. 151 */ 152 @Override 153 public void endElement(String namespaceURI, 154 String localName, 155 String qName) { 156 157 if (this.parent instanceof PieDatasetHandler) { 158 PieDatasetHandler handler = (PieDatasetHandler) this.parent; 159 handler.addItem(this.key, this.value); 160 this.root.popSubHandler(); 161 } 162 else if (this.parent instanceof CategorySeriesHandler) { 163 CategorySeriesHandler handler = (CategorySeriesHandler) this.parent; 164 handler.addItem(this.key, this.value); 165 this.root.popSubHandler(); 166 } 167 168 } 169 170}