Source for org.jfree.data.xml.ValueHandler

   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:  * ValueHandler.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):   Luke Quinane;
  34:  *
  35:  * $Id: ValueHandler.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:  * 25-Nov-2003 : Patch to handle 'NaN' values (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.data.xml;
  45: 
  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 'Value' element.
  52:  */
  53: public class ValueHandler extends DefaultHandler implements DatasetTags {
  54: 
  55:     /** The root handler. */
  56:     private RootHandler rootHandler;
  57: 
  58:     /** The item handler. */
  59:     private ItemHandler itemHandler;
  60: 
  61:     /** Storage for the current CDATA */
  62:     private StringBuffer currentText;
  63: 
  64:     /**
  65:      * Creates a new value handler.
  66:      *
  67:      * @param rootHandler  the root handler.
  68:      * @param itemHandler  the item handler.
  69:      */
  70:     public ValueHandler(RootHandler rootHandler, ItemHandler itemHandler) {
  71:         this.rootHandler = rootHandler;
  72:         this.itemHandler = itemHandler;
  73:         this.currentText = new StringBuffer();
  74:     }
  75: 
  76:     /**
  77:      * The start of an element.
  78:      *
  79:      * @param namespaceURI  the namespace.
  80:      * @param localName  the element name.
  81:      * @param qName  the element name.
  82:      * @param atts  the attributes.
  83:      *
  84:      * @throws SAXException for errors.
  85:      */
  86:     public void startElement(String namespaceURI,
  87:                              String localName,
  88:                              String qName,
  89:                              Attributes atts) throws SAXException {
  90: 
  91:         if (qName.equals(VALUE_TAG)) {
  92:             // no attributes to read
  93:             clearCurrentText();
  94:         }
  95:         else {
  96:             throw new SAXException("Expecting <Value> but found " + qName);
  97:         }
  98: 
  99:     }
 100: 
 101:     /**
 102:      * The end of an element.
 103:      *
 104:      * @param namespaceURI  the namespace.
 105:      * @param localName  the element name.
 106:      * @param qName  the element name.
 107:      *
 108:      * @throws SAXException for errors.
 109:      */
 110:     public void endElement(String namespaceURI,
 111:                            String localName,
 112:                            String qName) throws SAXException {
 113: 
 114:         if (qName.equals(VALUE_TAG)) {
 115:             Number value;
 116:             try {
 117:                 value = Double.valueOf(this.currentText.toString());
 118:                 if (((Double) value).isNaN()) {
 119:                     value = null;
 120:                 }
 121:             } 
 122:             catch (NumberFormatException e1) {
 123:                 value = null;
 124:             }
 125:             this.itemHandler.setValue(value);
 126:             this.rootHandler.popSubHandler();
 127:         }
 128:         else {
 129:             throw new SAXException("Expecting </Value> but found " + qName);
 130:         }
 131: 
 132:     }
 133: 
 134:     /**
 135:      * Receives some (or all) of the text in the current element.
 136:      *
 137:      * @param ch  character buffer.
 138:      * @param start  the start index.
 139:      * @param length  the length of the valid character data.
 140:      */
 141:     public void characters(char[] ch, int start, int length) {
 142:         if (this.currentText != null) {
 143:             this.currentText.append(String.copyValueOf(ch, start, length));
 144:         }
 145:     }
 146: 
 147:     /**
 148:      * Returns the current text of the textbuffer.
 149:      *
 150:      * @return The current text.
 151:      */
 152:     protected String getCurrentText() {
 153:         return this.currentText.toString();
 154:     }
 155: 
 156:     /**
 157:      * Removes all text from the textbuffer at the end of a CDATA section.
 158:      */
 159:     protected void clearCurrentText() {
 160:         this.currentText.delete(0, this.currentText.length());
 161:     }
 162: 
 163: }