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 * CategoryDatasetHandler.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.jfree.data.category.CategoryDataset; 044import org.jfree.data.category.DefaultCategoryDataset; 045import org.xml.sax.Attributes; 046import org.xml.sax.SAXException; 047import org.xml.sax.helpers.DefaultHandler; 048 049/** 050 * A SAX handler for reading a {@link CategoryDataset} from an XML file. 051 */ 052public class CategoryDatasetHandler extends RootHandler implements DatasetTags { 053 054 /** The dataset under construction. */ 055 private DefaultCategoryDataset dataset; 056 057 /** 058 * Creates a new handler. 059 */ 060 public CategoryDatasetHandler() { 061 this.dataset = null; 062 } 063 064 /** 065 * Returns the dataset. 066 * 067 * @return The dataset. 068 */ 069 public CategoryDataset getDataset() { 070 return this.dataset; 071 } 072 073 /** 074 * Adds an item to the dataset. 075 * 076 * @param rowKey the row key. 077 * @param columnKey the column key. 078 * @param value the value. 079 */ 080 public void addItem(Comparable rowKey, Comparable columnKey, Number value) { 081 this.dataset.addValue(value, rowKey, columnKey); 082 } 083 084 /** 085 * The start of an element. 086 * 087 * @param namespaceURI the namespace. 088 * @param localName the element name. 089 * @param qName the element name. 090 * @param atts the element attributes. 091 * 092 * @throws SAXException for errors. 093 */ 094 @Override 095 public void startElement(String namespaceURI, 096 String localName, 097 String qName, 098 Attributes atts) throws SAXException { 099 100 DefaultHandler current = getCurrentHandler(); 101 if (current != this) { 102 current.startElement(namespaceURI, localName, qName, atts); 103 } 104 else if (qName.equals(CATEGORYDATASET_TAG)) { 105 this.dataset = new DefaultCategoryDataset(); 106 } 107 else if (qName.equals(SERIES_TAG)) { 108 CategorySeriesHandler subhandler = new CategorySeriesHandler(this); 109 getSubHandlers().push(subhandler); 110 subhandler.startElement(namespaceURI, localName, qName, atts); 111 } 112 else { 113 throw new SAXException("Element not recognised: " + qName); 114 } 115 116 } 117 118 /** 119 * The end of an element. 120 * 121 * @param namespaceURI the namespace. 122 * @param localName the element name. 123 * @param qName the element name. 124 * 125 * @throws SAXException for errors. 126 */ 127 @Override 128 public void endElement(String namespaceURI, 129 String localName, 130 String qName) throws SAXException { 131 132 DefaultHandler current = getCurrentHandler(); 133 if (current != this) { 134 current.endElement(namespaceURI, localName, qName); 135 } 136 137 } 138 139}