1 19 20 package org.netbeans.modules.xml.xsd; 21 22 import javax.xml.parsers.SAXParserFactory ; 23 import org.xml.sax.XMLReader ; 24 import org.xml.sax.EntityResolver ; 25 import org.xml.sax.InputSource ; 26 27 import org.netbeans.api.xml.services.UserCatalog; 28 29 33 public class XSDParser { 34 35 36 public XSDParser() { 37 } 38 39 public static void main(String [] args) throws Exception { 40 if (args.length != 1) { 41 System.err.println("Error: missing file parameter required or too many args"); 42 } 43 44 java.io.FileInputStream fistr = new java.io.FileInputStream (args[0]); 45 46 SAXParserFactory factory = SAXParserFactory.newInstance(); 47 XMLReader reader = factory.newSAXParser().getXMLReader(); 48 XSDContentHandler handler = new XSDContentHandler(System.out); 49 reader.setContentHandler(handler); 50 reader.parse(new InputSource (fistr)); 51 } 52 53 public XSDGrammar parse(InputSource in) { 54 55 XSDContentHandler handler = new XSDContentHandler(System.out); 56 57 try { 58 SAXParserFactory factory = SAXParserFactory.newInstance(); 59 XMLReader reader = factory.newSAXParser().getXMLReader(); 60 61 UserCatalog catalog = UserCatalog.getDefault(); 62 EntityResolver res = (catalog == null ? null : catalog.getEntityResolver()); 63 64 if (res != null) { 65 reader.setEntityResolver(res); 66 } 67 68 reader.setContentHandler(handler); 69 reader.parse(in); 70 return handler.getGrammar(); 71 } catch (org.xml.sax.SAXException ex) { 72 if (Boolean.getBoolean("netbeans.debug.xml") || Boolean.getBoolean("netbeans.debug.exceptions")) { ex.printStackTrace(); 74 if (ex.getException() instanceof RuntimeException ) { 75 ex.getException().printStackTrace(); } 77 } 78 return handler.getGrammar(); } catch (java.io.IOException ex) { 80 if (Boolean.getBoolean("netbeans.debug.xml")) { ex.printStackTrace(); 82 } 83 return handler.getGrammar(); } catch (javax.xml.parsers.ParserConfigurationException e) { 85 if (Boolean.getBoolean("netbeans.debug.xml")) { e.printStackTrace(); 87 } 88 return handler.getGrammar(); } 90 } 91 } 92 | Popular Tags |