1 38 39 40 package com.sun.xml.fastinfoset.tools; 41 42 import com.sun.xml.fastinfoset.Decoder; 43 import com.sun.xml.fastinfoset.sax.SAXDocumentParser; 44 import com.sun.xml.fastinfoset.sax.Properties; 45 import java.io.BufferedInputStream ; 46 import java.io.InputStream ; 47 import java.io.OutputStream ; 48 import javax.xml.parsers.SAXParser ; 49 import javax.xml.parsers.SAXParserFactory ; 50 51 public class FI_SAX_Or_XML_SAX_SAXEvent extends TransformInputOutput { 52 53 public FI_SAX_Or_XML_SAX_SAXEvent() { 54 } 55 56 public void parse(InputStream document, OutputStream events) throws Exception { 57 if (!document.markSupported()) { 58 document = new BufferedInputStream (document); 59 } 60 61 document.mark(4); 62 boolean isFastInfosetDocument = Decoder.isFastInfosetDocument(document); 63 document.reset(); 64 65 if (isFastInfosetDocument) { 66 SAXDocumentParser parser = new SAXDocumentParser(); 67 SAXEventSerializer ses = new SAXEventSerializer(events); 68 parser.setContentHandler(ses); 69 parser.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, ses); 70 parser.parse(document); 71 } else { 72 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 73 parserFactory.setNamespaceAware(true); 74 SAXParser parser = parserFactory.newSAXParser(); 75 SAXEventSerializer ses = new SAXEventSerializer(events); 76 parser.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, ses); 77 parser.parse(document, ses); 78 } 79 } 80 81 public static void main(String [] args) throws Exception { 82 FI_SAX_Or_XML_SAX_SAXEvent p = new FI_SAX_Or_XML_SAX_SAXEvent(); 83 p.parse(args); 84 } 85 } 86 | Popular Tags |