KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jaxp > W3CDOMStreamReaderTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.jaxp;
18
19 import java.io.OutputStream JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
22 import javax.xml.stream.XMLStreamReader;
23 import javax.xml.transform.OutputKeys JavaDoc;
24 import javax.xml.transform.Transformer JavaDoc;
25 import javax.xml.transform.TransformerException JavaDoc;
26 import javax.xml.transform.TransformerFactory JavaDoc;
27 import javax.xml.transform.dom.DOMSource JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29
30 import org.w3c.dom.Attr JavaDoc;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34
35
36 /**
37  * @author <a HREF="mailto:dan@envoisolutions.com">Dan Diephouse</a>
38  * @since Oct 26, 2004
39  */

40 public class W3CDOMStreamReaderTest
41     extends AbstractStreamReaderTest
42 {
43     
44     
45     public void testSingleElement() throws Exception JavaDoc
46     {
47         Document JavaDoc doc = getDocument();
48         Element JavaDoc 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 JavaDoc getDocument() throws Exception JavaDoc{
65         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
66         factory.setNamespaceAware(true);
67         Document JavaDoc doc = factory.newDocumentBuilder().newDocument();
68         return doc;
69     }
70     
71     
72     public void testTextChild() throws Exception JavaDoc
73     {
74         Document JavaDoc doc = getDocument();
75         Element JavaDoc e = doc.createElementNS( "urn:test","root");
76         e.setAttribute("xmlns", "urn:test");
77         doc.appendChild(e);
78         Node JavaDoc 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 JavaDoc
89     {
90         Document JavaDoc doc = getDocument();
91         Element JavaDoc e = doc.createElementNS( "urn:test","test:root");
92         e.setAttribute("xmlns", "urn:test");
93         doc.appendChild(e);
94         Node JavaDoc text = doc.createTextNode("Hello World");
95         e.appendChild(text);
96         Element JavaDoc 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 JavaDoc
109     {
110         Document JavaDoc doc = getDocument();
111         
112         Element JavaDoc e = doc.createElementNS("urn:test","root");
113         e.setAttribute("xmlns", "urn:test");
114         doc.appendChild(e);
115         e.setAttribute("att1", "value1");
116         
117         Attr JavaDoc 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 JavaDoc
132     {
133         Document JavaDoc doc = getDocument();
134         Element JavaDoc e = doc.createElementNS("urn:test","root");
135         e.setAttribute("xmlns", "urn:test");
136         Element JavaDoc 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 JavaDoc doc, OutputStream JavaDoc out) throws TransformerException JavaDoc {
149         TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
150         //identity
151
Transformer JavaDoc t = tf.newTransformer();
152         t.setOutputProperty(OutputKeys.INDENT, "yes");
153         t.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(out));
154     }
155 }
156
Popular Tags