1 19 20 package org.netbeans.modules.xml.core.wizard; 21 22 import java.io.*; 23 import java.util.*; 24 25 import org.xml.sax.*; 26 import org.xml.sax.ext.*; 27 import org.xml.sax.helpers.*; 28 29 import org.openide.xml.*; 30 import org.openide.util.Lookup; 31 32 import org.netbeans.api.xml.services.UserCatalog; 33 import org.netbeans.api.xml.parsers.SAXEntityParser; 34 35 40 final class DTDParser extends DefaultHandler implements DeclHandler { 41 42 static final String SAX_PROPERTY = "http://xml.org/sax/properties/"; static final String DECL_HANDLER = "declaration-handler"; 45 private final Set roots = new TreeSet(); 46 47 48 public DTDParser() { 49 } 50 51 54 public Set parse(InputSource in) { 55 56 Util.THIS.debug("DTDParser started."); 57 58 try { 59 XMLReader parser = XMLUtil.createXMLReader(true); 61 parser.setContentHandler(this); 62 parser.setErrorHandler(this); 63 parser.setProperty(SAX_PROPERTY + DECL_HANDLER, this); 64 65 67 UserCatalog catalog = UserCatalog.getDefault(); 68 EntityResolver res = (catalog == null ? null : catalog.getEntityResolver()); 69 70 if (res != null) parser.setEntityResolver(res); 71 72 SAXEntityParser dtdParser = new SAXEntityParser(parser, false); 73 dtdParser.parse(in); 74 75 throw new IllegalStateException ("How we can get here?"); } catch (Stop stop) { 77 return roots; } catch (SAXException ex) { 79 Util.THIS.debug("Ignoring SAX ex. while parsing DTD:", ex); if (ex.getException() instanceof RuntimeException ) { 81 Util.THIS.debug("Nested exception:", ex.getException()); } 83 return roots; } catch (IOException ex) { 85 Util.THIS.debug("Ignoring I/O ex. while parsing DTD:", ex); return roots; } finally { 88 Util.THIS.debug("DTDParser stopped."); } 90 } 91 92 public void elementDecl(String name, String model) throws SAXException { 93 Util.THIS.debug("\telementDecl(" + name + ",...)"); roots.add(name); 95 } 96 97 public void externalEntityDecl(String name, String publicId, String systemId) throws SAXException { 98 } 99 100 public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) throws SAXException { 101 } 102 103 public void internalEntityDecl(String name, String value) throws SAXException { 104 } 105 106 public void notationDecl (String name, String publicId, String systemId) throws SAXException { 107 } 108 109 public void startElement (String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 110 Util.THIS.debug("\tstopping parser!"); throw new Stop(); 112 } 113 114 private class Stop extends SAXException { 115 116 private static final long serialVersionUID = -64662796017444980L; 117 118 Stop() { 119 super("STOP"); } 121 122 public Throwable fillInStackTrace() { 123 return this; 124 } 125 } 126 } 127 | Popular Tags |