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.Properties; 44 import com.sun.xml.fastinfoset.stax.StAXDocumentParser; 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 import com.sun.xml.fastinfoset.stax.StAX2SAXReader; 51 52 public class FI_StAX_SAX_Or_XML_SAX_SAXEvent extends TransformInputOutput { 53 54 public FI_StAX_SAX_Or_XML_SAX_SAXEvent() { 55 } 56 57 public void parse(InputStream document, OutputStream events) throws Exception { 58 if (!document.markSupported()) { 59 document = new BufferedInputStream (document); 60 } 61 62 document.mark(4); 63 boolean isFastInfosetDocument = Decoder.isFastInfosetDocument(document); 64 document.reset(); 65 66 if (isFastInfosetDocument) { 67 StAXDocumentParser parser = new StAXDocumentParser(); 68 parser.setInputStream(document); 69 SAXEventSerializer ses = new SAXEventSerializer(events); 70 StAX2SAXReader reader = new StAX2SAXReader(parser, ses); 71 reader.setLexicalHandler(ses); 72 reader.adapt(); 73 } else { 74 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 75 parserFactory.setNamespaceAware(true); 76 SAXParser parser = parserFactory.newSAXParser(); 77 SAXEventSerializer ses = new SAXEventSerializer(events); 78 parser.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, ses); 79 parser.parse(document, ses); 80 } 81 } 82 83 public static void main(String [] args) throws Exception { 84 FI_StAX_SAX_Or_XML_SAX_SAXEvent p = new FI_StAX_SAX_Or_XML_SAX_SAXEvent(); 85 p.parse(args); 86 } 87 88 } 89 | Popular Tags |