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