Source for org.jfree.data.xml.ItemHandler

   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:  * ItemHandler.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: ItemHandler.java,v 1.3.2.1 2005/10/25 21:36:10 mungady Exp $
  36:  *
  37:  * Changes (from 21-Jun-2001)
  38:  * --------------------------
  39:  * 23-Jan-2003 : Version 1 (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.data.xml;
  44: 
  45: import org.xml.sax.Attributes;
  46: import org.xml.sax.SAXException;
  47: import org.xml.sax.helpers.DefaultHandler;
  48: 
  49: /**
  50:  * A handler for reading key-value items.
  51:  */
  52: public class ItemHandler extends DefaultHandler implements DatasetTags {
  53: 
  54:     /** The root handler. */
  55:     private RootHandler root;
  56: 
  57:     /** The parent handler (can be the same as root, but not always). */
  58:     private DefaultHandler parent;
  59: 
  60:     /** The key. */
  61:     private Comparable key;
  62: 
  63:     /** The value. */
  64:     private Number value;
  65: 
  66:     /**
  67:      * Creates a new item handler.
  68:      *
  69:      * @param root  the root handler.
  70:      * @param parent  the parent handler.
  71:      */
  72:     public ItemHandler(RootHandler root, DefaultHandler parent) {
  73:         this.root = root;
  74:         this.parent = parent;
  75:         this.key = null;
  76:         this.value = null;
  77:     }
  78: 
  79:     /**
  80:      * Returns the key that has been read by the handler, or <code>null</code>.
  81:      *
  82:      * @return The key.
  83:      */
  84:     public Comparable getKey() {
  85:         return this.key;
  86:     }
  87: 
  88:     /**
  89:      * Sets the key.
  90:      *
  91:      * @param key  the key.
  92:      */
  93:     public void setKey(Comparable key) {
  94:         this.key = key;
  95:     }
  96: 
  97:     /**
  98:      * Returns the key that has been read by the handler, or <code>null</code>.
  99:      *
 100:      * @return The value.
 101:      */
 102:     public Number getValue() {
 103:         return this.value;
 104:     }
 105: 
 106:     /**
 107:      * Sets the value.
 108:      *
 109:      * @param value  the value.
 110:      */
 111:     public void setValue(Number value) {
 112:         this.value = value;
 113:     }
 114: 
 115:     /**
 116:      * The start of an element.
 117:      *
 118:      * @param namespaceURI  the namespace.
 119:      * @param localName  the element name.
 120:      * @param qName  the element name.
 121:      * @param atts  the attributes.
 122:      *
 123:      * @throws SAXException for errors.
 124:      */
 125:     public void startElement(String namespaceURI,
 126:                              String localName,
 127:                              String qName,
 128:                              Attributes atts) throws SAXException {
 129: 
 130:         if (qName.equals(ITEM_TAG)) {
 131:             KeyHandler subhandler = new KeyHandler(this.root, this);
 132:             this.root.pushSubHandler(subhandler);
 133:         }
 134:         else if (qName.equals(VALUE_TAG)) {
 135:             ValueHandler subhandler = new ValueHandler(this.root, this);
 136:             this.root.pushSubHandler(subhandler);
 137:         }
 138:         else {
 139:             throw new SAXException(
 140:                 "Expected <Item> or <Value>...found " + qName
 141:             );
 142:         }
 143: 
 144:     }
 145: 
 146:     /**
 147:      * The end of an element.
 148:      *
 149:      * @param namespaceURI  the namespace.
 150:      * @param localName  the element name.
 151:      * @param qName  the element name.
 152:      */
 153:     public void endElement(String namespaceURI,
 154:                            String localName,
 155:                            String qName) {
 156: 
 157:         if (this.parent instanceof PieDatasetHandler) {
 158:             PieDatasetHandler handler = (PieDatasetHandler) this.parent;
 159:             handler.addItem(this.key, this.value);
 160:             this.root.popSubHandler();
 161:         }
 162:         else if (this.parent instanceof CategorySeriesHandler) {
 163:             CategorySeriesHandler handler = (CategorySeriesHandler) this.parent;
 164:             handler.addItem(this.key, this.value);
 165:             this.root.popSubHandler();
 166:         }
 167: 
 168:     }
 169: 
 170: }