KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestImport


1 package test.saaj;
2
3 import org.apache.axis.utils.XMLUtils;
4 import org.w3c.dom.Document JavaDoc;
5 import org.w3c.dom.Node JavaDoc;
6 import test.AxisTestBase;
7
8 import javax.xml.parsers.DocumentBuilder JavaDoc;
9 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
10 import javax.xml.soap.MessageFactory JavaDoc;
11 import javax.xml.soap.MimeHeaders JavaDoc;
12 import javax.xml.soap.SOAPBody JavaDoc;
13 import javax.xml.soap.SOAPMessage JavaDoc;
14 import javax.xml.soap.SOAPPart JavaDoc;
15 import java.io.ByteArrayInputStream JavaDoc;
16
17 public class TestImport extends AxisTestBase {
18
19     public TestImport(String JavaDoc name) {
20         super(name);
21     }
22
23     private static final String JavaDoc SAMPLE_1 =
24             "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
25             "\n" +
26             "<SOAP-ENV:Body> " + "\n" +
27             "<m:GetLastTradePrice xmlns:m=\"http://wombat.ztrade.com\">" +
28             "\n" +
29             "<symbol>SUNW</symbol> " + "\n" +
30             "</m:GetLastTradePrice> " + "\n" +
31             "</SOAP-ENV:Body> " + "\n" +
32             "</SOAP-ENV:Envelope>";
33
34     private SOAPMessage JavaDoc getSOAPMessageFromString(String JavaDoc str) throws Exception JavaDoc {
35         MimeHeaders JavaDoc mimeHeaders = new MimeHeaders JavaDoc();
36         mimeHeaders.addHeader("content-type", "text/xml");
37         SOAPMessage JavaDoc message = MessageFactory.newInstance().createMessage(
38                 mimeHeaders,
39                 new ByteArrayInputStream JavaDoc(str.getBytes()));
40         return message;
41     }
42
43     public void testImports() throws Exception JavaDoc {
44         //DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
45
//DocumentBuilder db = dbf.newDocumentBuilder();
46
//Document doc1 = db.parse(new ByteArrayInputStream(SAMPLE_1.getBytes()));
47

48         Document JavaDoc doc2 = testImportFromSaajToDom();
49         Document JavaDoc body = testImportFromDomToSaaj(doc2);
50         XMLUtils.PrettyDocumentToStream(body, System.out);
51         //assertXMLEqual(doc1, body);
52
//assertXMLEqual(doc2, body);
53
//assertXMLEqual(doc1, doc2);
54
}
55
56     private Document JavaDoc testImportFromSaajToDom() throws Exception JavaDoc {
57         SOAPMessage JavaDoc message = getSOAPMessageFromString(SAMPLE_1);
58         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
59         DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
60         Document JavaDoc doc = db.newDocument();
61         org.w3c.dom.Node JavaDoc fromNode = message.getSOAPBody().getFirstChild();
62         Node JavaDoc n = doc.importNode(fromNode, true);
63         doc.appendChild(n);
64         return doc;
65     }
66
67     private Document JavaDoc testImportFromDomToSaaj(Document JavaDoc doc) throws Exception JavaDoc {
68         SOAPMessage JavaDoc sm = MessageFactory.newInstance().createMessage();
69         SOAPPart JavaDoc sp = sm.getSOAPPart();
70         SOAPBody JavaDoc body = sm.getSOAPBody();
71         org.w3c.dom.Node JavaDoc node = sp.importNode(doc.getDocumentElement(), true);
72         body.appendChild(node);
73         return sp;
74     }
75 }
76
77
Popular Tags