1 38 39 40 package com.sun.xml.fastinfoset.tools; 41 42 import com.sun.xml.fastinfoset.Decoder; 43 import org.jvnet.fastinfoset.FastInfosetSource; 44 import java.io.BufferedInputStream ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 import javax.xml.transform.Transformer ; 48 import javax.xml.transform.TransformerFactory ; 49 import javax.xml.transform.dom.DOMResult ; 50 import javax.xml.transform.dom.DOMSource ; 51 import javax.xml.transform.sax.SAXResult ; 52 import javax.xml.transform.stream.StreamSource ; 53 54 public class FI_SAX_Or_XML_SAX_DOM_SAX_SAXEvent extends TransformInputOutput { 55 56 public FI_SAX_Or_XML_SAX_DOM_SAX_SAXEvent() { 57 } 58 59 public void parse(InputStream document, OutputStream events) throws Exception { 60 if (!document.markSupported()) { 61 document = new BufferedInputStream (document); 62 } 63 64 document.mark(4); 65 boolean isFastInfosetDocument = Decoder.isFastInfosetDocument(document); 66 document.reset(); 67 68 TransformerFactory tf = TransformerFactory.newInstance(); 69 Transformer t = tf.newTransformer(); 70 DOMResult dr = new DOMResult (); 71 72 if (isFastInfosetDocument) { 73 t.transform(new FastInfosetSource(document), dr); 74 } else { 75 t.transform(new StreamSource (document), dr); 76 } 77 78 SAXEventSerializer ses = new SAXEventSerializer(events); 79 t.transform(new DOMSource (dr.getNode()), new SAXResult (ses)); 80 } 81 82 public static void main(String [] args) throws Exception { 83 FI_SAX_Or_XML_SAX_DOM_SAX_SAXEvent p = new FI_SAX_Or_XML_SAX_DOM_SAX_SAXEvent(); 84 p.parse(args); 85 } 86 87 } 88 | Popular Tags |