Source for org.jfree.data.xml.CategoryDatasetHandler

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2005, 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:  * CategoryDatasetHandler.java
  29:  * ---------------------------
  30:  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: CategoryDatasetHandler.java,v 1.3.2.1 2005/10/25 21:36:10 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 23-Jan-2003 : Version 1 (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.data.xml;
  44: 
  45: import org.jfree.data.category.CategoryDataset;
  46: import org.jfree.data.category.DefaultCategoryDataset;
  47: import org.xml.sax.Attributes;
  48: import org.xml.sax.SAXException;
  49: import org.xml.sax.helpers.DefaultHandler;
  50: 
  51: /**
  52:  * A SAX handler for reading a {@link CategoryDataset} from an XML file.
  53:  */
  54: public class CategoryDatasetHandler extends RootHandler implements DatasetTags {
  55: 
  56:     /** The dataset under construction. */
  57:     private DefaultCategoryDataset dataset;
  58: 
  59:     /**
  60:      * Creates a new handler.
  61:      */
  62:     public CategoryDatasetHandler() {
  63:         this.dataset = null;
  64:     }
  65: 
  66:     /**
  67:      * Returns the dataset.
  68:      *
  69:      * @return The dataset.
  70:      */
  71:     public CategoryDataset getDataset() {
  72:         return this.dataset;
  73:     }
  74: 
  75:     /**
  76:      * Adds an item to the dataset.
  77:      *
  78:      * @param rowKey  the row key.
  79:      * @param columnKey  the column key.
  80:      * @param value  the value.
  81:      */
  82:     public void addItem(Comparable rowKey, Comparable columnKey, Number value) {
  83:         this.dataset.addValue(value, rowKey, columnKey);
  84:     }
  85: 
  86:     /**
  87:      * The start of an element.
  88:      *
  89:      * @param namespaceURI  the namespace.
  90:      * @param localName  the element name.
  91:      * @param qName  the element name.
  92:      * @param atts  the element attributes.
  93:      *
  94:      * @throws SAXException for errors.
  95:      */
  96:     public void startElement(String namespaceURI,
  97:                              String localName,
  98:                              String qName,
  99:                              Attributes atts) throws SAXException {
 100: 
 101:         DefaultHandler current = getCurrentHandler();
 102:         if (current != this) {
 103:             current.startElement(namespaceURI, localName, qName, atts);
 104:         }
 105:         else if (qName.equals(CATEGORYDATASET_TAG)) {
 106:             this.dataset = new DefaultCategoryDataset();
 107:         }
 108:         else if (qName.equals(SERIES_TAG)) {
 109:             CategorySeriesHandler subhandler = new CategorySeriesHandler(this);
 110:             getSubHandlers().push(subhandler);
 111:             subhandler.startElement(namespaceURI, localName, qName, atts);
 112:         }
 113:         else {
 114:             throw new SAXException("Element not recognised: " + qName);
 115:         }
 116: 
 117:     }
 118: 
 119:     /**
 120:      * The end of an element.
 121:      *
 122:      * @param namespaceURI  the namespace.
 123:      * @param localName  the element name.
 124:      * @param qName  the element name.
 125:      *
 126:      * @throws SAXException for errors.
 127:      */
 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: }