File: TestImport.java

package info (click to toggle)
axis 1.4-29
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 52,100 kB
  • sloc: java: 129,124; xml: 10,602; jsp: 983; sh: 84; cs: 36; makefile: 18
file content (76 lines) | stat: -rw-r--r-- 2,808 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package test.saaj;

import org.apache.axis.utils.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import test.AxisTestBase;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import java.io.ByteArrayInputStream;

public class TestImport extends AxisTestBase {

    public TestImport(String name) {
        super(name);
    }

    private static final String SAMPLE_1 =
            "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
            "\n" +
            "<SOAP-ENV:Body> " + "\n" +
            "<m:GetLastTradePrice xmlns:m=\"http://wombat.ztrade.com\">" +
            "\n" +
            "<symbol>SUNW</symbol> " + "\n" +
            "</m:GetLastTradePrice> " + "\n" +
            "</SOAP-ENV:Body> " + "\n" +
            "</SOAP-ENV:Envelope>";

    private SOAPMessage getSOAPMessageFromString(String str) throws Exception {
        MimeHeaders mimeHeaders = new MimeHeaders();
        mimeHeaders.addHeader("content-type", "text/xml");
        SOAPMessage message = MessageFactory.newInstance().createMessage(
                mimeHeaders,
                new ByteArrayInputStream(str.getBytes()));
        return message;
    }

    public void testImports() throws Exception {
        //DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //DocumentBuilder db = dbf.newDocumentBuilder();
        //Document doc1 = db.parse(new ByteArrayInputStream(SAMPLE_1.getBytes()));

        Document doc2 = testImportFromSaajToDom();
        Document body = testImportFromDomToSaaj(doc2);
        XMLUtils.PrettyDocumentToStream(body, System.out);
        //assertXMLEqual(doc1, body);
        //assertXMLEqual(doc2, body);
        //assertXMLEqual(doc1, doc2);
    }

    private Document testImportFromSaajToDom() throws Exception {
        SOAPMessage message = getSOAPMessageFromString(SAMPLE_1);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        org.w3c.dom.Node fromNode = message.getSOAPBody().getFirstChild();
        Node n = doc.importNode(fromNode, true);
        doc.appendChild(n);
        return doc;
    }

    private Document testImportFromDomToSaaj(Document doc) throws Exception {
        SOAPMessage sm = MessageFactory.newInstance().createMessage();
        SOAPPart sp = sm.getSOAPPart();
        SOAPBody body = sm.getSOAPBody();
        org.w3c.dom.Node node = sp.importNode(doc.getDocumentElement(), true);
        body.appendChild(node);
        return sp;
    }
}