001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/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: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------------------
028: * AbstractXmlReadHandler.java
029: * ---------------------------
030: * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: AbstractXmlReadHandler.java,v 1.4 2005/10/18 13:32:52 mungady Exp $
036: *
037: * Changes (from 25-Nov-2003)
038: * --------------------------
039: * 25-Nov-2003 : Added Javadocs (DG);
040: *
041: */
042: package org.jfree.xml.parser;
043:
044: import org.xml.sax.Attributes;
045: import org.xml.sax.SAXException;
046: import org.jfree.util.Log;
047:
048: /**
049: * A base class for implementing an {@link XmlReadHandler}.
050: */
051: public abstract class AbstractXmlReadHandler implements XmlReadHandler {
052: /** The root handler. */
053: private RootXmlReadHandler rootHandler;
054:
055: /** The tag name. */
056: private String tagName;
057:
058: /** A flag indicating the first call. */
059: private boolean firstCall = true;
060:
061: /**
062: * Creates a new handler.
063: */
064: public AbstractXmlReadHandler() {
065: }
066:
067: /**
068: * Initialises the handler.
069: *
070: * @param rootHandler the root handler.
071: * @param tagName the tag name.
072: */
073: public void init(final RootXmlReadHandler rootHandler,
074: final String tagName) {
075: if (rootHandler == null) {
076: throw new NullPointerException(
077: "Root handler must not be null");
078: }
079: if (tagName == null) {
080: throw new NullPointerException("Tag name must not be null");
081: }
082: this .rootHandler = rootHandler;
083: this .tagName = tagName;
084: }
085:
086: /**
087: * This method is called at the start of an element.
088: *
089: * @param tagName the tag name.
090: * @param attrs the attributes.
091: *
092: * @throws SAXException if there is a parsing error.
093: * @throws XmlReaderException if there is a reader error.
094: */
095: public final void startElement(final String tagName,
096: final Attributes attrs) throws XmlReaderException,
097: SAXException {
098: if (this .firstCall) {
099: if (!this .tagName.equals(tagName)) {
100: throw new SAXException("Expected <" + this .tagName
101: + ">, found <" + tagName + ">");
102: }
103: this .firstCall = false;
104: startParsing(attrs);
105: } else {
106: final XmlReadHandler childHandler = getHandlerForChild(
107: tagName, attrs);
108: if (childHandler == null) {
109: Log.warn("Unknown tag <" + tagName + ">");
110: return;
111: }
112: childHandler.init(getRootHandler(), tagName);
113: this .rootHandler.recurse(childHandler, tagName, attrs);
114: }
115: }
116:
117: /**
118: * This method is called to process the character data between element tags.
119: *
120: * @param ch the character buffer.
121: * @param start the start index.
122: * @param length the length.
123: *
124: * @throws SAXException if there is a parsing error.
125: */
126: public void characters(final char[] ch, final int start,
127: final int length) throws SAXException {
128: // nothing required
129: }
130:
131: /**
132: * This method is called at the end of an element.
133: *
134: * @param tagName the tag name.
135: *
136: * @throws SAXException if there is a parsing error.
137: */
138: public final void endElement(final String tagName)
139: throws SAXException {
140: if (this .tagName.equals(tagName)) {
141: try {
142: doneParsing();
143: this .rootHandler.unwind(tagName);
144: } catch (XmlReaderException xre) {
145: throw new SAXException(xre);
146: }
147: }
148: }
149:
150: /**
151: * Starts parsing.
152: *
153: * @param attrs the attributes.
154: *
155: * @throws SAXException if there is a parsing error.
156: */
157: protected void startParsing(final Attributes attrs)
158: throws SAXException, XmlReaderException {
159: // nothing required
160: }
161:
162: /**
163: * Done parsing.
164: *
165: * @throws SAXException if there is a parsing error.
166: * @throws XmlReaderException if there is a reader error.
167: */
168: protected void doneParsing() throws SAXException,
169: XmlReaderException {
170: // nothing required
171: }
172:
173: /**
174: * Returns the handler for a child element.
175: *
176: * @param tagName the tag name.
177: * @param atts the attributes.
178: *
179: * @return the handler or null, if the tagname is invalid.
180: *
181: * @throws SAXException if there is a parsing error.
182: * @throws XmlReaderException if there is a reader error.
183: */
184: protected XmlReadHandler getHandlerForChild(final String tagName,
185: final Attributes atts) throws XmlReaderException,
186: SAXException {
187: return null;
188: }
189:
190: /**
191: * Returns the tag name.
192: *
193: * @return the tag name.
194: */
195: public String getTagName() {
196: return this .tagName;
197: }
198:
199: /**
200: * Returns the root handler for the parsing.
201: *
202: * @return the root handler.
203: */
204: public RootXmlReadHandler getRootHandler() {
205: return this.rootHandler;
206: }
207:
208: }
|