1 17 package org.apache.servicemix.jbi.jaxp; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.InputStream ; 21 import java.io.StringWriter ; 22 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.stream.XMLInputFactory; 25 import javax.xml.stream.XMLStreamReader; 26 import javax.xml.transform.Transformer ; 27 import javax.xml.transform.TransformerFactory ; 28 import javax.xml.transform.dom.DOMSource ; 29 import javax.xml.transform.stream.StreamResult ; 30 31 import junit.framework.TestCase; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.w3c.dom.Document ; 36 37 public class FragmentStreamReaderTest extends TestCase { 38 39 private static final Log log = LogFactory.getLog(FragmentStreamReaderTest.class); 40 41 public void testStaxSource() throws Exception { 42 InputStream is = getClass().getResourceAsStream("test.xml"); 43 XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is); 44 xsr = new ExtendedXMLStreamReader(xsr); 45 xsr.nextTag(); 46 log.info(xsr.getName()); 47 xsr.nextTag(); 48 log.info(xsr.getName()); 49 XMLStreamReader fsr = new FragmentStreamReader(xsr); 50 StaxSource ss = new StaxSource(fsr); 51 StringWriter buffer = new StringWriter (); 52 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 53 transformer.transform(ss, new StreamResult (buffer)); 54 log.info(buffer.toString()); 55 DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance(); 56 dbf.setNamespaceAware(true); 57 Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream (buffer.toString().getBytes())); 58 StringWriter buffer2 = new StringWriter (); 59 transformer.transform(new DOMSource (doc), new StreamResult (buffer2)); 60 log.info(buffer2.toString()); 61 } 62 63 } 64 | Popular Tags |