1 19 package org.netbeans.modules.xml.tools.generator; 20 21 import org.xml.sax.*; 22 23 33 public class SAXBindingsParser extends org.xml.sax.helpers.DefaultHandler { 34 35 private java.lang.StringBuffer buffer; 36 37 private SAXBindingsHandler handler; 38 39 private java.util.Stack context; 40 41 public SAXBindingsParser(final SAXBindingsHandler handler) { 42 this.handler = handler; 43 buffer = new StringBuffer (111); 44 context = new java.util.Stack (); 45 } 46 47 public void setDocumentLocator(Locator locator) { 48 } 49 50 51 public void startElement(String ns, String ln, String name, Attributes attrs) throws SAXException { 52 dispatch(true); 53 context.push(new Object [] {name, new org.xml.sax.helpers.AttributesImpl (attrs)}); 54 if ("parslet".equals(name)) { handler.handle_parslet(attrs); 56 } else if ("SAX-bindings".equals(name)) { handler.start_SAX_bindings(attrs); 58 } else if ("attbind".equals(name)) { handler.handle_attbind(attrs); 60 } else if ("bind".equals(name)) { handler.start_bind(attrs); 62 } 63 } 64 65 public void endElement(String ns, String ln, java.lang.String name) throws SAXException { 66 dispatch(false); 67 context.pop(); 68 if ("SAX-bindings".equals(name)) { handler.end_SAX_bindings(); 70 } else if ("bind".equals(name)) { handler.end_bind(); 72 } 73 } 74 75 public void characters(char[] chars, int start, int len) throws SAXException { 76 buffer.append(chars, start, len); 77 } 78 79 public void ignorableWhitespace(char[] chars, int start, int len) throws SAXException { 80 } 81 82 public void processingInstruction(java.lang.String target, java.lang.String data) throws SAXException { 83 } 84 85 private void dispatch(final boolean fireOnlyIfMixed) throws SAXException { 86 if (fireOnlyIfMixed && buffer.length() == 0) return; 88 Object [] ctx = (Object []) context.peek(); 89 String here = (String ) ctx[0]; 90 Attributes attrs = (Attributes) ctx[1]; 91 buffer.delete(0, buffer.length()); 92 } 93 94 102 public void parse(final InputSource input) throws SAXException, javax.xml.parsers.ParserConfigurationException , java.io.IOException { 103 parse(input, this); 104 } 105 106 114 public void parse(final java.net.URL url) throws SAXException, javax.xml.parsers.ParserConfigurationException , java.io.IOException { 115 parse(new InputSource(url.toExternalForm()), this); 116 } 117 118 126 public static void parse(final InputSource input, final SAXBindingsHandler handler) throws SAXException, javax.xml.parsers.ParserConfigurationException , java.io.IOException { 127 parse(input, new SAXBindingsParser(handler)); 128 } 129 130 138 public static void parse(final java.net.URL url, final SAXBindingsHandler handler) throws SAXException, javax.xml.parsers.ParserConfigurationException , java.io.IOException { 139 parse(new InputSource(url.toExternalForm()), handler); 140 } 141 142 private static void parse(final InputSource input, final SAXBindingsParser recognizer) throws SAXException, javax.xml.parsers.ParserConfigurationException , java.io.IOException { 143 javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance(); 144 factory.setValidating(true); factory.setNamespaceAware(false); XMLReader parser = factory.newSAXParser().getXMLReader(); 147 parser.setContentHandler(recognizer); 148 parser.setErrorHandler(recognizer.getDefaultErrorHandler()); 149 parser.parse(input); 150 } 151 152 private ErrorHandler getDefaultErrorHandler() { 153 return new ErrorHandler() { 154 public void error(SAXParseException ex) throws SAXException { 155 if (context.isEmpty()) { 156 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Missing DOCTYPE."); } 158 throw ex; 159 } 160 161 public void fatalError(SAXParseException ex) throws SAXException { 162 throw ex; 163 } 164 165 public void warning(SAXParseException ex) throws SAXException { 166 } 168 }; 169 170 } 171 172 } 173 | Popular Tags |