KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.Arrays JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.stream.XMLInputFactory;
26 import javax.xml.stream.XMLStreamReader;
27 import javax.xml.transform.Transformer JavaDoc;
28 import javax.xml.transform.TransformerFactory JavaDoc;
29 import javax.xml.transform.dom.DOMResult JavaDoc;
30 import javax.xml.transform.dom.DOMSource JavaDoc;
31 import javax.xml.transform.stream.StreamResult JavaDoc;
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 JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.w3c.dom.Text JavaDoc;
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 JavaDoc {
48         InputStream JavaDoc is = getClass().getResourceAsStream("test.xml");
49         XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
50         StaxSource ss = new StaxSource(xsr);
51         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
52         Transformer JavaDoc transformer = TransformerFactory.newInstance().newTransformer();
53         transformer.transform(ss, new StreamResult JavaDoc(buffer));
54         log.info(buffer.toString());
55         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
56         dbf.setNamespaceAware(true);
57         Document JavaDoc doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buffer.toString().getBytes()));
58         StringWriter JavaDoc buffer2 = new StringWriter JavaDoc();
59         transformer.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(buffer2));
60         log.info(buffer2.toString());
61     }
62
63     public void testStaxSourceOnDOM() throws Exception JavaDoc {
64         InputStream JavaDoc is = getClass().getResourceAsStream("test.xml");
65         XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
66         StaxSource ss = new StaxSource(xsr);
67         Transformer JavaDoc transformer = TransformerFactory.newInstance().newTransformer();
68         DOMResult JavaDoc result = new DOMResult JavaDoc();
69         transformer.transform(ss, result);
70         assertNotNull(result.getNode());
71     }
72
73     public void testStaxToDOM() throws Exception JavaDoc {
74         InputStream JavaDoc is = getClass().getResourceAsStream("test.xml");
75         XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
76         StaxSource ss = new StaxSource(xsr);
77         DOMSource JavaDoc src = new SourceTransformer().toDOMSource(ss);
78         assertNotNull(src);
79         assertNotNull(src.getNode());
80         NodeList JavaDoc nl = ((Document JavaDoc) src.getNode()).getDocumentElement().getElementsByTagName("long");
81         assertEquals(1, nl.getLength());
82         Text JavaDoc txt = (Text JavaDoc) nl.item(0).getFirstChild();
83         System.out.println(txt.getTextContent());
84         
85         StringBuffer JavaDoc expected = new StringBuffer JavaDoc();
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         /*
99         char[] c1 = txt.getTextContent().toCharArray();
100         char[] c2 = expected.toString().toCharArray();
101         for (int i = 0; i < c1.length; i++) {
102             if (c1[i] != c2[i]) {
103                 fail("Expected '" + (int)c2[i] + "' but found '" + (int)c1[i] + "' at index " + i);
104             }
105         }
106         */

107         assertEquals(expected.toString(), txt.getTextContent());
108     }
109
110 }
111
Popular Tags