001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------------
028 * KeyHandler.java
029 * ---------------
030 * (C) Copyright 2003-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.xml;
038
039import org.xml.sax.Attributes;
040import org.xml.sax.SAXException;
041import org.xml.sax.helpers.DefaultHandler;
042
043/**
044 * A SAX handler for reading a key.
045 */
046public class KeyHandler extends DefaultHandler implements DatasetTags {
047
048    /** The root handler. */
049    private RootHandler rootHandler;
050
051    /** The item handler. */
052    private ItemHandler itemHandler;
053
054    /** Storage for the current CDATA */
055    private StringBuffer currentText;
056
057    /** The key. */
058    //private Comparable key;
059
060    /**
061     * Creates a new handler.
062     *
063     * @param rootHandler  the root handler.
064     * @param itemHandler  the item handler.
065     */
066    public KeyHandler(RootHandler rootHandler, ItemHandler itemHandler) {
067        this.rootHandler = rootHandler;
068        this.itemHandler = itemHandler;
069        this.currentText = new StringBuffer();
070        //this.key = null;
071    }
072
073    /**
074     * The start of an element.
075     *
076     * @param namespaceURI  the namespace.
077     * @param localName  the element name.
078     * @param qName  the element name.
079     * @param atts  the attributes.
080     *
081     * @throws SAXException for errors.
082     */
083    @Override
084    public void startElement(String namespaceURI,
085                             String localName,
086                             String qName,
087                             Attributes atts) throws SAXException {
088
089        if (qName.equals(KEY_TAG)) {
090            clearCurrentText();
091        }
092        else {
093            throw new SAXException("Expecting <Key> but found " + qName);
094        }
095
096    }
097
098    /**
099     * The end of an element.
100     *
101     * @param namespaceURI  the namespace.
102     * @param localName  the element name.
103     * @param qName  the element name.
104     *
105     * @throws SAXException for errors.
106     */
107    @Override
108    public void endElement(String namespaceURI,
109                           String localName,
110                           String qName) throws SAXException {
111
112        if (qName.equals(KEY_TAG)) {
113            this.itemHandler.setKey(getCurrentText());
114            this.rootHandler.popSubHandler();
115            this.rootHandler.pushSubHandler(
116                new ValueHandler(this.rootHandler, this.itemHandler)
117            );
118        }
119        else {
120            throw new SAXException("Expecting </Key> but found " + qName);
121        }
122
123    }
124
125    /**
126     * Receives some (or all) of the text in the current element.
127     *
128     * @param ch  character buffer.
129     * @param start  the start index.
130     * @param length  the length of the valid character data.
131     */
132    @Override
133    public void characters(char[] ch, int start, int length) {
134        if (this.currentText != null) {
135            this.currentText.append(String.copyValueOf(ch, start, length));
136        }
137    }
138
139    /**
140     * Returns the current text of the textbuffer.
141     *
142     * @return The current text.
143     */
144    protected String getCurrentText() {
145        return this.currentText.toString();
146    }
147
148    /**
149     * Removes all text from the textbuffer at the end of a CDATA section.
150     */
151    protected void clearCurrentText() {
152        this.currentText.delete(0, this.currentText.length());
153    }
154
155}