KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24 import javax.xml.stream.XMLInputFactory;
25 import javax.xml.stream.XMLStreamReader;
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.TransformerFactory JavaDoc;
28 import javax.xml.transform.dom.DOMSource JavaDoc;
29 import javax.xml.transform.stream.StreamResult JavaDoc;
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 JavaDoc;
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 JavaDoc {
42         InputStream JavaDoc 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 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 }
64
Popular Tags