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 import java.util.Arrays ; 23 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.stream.XMLInputFactory; 26 import javax.xml.stream.XMLStreamReader; 27 import javax.xml.transform.Transformer ; 28 import javax.xml.transform.TransformerFactory ; 29 import javax.xml.transform.dom.DOMResult ; 30 import javax.xml.transform.dom.DOMSource ; 31 import javax.xml.transform.stream.StreamResult ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.servicemix.jbi.jaxp.StaxSource; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.NodeList ; 39 import org.w3c.dom.Text ; 40 41 import junit.framework.TestCase; 42 43 public class StaxSourceTest extends TestCase { 44 45 private static final Log log = LogFactory.getLog(StaxSourceTest.class); 46 47 public void testStaxSourceOnStream() throws Exception { 48 InputStream is = getClass().getResourceAsStream("test.xml"); 49 XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is); 50 StaxSource ss = new StaxSource(xsr); 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 public void testStaxSourceOnDOM() throws Exception { 64 InputStream is = getClass().getResourceAsStream("test.xml"); 65 XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is); 66 StaxSource ss = new StaxSource(xsr); 67 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 68 DOMResult result = new DOMResult (); 69 transformer.transform(ss, result); 70 assertNotNull(result.getNode()); 71 } 72 73 public void testStaxToDOM() throws Exception { 74 InputStream is = getClass().getResourceAsStream("test.xml"); 75 XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is); 76 StaxSource ss = new StaxSource(xsr); 77 DOMSource src = new SourceTransformer().toDOMSource(ss); 78 assertNotNull(src); 79 assertNotNull(src.getNode()); 80 NodeList nl = ((Document ) src.getNode()).getDocumentElement().getElementsByTagName("long"); 81 assertEquals(1, nl.getLength()); 82 Text txt = (Text ) nl.item(0).getFirstChild(); 83 System.out.println(txt.getTextContent()); 84 85 StringBuffer expected = new StringBuffer (); 86 for (int i = 0; i < 4; i++) { 87 for (int j = 0; j < 10; j++) { 88 for (int k = 0; k < 10; k++) { 89 expected.append((char)('0' + j)); 90 expected.append((char)('0' + k)); 91 if (k != 9) { 92 expected.append(' '); 93 } 94 } 95 expected.append("\n"); 96 } 97 } 98 107 assertEquals(expected.toString(), txt.getTextContent()); 108 } 109 110 } 111 | Popular Tags |