1 44 45 package org.jfree.xml; 46 47 import java.util.HashMap ; 48 import java.util.Stack ; 49 50 import org.xml.sax.Attributes ; 51 import org.xml.sax.SAXException ; 52 53 62 public abstract class Parser extends FrontendDefaultHandler { 63 64 65 public static final String CONTENTBASE_KEY = "content-base"; 66 67 68 private Stack activeFactories; 69 70 71 private ElementDefinitionHandler initialFactory; 72 73 74 private HashMap parserHelperObjects; 75 76 79 public Parser() { 80 this.activeFactories = new Stack (); 81 this.parserHelperObjects = new HashMap (); 82 } 83 84 88 public String [] getComments() { 89 return getCommentHandler().getComments(); 90 } 91 92 97 public void pushFactory(final ElementDefinitionHandler factory) { 98 this.activeFactories.push(factory); 99 } 100 101 106 public ElementDefinitionHandler peekFactory() { 107 return (ElementDefinitionHandler) this.activeFactories.peek(); 108 } 109 110 115 public ElementDefinitionHandler popFactory() { 116 this.activeFactories.pop(); 117 return peekFactory(); 118 } 119 120 132 public void endDocument() throws SAXException { 133 } 135 136 147 public void startDocument() throws SAXException { 148 this.activeFactories.clear(); 149 pushFactory(getInitialFactory()); 150 } 151 152 167 public void characters(final char[] ch, final int start, final int length) 168 throws SAXException { 169 try { 170 peekFactory().characters(ch, start, length); 171 } 172 catch (ParseException pe) { 173 throw pe; 174 } 175 catch (Exception e) { 176 throw new ParseException(e, getLocator()); 177 } 178 } 179 180 196 public void endElement(final String uri, final String localName, final String qName) 197 throws SAXException { 198 try { 199 peekFactory().endElement(qName); 200 } 201 catch (ParseException pe) { 202 throw pe; 203 } 204 catch (Exception e) { 205 throw new ParseException(e, getLocator()); 206 } 207 finally { 208 getCommentHandler().clearComments(); 209 } 210 } 211 212 213 230 public void startElement(final String uri, final String localName, 231 final String qName, final Attributes attributes) 232 throws SAXException { 233 try { 234 peekFactory().startElement(qName, attributes); 235 } 236 catch (ParseException pe) { 237 throw pe; 238 } 239 catch (Exception e) { 240 throw new ParseException(e, getLocator()); 241 } 242 finally { 243 getCommentHandler().clearComments(); 244 } 245 } 246 247 252 public void setInitialFactory(final ElementDefinitionHandler factory) { 253 this.initialFactory = factory; 254 } 255 256 261 public ElementDefinitionHandler getInitialFactory() { 262 return this.initialFactory; 263 } 264 265 271 public void setHelperObject(final String key, final Object value) { 272 if (value == null) { 273 this.parserHelperObjects.remove(key); 274 } 275 else { 276 this.parserHelperObjects.put(key, value); 277 } 278 } 279 280 287 public Object getHelperObject(final String key) { 288 return this.parserHelperObjects.get(key); 289 } 290 291 296 public abstract Parser getInstance(); 297 298 299 public final FrontendDefaultHandler newInstance() { 300 return getInstance(); 301 } 302 303 310 public abstract Object getResult(); 311 } 312
| Popular Tags
|