001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, by Object Refinery Limited 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-2008, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 23-Jan-2003 : Version 1 (DG);
038 *
039 */
040
041package org.jfree.data.xml;
042
043import org.xml.sax.Attributes;
044import org.xml.sax.SAXException;
045import org.xml.sax.helpers.DefaultHandler;
046
047/**
048 * A SAX handler for reading a key.
049 */
050public class KeyHandler extends DefaultHandler implements DatasetTags {
051
052    /** The root handler. */
053    private RootHandler rootHandler;
054
055    /** The item handler. */
056    private ItemHandler itemHandler;
057
058    /** Storage for the current CDATA */
059    private StringBuffer currentText;
060
061    /** The key. */
062    //private Comparable key;
063
064    /**
065     * Creates a new handler.
066     *
067     * @param rootHandler  the root handler.
068     * @param itemHandler  the item handler.
069     */
070    public KeyHandler(RootHandler rootHandler, ItemHandler itemHandler) {
071        this.rootHandler = rootHandler;
072        this.itemHandler = itemHandler;
073        this.currentText = new StringBuffer();
074        //this.key = null;
075    }
076
077    /**
078     * The start of an element.
079     *
080     * @param namespaceURI  the namespace.
081     * @param localName  the element name.
082     * @param qName  the element name.
083     * @param atts  the attributes.
084     *
085     * @throws SAXException for errors.
086     */
087    @Override
088    public void startElement(String namespaceURI,
089                             String localName,
090                             String qName,
091                             Attributes atts) throws SAXException {
092
093        if (qName.equals(KEY_TAG)) {
094            clearCurrentText();
095        }
096        else {
097            throw new SAXException("Expecting <Key> but found " + qName);
098        }
099
100    }
101
102    /**
103     * The end of an element.
104     *
105     * @param namespaceURI  the namespace.
106     * @param localName  the element name.
107     * @param qName  the element name.
108     *
109     * @throws SAXException for errors.
110     */
111    @Override
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    @Override
137    public void characters(char[] ch, int start, int length) {
138        if (this.currentText != null) {
139            this.currentText.append(String.copyValueOf(ch, start, length));
140        }
141    }
142
143    /**
144     * Returns the current text of the textbuffer.
145     *
146     * @return The current text.
147     */
148    protected String getCurrentText() {
149        return this.currentText.toString();
150    }
151
152    /**
153     * Removes all text from the textbuffer at the end of a CDATA section.
154     */
155    protected void clearCurrentText() {
156        this.currentText.delete(0, this.currentText.length());
157    }
158
159}