1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
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.
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.
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,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* ---------------------------
28
* AbstractXmlReadHandler.java
29
* ---------------------------
30
* (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
32
* Original Author: Thomas Morgner;
33
* Contributor(s): David Gilbert (for Object Refinery Limited);
35
* $Id: AbstractXmlReadHandler.java,v 1.4 2005/10/18 13:32:52 mungady Exp $
37
* Changes (from 25-Nov-2003)
38
* --------------------------
39
* 25-Nov-2003 : Added Javadocs (DG);
42
package org.jfree.xml.parser;
44
import org.xml.sax.Attributes;
45
import org.xml.sax.SAXException;
46
import org.jfree.util.Log;
49
* A base class for implementing an {@link XmlReadHandler}.
51
public abstract class AbstractXmlReadHandler implements XmlReadHandler {
52
/** The root handler. */
53
private RootXmlReadHandler rootHandler;
56
private String tagName;
58
/** A flag indicating the first call. */
59
private boolean firstCall = true;
62
* Creates a new handler.
64
public AbstractXmlReadHandler() {
68
* Initialises the handler.
70
* @param rootHandler the root handler.
71
* @param tagName the tag name.
73
public void init(final RootXmlReadHandler rootHandler, final String tagName) {
74
if (rootHandler == null) {
75
throw new NullPointerException("Root handler must not be null");
77
if (tagName == null) {
78
throw new NullPointerException("Tag name must not be null");
80
this.rootHandler = rootHandler;
81
this.tagName = tagName;
85
* This method is called at the start of an element.
87
* @param tagName the tag name.
88
* @param attrs the attributes.
90
* @throws SAXException if there is a parsing error.
91
* @throws XmlReaderException if there is a reader error.
93
public final void startElement(final String tagName, final Attributes attrs)
94
throws XmlReaderException, SAXException {
96
if (!this.tagName.equals(tagName)) {
97
throw new SAXException("Expected <" + this.tagName + ">, found <" + tagName + ">");
99
this.firstCall = false;
103
final XmlReadHandler childHandler = getHandlerForChild(tagName, attrs);
104
if (childHandler == null) {
105
Log.warn ("Unknown tag <" + tagName + ">");
108
childHandler.init(getRootHandler(), tagName);
109
this.rootHandler.recurse(childHandler, tagName, attrs);
114
* This method is called to process the character data between element tags.
116
* @param ch the character buffer.
117
* @param start the start index.
118
* @param length the length.
120
* @throws SAXException if there is a parsing error.
122
public void characters(final char[] ch, final int start, final int length) throws SAXException {
127
* This method is called at the end of an element.
129
* @param tagName the tag name.
131
* @throws SAXException if there is a parsing error.
133
public final void endElement(final String tagName) throws SAXException {
134
if (this.tagName.equals(tagName)) {
137
this.rootHandler.unwind(tagName);
139
catch (XmlReaderException xre) {
140
throw new SAXException(xre);
148
* @param attrs the attributes.
150
* @throws SAXException if there is a parsing error.
152
protected void startParsing(final Attributes attrs) throws SAXException, XmlReaderException {
159
* @throws SAXException if there is a parsing error.
160
* @throws XmlReaderException if there is a reader error.
162
protected void doneParsing() throws SAXException, XmlReaderException {
167
* Returns the handler for a child element.
169
* @param tagName the tag name.
170
* @param atts the attributes.
172
* @return the handler or null, if the tagname is invalid.
174
* @throws SAXException if there is a parsing error.
175
* @throws XmlReaderException if there is a reader error.
177
protected XmlReadHandler getHandlerForChild(final String tagName, final Attributes atts)
178
throws XmlReaderException, SAXException {
183
* Returns the tag name.
185
* @return the tag name.
187
public String getTagName() {
192
* Returns the root handler for the parsing.
194
* @return the root handler.
196
public RootXmlReadHandler getRootHandler() {
197
return this.rootHandler;