Source for org.jfree.data.xml.CategorySeriesHandler

   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:  * CategorySeriesHandler.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 java.util.Iterator;
  44: 
  45: import org.jfree.data.DefaultKeyedValues;
  46: import org.xml.sax.Attributes;
  47: import org.xml.sax.SAXException;
  48: import org.xml.sax.helpers.DefaultHandler;
  49: 
  50: /**
  51:  * A handler for reading a series for a category dataset.
  52:  */
  53: public class CategorySeriesHandler extends DefaultHandler 
  54:                                    implements DatasetTags {
  55: 
  56:     /** The root handler. */
  57:     private RootHandler root;
  58: 
  59:     /** The series key. */
  60:     private Comparable seriesKey;
  61: 
  62:     /** The values. */
  63:     private DefaultKeyedValues values;
  64: 
  65:     /**
  66:      * Creates a new item handler.
  67:      *
  68:      * @param root  the root handler.
  69:      */
  70:     public CategorySeriesHandler(RootHandler root) {
  71:         this.root = root;
  72:         this.values = new DefaultKeyedValues();
  73:     }
  74: 
  75:     /**
  76:      * Sets the series key.
  77:      *
  78:      * @param key  the key.
  79:      */
  80:     public void setSeriesKey(Comparable key) {
  81:         this.seriesKey = key;
  82:     }
  83: 
  84:     /**
  85:      * Adds an item to the temporary storage for the series.
  86:      *
  87:      * @param key  the key.
  88:      * @param value  the value.
  89:      */
  90:     public void addItem(Comparable key, final Number value) {
  91:         this.values.addValue(key, value);
  92:     }
  93: 
  94:     /**
  95:      * The start of an element.
  96:      *
  97:      * @param namespaceURI  the namespace.
  98:      * @param localName  the element name.
  99:      * @param qName  the element name.
 100:      * @param atts  the attributes.
 101:      *
 102:      * @throws SAXException for errors.
 103:      */
 104:     public void startElement(String namespaceURI,
 105:                              String localName,
 106:                              String qName,
 107:                              Attributes atts) throws SAXException {
 108: 
 109:         if (qName.equals(SERIES_TAG)) {
 110:             setSeriesKey(atts.getValue("name"));
 111:             ItemHandler subhandler = new ItemHandler(this.root, this);
 112:             this.root.pushSubHandler(subhandler);
 113:         }
 114:         else if (qName.equals(ITEM_TAG)) {
 115:             ItemHandler subhandler = new ItemHandler(this.root, this);
 116:             this.root.pushSubHandler(subhandler);
 117:             subhandler.startElement(namespaceURI, localName, qName, atts);
 118:         }
 119: 
 120:         else {
 121:             throw new SAXException(
 122:                 "Expecting <Series> or <Item> tag...found " + qName
 123:             );
 124:         }
 125:     }
 126: 
 127:     /**
 128:      * The end of an element.
 129:      *
 130:      * @param namespaceURI  the namespace.
 131:      * @param localName  the element name.
 132:      * @param qName  the element name.
 133:      */
 134:     public void endElement(String namespaceURI,
 135:                            String localName,
 136:                            String qName) {
 137: 
 138:         if (this.root instanceof CategoryDatasetHandler) {
 139:             CategoryDatasetHandler handler = (CategoryDatasetHandler) this.root;
 140: 
 141:             Iterator iterator = this.values.getKeys().iterator();
 142:             while (iterator.hasNext()) {
 143:                 Comparable key = (Comparable) iterator.next();
 144:                 Number value = this.values.getValue(key);
 145:                 handler.addItem(this.seriesKey, key, value);
 146:             }
 147: 
 148:             this.root.popSubHandler();
 149:         }
 150: 
 151:     }
 152: 
 153: }