1 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 51 public abstract class AbstractXmlReadHandler implements XmlReadHandler { 52 53 private RootXmlReadHandler rootHandler; 54 55 56 private String tagName; 57 58 59 private boolean firstCall = true; 60 61 64 public AbstractXmlReadHandler() { 65 } 66 67 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 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 122 public void characters(final char[] ch, final int start, final int length) throws SAXException { 123 } 125 126 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 152 protected void startParsing(final Attributes attrs) throws SAXException , XmlReaderException { 153 } 155 156 162 protected void doneParsing() throws SAXException , XmlReaderException { 163 } 165 166 177 protected XmlReadHandler getHandlerForChild(final String tagName, final Attributes atts) 178 throws XmlReaderException, SAXException { 179 return null; 180 } 181 182 187 public String getTagName() { 188 return this.tagName; 189 } 190 191 196 public RootXmlReadHandler getRootHandler() { 197 return this.rootHandler; 198 } 199 200 } 201 202
| Popular Tags
|