1 19 20 package org.netbeans.modules.xml.core.wizard; 21 22 import java.io.IOException ; 23 import java.util.Set ; 24 import java.util.TreeSet ; 25 import org.netbeans.api.xml.services.UserCatalog; 26 import org.openide.util.Lookup; 27 import org.openide.xml.XMLUtil; 28 import org.xml.sax.*; 29 import org.xml.sax.helpers.*; 30 31 37 final class SchemaParser extends DefaultHandler { 38 39 private SchemaInfo info = new SchemaInfo(); 40 41 private int depth = 0; 43 44 45 public SchemaParser() { 46 } 47 48 public SchemaInfo parse(String sid) { 49 if (sid == null) { 50 return null; 51 } else { 52 return parse( new InputSource(sid)); 53 } 54 } 55 56 public SchemaInfo parse(InputSource in) { 57 58 Util.THIS.debug("SchemaParser started."); 60 try { 61 depth = 0; 62 XMLReader parser = XMLUtil.createXMLReader(false, true); 63 parser.setContentHandler(this); 64 parser.setErrorHandler(this); 65 66 UserCatalog catalog = UserCatalog.getDefault(); 67 EntityResolver res = (catalog == null ? null : catalog.getEntityResolver()); 68 69 if (res != null) parser.setEntityResolver(res); 70 71 parser.parse(in); 72 73 return info; 74 75 } catch (SAXException ex) { 76 Util.THIS.debug("Ignoring ex. thrown while looking for Schema roots:", ex); if (ex.getException() instanceof RuntimeException ) { 78 Util.THIS.debug("Nested exception:", ex.getException()); } 80 return info; } catch (IOException ex) { 82 Util.THIS.debug("Ignoring ex. thrown while looking for Schema roots:", ex); return info; } finally { 85 Util.THIS.debug("SchemaParser stopped."); } 87 88 } 89 90 public void startElement (String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 91 depth++; 92 if (depth > 2) return; 93 94 97 if ("element".equals(localName)) { String root = atts.getValue("name"); 99 if (root != null) { 100 Util.THIS.debug("\telement decl: " + root); info.roots.add(root); 102 } 103 } else if ("schema".equals(localName)) { String ns = atts.getValue("targetNamespace"); if (ns != null) { 106 Util.THIS.debug("\ttarget namespace: " + ns); info.namespace = ns; 108 } 109 } 110 } 111 112 public void endElement (String uri, String localName, String qName) { 113 depth--; 114 } 115 116 119 public static final class SchemaInfo { 120 123 public final Set roots = new TreeSet (); 124 125 128 public String namespace; 129 } 130 } 131 | Popular Tags |