Source for org.jfree.data.xml.KeyHandler

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