01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.staxutils;
19:
20: import java.io.ByteArrayInputStream;
21:
22: import javax.xml.parsers.DocumentBuilder;
23: import javax.xml.parsers.DocumentBuilderFactory;
24: import javax.xml.soap.MessageFactory;
25: import javax.xml.soap.SOAPMessage;
26: import javax.xml.soap.SOAPPart;
27: import javax.xml.stream.XMLStreamReader;
28: import javax.xml.transform.dom.DOMSource;
29:
30: import org.w3c.dom.Document;
31:
32: import org.apache.cxf.helpers.XMLUtils;
33:
34: import org.junit.Assert;
35: import org.junit.Test;
36:
37: public class W3CDOMStreamReaderTest extends Assert {
38:
39: private static final String RESULT = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
40: + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
41: + "<SOAP-ENV:Header/><SOAP-ENV:Body/>"
42: + "<Test xmlns=\"http://example.org/types\">"
43: + "<argument>foobar</argument></Test></SOAP-ENV:Envelope>";
44:
45: @Test
46: public void testReader() throws Exception {
47: ByteArrayInputStream is = new ByteArrayInputStream(
48: "<Test xmlns=\"http://example.org/types\"><argument>foobar</argument></Test>"
49: .getBytes());
50: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
51: .newInstance();
52: docBuilderFactory.setNamespaceAware(true);
53: DocumentBuilder docBuilder = docBuilderFactory
54: .newDocumentBuilder();
55:
56: MessageFactory factory = MessageFactory.newInstance();
57: SOAPMessage msg = factory.createMessage();
58: SOAPPart part = msg.getSOAPPart();
59:
60: Document doc = docBuilder.parse(is);
61:
62: W3CDOMStreamWriter writer = new W3CDOMStreamWriter(part
63: .getEnvelope());
64: XMLStreamReader reader = StaxUtils
65: .createXMLStreamReader(new DOMSource(doc));
66:
67: StaxUtils.copy(reader, writer);
68: assertEquals(RESULT, XMLUtils.toString(writer.getDocument()));
69:
70: }
71:
72: }
|