1 38 39 40 package com.sun.xml.fastinfoset.tools; 41 42 import com.sun.xml.fastinfoset.Decoder; 43 import com.sun.xml.fastinfoset.dom.DOMDocumentParser; 44 import java.io.BufferedInputStream ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 import javax.xml.parsers.DocumentBuilder ; 48 import javax.xml.parsers.DocumentBuilderFactory ; 49 import javax.xml.transform.Transformer ; 50 import javax.xml.transform.TransformerFactory ; 51 import javax.xml.transform.dom.DOMSource ; 52 import javax.xml.transform.sax.SAXResult ; 53 import org.w3c.dom.Document ; 54 55 public class FI_DOM_Or_XML_DOM_SAX_SAXEvent extends TransformInputOutput { 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 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 67 dbf.setNamespaceAware(true); 68 DocumentBuilder db = dbf.newDocumentBuilder(); 69 70 Document d; 71 if (isFastInfosetDocument) { 72 d = db.newDocument(); 73 DOMDocumentParser ddp = new DOMDocumentParser(); 74 ddp.parse(d, document); 75 } else { 76 d = db.parse(document); 77 } 78 79 SAXEventSerializer ses = new SAXEventSerializer(events); 80 81 TransformerFactory tf = TransformerFactory.newInstance(); 82 Transformer t = tf.newTransformer(); 83 t.transform(new DOMSource (d), new SAXResult (ses)); 84 } 85 86 public static void main(String [] args) throws Exception { 87 FI_DOM_Or_XML_DOM_SAX_SAXEvent p = new FI_DOM_Or_XML_DOM_SAX_SAXEvent(); 88 p.parse(args); 89 } 90 91 } 92 | Popular Tags |