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