1 17 package org.apache.servicemix.jbi.jaxp; 18 19 import java.io.OutputStream ; 20 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import javax.xml.stream.XMLStreamReader; 23 import javax.xml.transform.OutputKeys ; 24 import javax.xml.transform.Transformer ; 25 import javax.xml.transform.TransformerException ; 26 import javax.xml.transform.TransformerFactory ; 27 import javax.xml.transform.dom.DOMSource ; 28 import javax.xml.transform.stream.StreamResult ; 29 30 import org.w3c.dom.Attr ; 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.Node ; 34 35 36 40 public class W3CDOMStreamReaderTest 41 extends AbstractStreamReaderTest 42 { 43 44 45 public void testSingleElement() throws Exception 46 { 47 Document doc = getDocument(); 48 Element e = doc.createElementNS("urn:test","root"); 49 e.setAttribute("xmlns", "urn:test"); 50 doc.appendChild(e); 51 52 assertEquals(1, e.getAttributes().getLength()); 53 System.out.println("start: " + XMLStreamReader.START_ELEMENT); 54 System.out.println("attr: " + XMLStreamReader.ATTRIBUTE); 55 System.out.println("ns: " + XMLStreamReader.NAMESPACE); 56 System.out.println("chars: " + XMLStreamReader.CHARACTERS); 57 System.out.println("end: " + XMLStreamReader.END_ELEMENT); 58 59 writeXml(doc,System.out); 60 W3CDOMStreamReader reader = new W3CDOMStreamReader(doc.getDocumentElement()); 61 testSingleElement(reader); 62 } 63 64 private Document getDocument() throws Exception { 65 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 66 factory.setNamespaceAware(true); 67 Document doc = factory.newDocumentBuilder().newDocument(); 68 return doc; 69 } 70 71 72 public void testTextChild() throws Exception 73 { 74 Document doc = getDocument(); 75 Element e = doc.createElementNS( "urn:test","root"); 76 e.setAttribute("xmlns", "urn:test"); 77 doc.appendChild(e); 78 Node text = doc.createTextNode("Hello World"); 79 e.appendChild(text); 80 81 writeXml(doc,System.out); 82 83 W3CDOMStreamReader reader = new W3CDOMStreamReader(e); 84 testTextChild(reader); 85 } 86 87 88 public void testMixedContent() throws Exception 89 { 90 Document doc = getDocument(); 91 Element e = doc.createElementNS( "urn:test","test:root"); 92 e.setAttribute("xmlns", "urn:test"); 93 doc.appendChild(e); 94 Node text = doc.createTextNode("Hello World"); 95 e.appendChild(text); 96 Element child = doc.createElement("element"); 97 e.appendChild(child); 98 text = doc.createTextNode(" more text"); 99 e.appendChild(text); 100 101 writeXml(doc,System.out); 102 103 W3CDOMStreamReader reader = new W3CDOMStreamReader(e); 104 testMixedContent(reader); 105 } 106 107 108 public void testAttributes() throws Exception 109 { 110 Document doc = getDocument(); 111 112 Element e = doc.createElementNS("urn:test","root"); 113 e.setAttribute("xmlns", "urn:test"); 114 doc.appendChild(e); 115 e.setAttribute("att1", "value1"); 116 117 Attr attr = doc.createAttributeNS("urn:test2","att2"); 118 attr.setValue("value2"); 119 attr.setPrefix("p"); 120 121 e.setAttribute("xmlns:p", "urn:test2"); 122 123 e.setAttributeNode(attr); 124 writeXml(doc,System.out); 125 126 W3CDOMStreamReader reader = new W3CDOMStreamReader(doc.getDocumentElement()); 127 128 testAttributes(reader); 129 } 130 131 public void testElementChild() throws Exception 132 { 133 Document doc = getDocument(); 134 Element e = doc.createElementNS("urn:test","root"); 135 e.setAttribute("xmlns", "urn:test"); 136 Element child = doc.createElementNS("urn:test2","child"); 137 child.setAttribute("xmlns:a", "urn:test2"); 138 139 child.setPrefix("a"); 140 e.appendChild(child); 141 doc.appendChild(e); 142 writeXml(doc,System.out); 143 144 W3CDOMStreamReader reader = new W3CDOMStreamReader(e); 145 testElementChild(reader); 146 } 147 148 protected void writeXml(Document doc, OutputStream out) throws TransformerException { 149 TransformerFactory tf = TransformerFactory.newInstance(); 150 Transformer t = tf.newTransformer(); 152 t.setOutputProperty(OutputKeys.INDENT, "yes"); 153 t.transform(new DOMSource (doc), new StreamResult (out)); 154 } 155 } 156 | Popular Tags |