| 1 |
package test.saaj; |
| 2 |
|
| 3 |
import org.w3c.dom.Document; |
| 4 |
import org.w3c.dom.Node; |
| 5 |
import org.w3c.dom.NamedNodeMap; |
| 6 |
import org.w3c.dom.Attr; |
| 7 |
import org.w3c.dom.Text; |
| 8 |
import org.w3c.dom.NodeList; |
| 9 |
|
| 10 |
import javax.xml.soap.MessageFactory; |
| 11 |
import javax.xml.soap.Name; |
| 12 |
import javax.xml.soap.SOAPBody; |
| 13 |
import javax.xml.soap.SOAPElement; |
| 14 |
import javax.xml.soap.SOAPHeader; |
| 15 |
import javax.xml.soap.SOAPMessage; |
| 16 |
import javax.xml.soap.SOAPFactory; |
| 17 |
import javax.xml.soap.SOAPBodyElement; |
| 18 |
import javax.xml.soap.SOAPEnvelope; |
| 19 |
import javax.xml.soap.SOAPPart; |
| 20 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 21 |
import javax.xml.parsers.DocumentBuilder; |
| 22 |
import java.io.ByteArrayInputStream; |
| 23 |
|
| 24 |
public class TestText extends junit.framework.TestCase { |
| 25 |
|
| 26 |
public TestText(String name) { |
| 27 |
super(name); |
| 28 |
} |
| 29 |
|
| 30 |
// Test SAAJ addTextNode performance |
| 31 |
public void testAddTextNode() throws Exception { |
| 32 |
SOAPFactory soapFactory = SOAPFactory.newInstance(); |
| 33 |
MessageFactory factory = MessageFactory.newInstance(); |
| 34 |
SOAPMessage message = factory.createMessage(); |
| 35 |
SOAPHeader header = message.getSOAPHeader(); |
| 36 |
SOAPBody body = message.getSOAPBody(); |
| 37 |
|
| 38 |
// Create the base element |
| 39 |
Name bodyName = soapFactory.createName("VBGenReceiver", "xsi", |
| 40 |
"http://www.w3.org/2001/XMLSchema-instance"); |
| 41 |
SOAPBodyElement bodyElement = body.addBodyElement(bodyName); |
| 42 |
|
| 43 |
// Create the MetaData Tag |
| 44 |
Name name = soapFactory.createName("MetaData"); |
| 45 |
SOAPElement metaData = bodyElement.addChildElement(name); |
| 46 |
|
| 47 |
//Create the SKey Tag |
| 48 |
name = soapFactory.createName("SKey"); |
| 49 |
SOAPElement sKey = metaData.addChildElement(name); |
| 50 |
sKey.addTextNode("SKEY001"); |
| 51 |
|
| 52 |
//Create Object Tag |
| 53 |
name = soapFactory.createName("Object"); |
| 54 |
SOAPElement object = bodyElement.addChildElement(name); |
| 55 |
|
| 56 |
//Create Book ID Tag |
| 57 |
name = soapFactory.createName("BookID"); |
| 58 |
SOAPElement bookID = object.addChildElement(name); |
| 59 |
bookID.addTextNode("BookID002"); |
| 60 |
|
| 61 |
//Create OrderID tag |
| 62 |
name = soapFactory.createName("OrderID"); |
| 63 |
SOAPElement orderID = object.addChildElement(name); |
| 64 |
orderID.addTextNode("OrderID003"); |
| 65 |
|
| 66 |
//create PurchaseID tage |
| 67 |
name = soapFactory.createName("PurchaseID"); |
| 68 |
SOAPElement purchaseID = object.addChildElement(name); |
| 69 |
purchaseID.addTextNode("PurchaseID005"); |
| 70 |
|
| 71 |
//create LanguageID Tag |
| 72 |
name = soapFactory.createName("LanguageID"); |
| 73 |
SOAPElement languageID = object.addChildElement(name); |
| 74 |
languageID.addTextNode("LanguageID004"); |
| 75 |
|
| 76 |
//create LanguageID Tag |
| 77 |
name = soapFactory.createName("LanguageName"); |
| 78 |
SOAPElement languageName = object.addChildElement(name); |
| 79 |
languageName.addTextNode("LanguageName006"); |
| 80 |
|
| 81 |
//create LanguageID Tag |
| 82 |
name = soapFactory.createName("Title"); |
| 83 |
SOAPElement title = object.addChildElement(name); |
| 84 |
title.addTextNode("Title007"); |
| 85 |
|
| 86 |
//create LanguageID Tag |
| 87 |
name = soapFactory.createName("Author"); |
| 88 |
SOAPElement author = object.addChildElement(name); |
| 89 |
author.addTextNode("Author008"); |
| 90 |
|
| 91 |
//create LanguageID Tag |
| 92 |
name = soapFactory.createName("Format"); |
| 93 |
SOAPElement format = bodyElement.addChildElement(name); |
| 94 |
|
| 95 |
//create LanguageID Tag |
| 96 |
name = soapFactory.createName("Type"); |
| 97 |
SOAPElement formatType = format.addChildElement(name); |
| 98 |
formatType.addTextNode("Type009"); |
| 99 |
|
| 100 |
//create LanguageID Tag |
| 101 |
name = soapFactory.createName("Delivery"); |
| 102 |
SOAPElement delivery = bodyElement.addChildElement(name); |
| 103 |
|
| 104 |
//create LanguageID Tag |
| 105 |
name = soapFactory.createName("Name"); |
| 106 |
SOAPElement delName = delivery.addChildElement(name); |
| 107 |
delName.addTextNode("Name010"); |
| 108 |
|
| 109 |
//create LanguageID Tag |
| 110 |
name = soapFactory.createName("Address1"); |
| 111 |
SOAPElement address1 = delivery.addChildElement(name); |
| 112 |
address1.addTextNode("Address1011"); |
| 113 |
|
| 114 |
//create LanguageID Tag |
| 115 |
name = soapFactory.createName("Address2"); |
| 116 |
SOAPElement address2 = delivery.addChildElement(name); |
| 117 |
address2.addTextNode("Address2012"); |
| 118 |
|
| 119 |
//create LanguageID Tag |
| 120 |
name = soapFactory.createName("City"); |
| 121 |
SOAPElement city = delivery.addChildElement(name); |
| 122 |
city.addTextNode("City013"); |
| 123 |
|
| 124 |
//create LanguageID Tag |
| 125 |
name = soapFactory.createName("State"); |
| 126 |
SOAPElement state = delivery.addChildElement(name); |
| 127 |
state.addTextNode("State014"); |
| 128 |
|
| 129 |
//create LanguageID Tag |
| 130 |
name = soapFactory.createName("PostalCode"); |
| 131 |
SOAPElement postalCode = delivery.addChildElement(name); |
| 132 |
postalCode.addTextNode("PostalCode015"); |
| 133 |
|
| 134 |
System.out.println("The message is lll:\n"); |
| 135 |
message.writeTo(System.out); |
| 136 |
} |
| 137 |
|
| 138 |
public void testTraverseDOM() throws Exception { |
| 139 |
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 140 |
"<book year=\"1992\">\n" + |
| 141 |
" <title>Advanced Programming in the Unix environment</title>\n" + |
| 142 |
" <author><last>Stevens</last><first>W.</first></author>\n" + |
| 143 |
" <publisher>Addison-Wesley</publisher>\n" + |
| 144 |
" <price>65.95</price>\n" + |
| 145 |
"</book>\n" + |
| 146 |
""; |
| 147 |
|
| 148 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 149 |
factory.setNamespaceAware(true); |
| 150 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
| 151 |
Document payload = builder.parse(new ByteArrayInputStream(xml.getBytes())); |
| 152 |
|
| 153 |
MessageFactory soapMsgFactory = MessageFactory.newInstance(); |
| 154 |
SOAPMessage soapMsg = soapMsgFactory.createMessage(); |
| 155 |
|
| 156 |
SOAPPart soapPart = soapMsg.getSOAPPart(); |
| 157 |
SOAPEnvelope soapEnv = soapPart.getEnvelope(); |
| 158 |
SOAPBody soapBody = soapEnv.getBody(); |
| 159 |
|
| 160 |
soapBody.addDocument(payload); |
| 161 |
|
| 162 |
System.out.println("***************"); |
| 163 |
soapMsg.writeTo(System.out); |
| 164 |
|
| 165 |
processNode(soapPart); |
| 166 |
} |
| 167 |
|
| 168 |
private void processNode(Node currentNode) { |
| 169 |
switch (currentNode.getNodeType()) { |
| 170 |
// process a Document node |
| 171 |
case Node.DOCUMENT_NODE: |
| 172 |
Document doc = (Document) currentNode; |
| 173 |
System.out.println("Document node: " + doc.getNodeName() + |
| 174 |
"\nRoot element: " + |
| 175 |
doc.getDocumentElement().getNodeName()); |
| 176 |
processChildNodes(doc.getChildNodes()); |
| 177 |
break; |
| 178 |
|
| 179 |
// process an Element node |
| 180 |
case Node.ELEMENT_NODE: |
| 181 |
System.out.println("\nElement node: " + |
| 182 |
currentNode.getNodeName()); |
| 183 |
NamedNodeMap attributeNodes = |
| 184 |
currentNode.getAttributes(); |
| 185 |
for (int i = 0; i < attributeNodes.getLength(); i++) { |
| 186 |
Attr attribute = (Attr) attributeNodes.item(i); |
| 187 |
System.out.println("\tAttribute: " + |
| 188 |
attribute.getNodeName() + " ; Value = " + |
| 189 |
attribute.getNodeValue()); |
| 190 |
} |
| 191 |
processChildNodes(currentNode.getChildNodes()); |
| 192 |
break; |
| 193 |
|
| 194 |
// process a text node and a CDATA section |
| 195 |
case Node.CDATA_SECTION_NODE: |
| 196 |
case Node.TEXT_NODE: |
| 197 |
Text text = (Text) currentNode; |
| 198 |
if (!text.getNodeValue().trim().equals("")) |
| 199 |
System.out.println("\tText: " + |
| 200 |
text.getNodeValue()); |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
private void processChildNodes(NodeList children) { |
| 206 |
if (children.getLength() != 0) |
| 207 |
for (int i = 0; i < children.getLength(); i++) |
| 208 |
processNode(children.item(i)); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
public static void main(String[] args) throws Exception { |
| 213 |
TestText tester = new TestText("TestEnvelope"); |
| 214 |
tester.testAddTextNode(); |
| 215 |
} |
| 216 |
} |