Frames | No Frames |
1: /* ======================================================================== 2: * JCommon : a free general purpose class 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/jcommon/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: * AbstractXmlReadHandler.java 29: * --------------------------- 30: * (C)opyright 2003, 2004, by Thomas Morgner and Contributors. 31: * 32: * Original Author: Thomas Morgner; 33: * Contributor(s): David Gilbert (for Object Refinery Limited); 34: * 35: * $Id: AbstractXmlReadHandler.java,v 1.5 2008/09/10 09:20:16 mungady Exp $ 36: * 37: * Changes (from 25-Nov-2003) 38: * -------------------------- 39: * 25-Nov-2003 : Added Javadocs (DG); 40: * 41: */ 42: package org.jfree.xml.parser; 43: 44: import org.xml.sax.Attributes; 45: import org.xml.sax.SAXException; 46: import org.jfree.util.Log; 47: 48: /** 49: * A base class for implementing an {@link XmlReadHandler}. 50: */ 51: public abstract class AbstractXmlReadHandler implements XmlReadHandler { 52: /** The root handler. */ 53: private RootXmlReadHandler rootHandler; 54: 55: /** The tag name. */ 56: private String tagName; 57: 58: /** A flag indicating the first call. */ 59: private boolean firstCall = true; 60: 61: /** 62: * Creates a new handler. 63: */ 64: public AbstractXmlReadHandler() { 65: } 66: 67: /** 68: * Initialises the handler. 69: * 70: * @param rootHandler the root handler. 71: * @param tagName the tag name. 72: */ 73: public void init(final RootXmlReadHandler rootHandler, final String tagName) { 74: if (rootHandler == null) { 75: throw new NullPointerException("Root handler must not be null"); 76: } 77: if (tagName == null) { 78: throw new NullPointerException("Tag name must not be null"); 79: } 80: this.rootHandler = rootHandler; 81: this.tagName = tagName; 82: } 83: 84: /** 85: * This method is called at the start of an element. 86: * 87: * @param tagName the tag name. 88: * @param attrs the attributes. 89: * 90: * @throws SAXException if there is a parsing error. 91: * @throws XmlReaderException if there is a reader error. 92: */ 93: public final void startElement(final String tagName, final Attributes attrs) 94: throws XmlReaderException, SAXException { 95: if (this.firstCall) { 96: if (!this.tagName.equals(tagName)) { 97: throw new SAXException("Expected <" + this.tagName + ">, found <" + tagName + ">"); 98: } 99: this.firstCall = false; 100: startParsing(attrs); 101: } 102: else { 103: final XmlReadHandler childHandler = getHandlerForChild(tagName, attrs); 104: if (childHandler == null) { 105: Log.warn ("Unknown tag <" + tagName + ">"); 106: return; 107: } 108: childHandler.init(getRootHandler(), tagName); 109: this.rootHandler.recurse(childHandler, tagName, attrs); 110: } 111: } 112: 113: /** 114: * This method is called to process the character data between element tags. 115: * 116: * @param ch the character buffer. 117: * @param start the start index. 118: * @param length the length. 119: * 120: * @throws SAXException if there is a parsing error. 121: */ 122: public void characters(final char[] ch, final int start, final int length) throws SAXException { 123: // nothing required 124: } 125: 126: /** 127: * This method is called at the end of an element. 128: * 129: * @param tagName the tag name. 130: * 131: * @throws SAXException if there is a parsing error. 132: */ 133: public final void endElement(final String tagName) throws SAXException { 134: if (this.tagName.equals(tagName)) { 135: try { 136: doneParsing(); 137: this.rootHandler.unwind(tagName); 138: } 139: catch (XmlReaderException xre) { 140: throw new SAXException(xre); 141: } 142: } 143: } 144: 145: /** 146: * Starts parsing. 147: * 148: * @param attrs the attributes. 149: * 150: * @throws SAXException if there is a parsing error. 151: * @throws XmlReaderException ? 152: */ 153: protected void startParsing(final Attributes attrs) 154: throws SAXException, XmlReaderException { 155: // nothing required 156: } 157: 158: /** 159: * Done parsing. 160: * 161: * @throws SAXException if there is a parsing error. 162: * @throws XmlReaderException if there is a reader error. 163: */ 164: protected void doneParsing() throws SAXException, XmlReaderException { 165: // nothing required 166: } 167: 168: /** 169: * Returns the handler for a child element. 170: * 171: * @param tagName the tag name. 172: * @param atts the attributes. 173: * 174: * @return the handler or null, if the tagname is invalid. 175: * 176: * @throws SAXException if there is a parsing error. 177: * @throws XmlReaderException if there is a reader error. 178: */ 179: protected XmlReadHandler getHandlerForChild(final String tagName, final Attributes atts) 180: throws XmlReaderException, SAXException { 181: return null; 182: } 183: 184: /** 185: * Returns the tag name. 186: * 187: * @return the tag name. 188: */ 189: public String getTagName() { 190: return this.tagName; 191: } 192: 193: /** 194: * Returns the root handler for the parsing. 195: * 196: * @return the root handler. 197: */ 198: public RootXmlReadHandler getRootHandler() { 199: return this.rootHandler; 200: } 201: 202: }