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-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:  * CategoryDatasetHandler.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.jfree.data.category.CategoryDataset;
  44: import org.jfree.data.category.DefaultCategoryDataset;
  45: import org.xml.sax.Attributes;
  46: import org.xml.sax.SAXException;
  47: import org.xml.sax.helpers.DefaultHandler;
  48: 
  49: /**
  50:  * A SAX handler for reading a {@link CategoryDataset} from an XML file.
  51:  */
  52: public class CategoryDatasetHandler extends RootHandler implements DatasetTags {
  53: 
  54:     /** The dataset under construction. */
  55:     private DefaultCategoryDataset dataset;
  56: 
  57:     /**
  58:      * Creates a new handler.
  59:      */
  60:     public CategoryDatasetHandler() {
  61:         this.dataset = null;
  62:     }
  63: 
  64:     /**
  65:      * Returns the dataset.
  66:      *
  67:      * @return The dataset.
  68:      */
  69:     public CategoryDataset getDataset() {
  70:         return this.dataset;
  71:     }
  72: 
  73:     /**
  74:      * Adds an item to the dataset.
  75:      *
  76:      * @param rowKey  the row key.
  77:      * @param columnKey  the column key.
  78:      * @param value  the value.
  79:      */
  80:     public void addItem(Comparable rowKey, Comparable columnKey, Number value) {
  81:         this.dataset.addValue(value, rowKey, columnKey);
  82:     }
  83: 
  84:     /**
  85:      * The start of an element.
  86:      *
  87:      * @param namespaceURI  the namespace.
  88:      * @param localName  the element name.
  89:      * @param qName  the element name.
  90:      * @param atts  the element attributes.
  91:      *
  92:      * @throws SAXException for errors.
  93:      */
  94:     public void startElement(String namespaceURI,
  95:                              String localName,
  96:                              String qName,
  97:                              Attributes atts) throws SAXException {
  98: 
  99:         DefaultHandler current = getCurrentHandler();
 100:         if (current != this) {
 101:             current.startElement(namespaceURI, localName, qName, atts);
 102:         }
 103:         else if (qName.equals(CATEGORYDATASET_TAG)) {
 104:             this.dataset = new DefaultCategoryDataset();
 105:         }
 106:         else if (qName.equals(SERIES_TAG)) {
 107:             CategorySeriesHandler subhandler = new CategorySeriesHandler(this);
 108:             getSubHandlers().push(subhandler);
 109:             subhandler.startElement(namespaceURI, localName, qName, atts);
 110:         }
 111:         else {
 112:             throw new SAXException("Element not recognised: " + qName);
 113:         }
 114: 
 115:     }
 116: 
 117:     /**
 118:      * The end of an element.
 119:      *
 120:      * @param namespaceURI  the namespace.
 121:      * @param localName  the element name.
 122:      * @param qName  the element name.
 123:      *
 124:      * @throws SAXException for errors.
 125:      */
 126:     public void endElement(String namespaceURI,
 127:                            String localName,
 128:                            String qName) throws SAXException {
 129: 
 130:         DefaultHandler current = getCurrentHandler();
 131:         if (current != this) {
 132:             current.endElement(namespaceURI, localName, qName);
 133:         }
 134: 
 135:     }
 136: 
 137: }