KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestText


1 package test.saaj;
2
3 import org.w3c.dom.Document JavaDoc;
4 import org.w3c.dom.Node JavaDoc;
5 import org.w3c.dom.NamedNodeMap JavaDoc;
6 import org.w3c.dom.Attr JavaDoc;
7 import org.w3c.dom.Text JavaDoc;
8 import org.w3c.dom.NodeList JavaDoc;
9
10 import javax.xml.soap.MessageFactory JavaDoc;
11 import javax.xml.soap.Name JavaDoc;
12 import javax.xml.soap.SOAPBody JavaDoc;
13 import javax.xml.soap.SOAPElement JavaDoc;
14 import javax.xml.soap.SOAPHeader JavaDoc;
15 import javax.xml.soap.SOAPMessage JavaDoc;
16 import javax.xml.soap.SOAPFactory JavaDoc;
17 import javax.xml.soap.SOAPBodyElement JavaDoc;
18 import javax.xml.soap.SOAPEnvelope JavaDoc;
19 import javax.xml.soap.SOAPPart JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import java.io.ByteArrayInputStream JavaDoc;
23
24 public class TestText extends junit.framework.TestCase {
25
26     public TestText(String JavaDoc name) {
27         super(name);
28     }
29
30     // Test SAAJ addTextNode performance
31
public void testAddTextNode() throws Exception JavaDoc {
32         SOAPFactory JavaDoc soapFactory = SOAPFactory.newInstance();
33         MessageFactory JavaDoc factory = MessageFactory.newInstance();
34         SOAPMessage JavaDoc message = factory.createMessage();
35         SOAPHeader JavaDoc header = message.getSOAPHeader();
36         SOAPBody JavaDoc body = message.getSOAPBody();
37
38         // Create the base element
39
Name JavaDoc bodyName = soapFactory.createName("VBGenReceiver", "xsi",
40                 "http://www.w3.org/2001/XMLSchema-instance");
41         SOAPBodyElement JavaDoc bodyElement = body.addBodyElement(bodyName);
42
43         // Create the MetaData Tag
44
Name JavaDoc name = soapFactory.createName("MetaData");
45         SOAPElement JavaDoc metaData = bodyElement.addChildElement(name);
46
47         //Create the SKey Tag
48
name = soapFactory.createName("SKey");
49         SOAPElement JavaDoc sKey = metaData.addChildElement(name);
50         sKey.addTextNode("SKEY001");
51
52         //Create Object Tag
53
name = soapFactory.createName("Object");
54         SOAPElement JavaDoc object = bodyElement.addChildElement(name);
55
56         //Create Book ID Tag
57
name = soapFactory.createName("BookID");
58         SOAPElement JavaDoc bookID = object.addChildElement(name);
59         bookID.addTextNode("BookID002");
60
61         //Create OrderID tag
62
name = soapFactory.createName("OrderID");
63         SOAPElement JavaDoc orderID = object.addChildElement(name);
64         orderID.addTextNode("OrderID003");
65
66         //create PurchaseID tage
67
name = soapFactory.createName("PurchaseID");
68         SOAPElement JavaDoc purchaseID = object.addChildElement(name);
69         purchaseID.addTextNode("PurchaseID005");
70
71         //create LanguageID Tag
72
name = soapFactory.createName("LanguageID");
73         SOAPElement JavaDoc languageID = object.addChildElement(name);
74         languageID.addTextNode("LanguageID004");
75
76         //create LanguageID Tag
77
name = soapFactory.createName("LanguageName");
78         SOAPElement JavaDoc languageName = object.addChildElement(name);
79         languageName.addTextNode("LanguageName006");
80
81         //create LanguageID Tag
82
name = soapFactory.createName("Title");
83         SOAPElement JavaDoc title = object.addChildElement(name);
84         title.addTextNode("Title007");
85
86         //create LanguageID Tag
87
name = soapFactory.createName("Author");
88         SOAPElement JavaDoc author = object.addChildElement(name);
89         author.addTextNode("Author008");
90
91         //create LanguageID Tag
92
name = soapFactory.createName("Format");
93         SOAPElement JavaDoc format = bodyElement.addChildElement(name);
94
95         //create LanguageID Tag
96
name = soapFactory.createName("Type");
97         SOAPElement JavaDoc formatType = format.addChildElement(name);
98         formatType.addTextNode("Type009");
99
100         //create LanguageID Tag
101
name = soapFactory.createName("Delivery");
102         SOAPElement JavaDoc delivery = bodyElement.addChildElement(name);
103
104         //create LanguageID Tag
105
name = soapFactory.createName("Name");
106         SOAPElement JavaDoc delName = delivery.addChildElement(name);
107         delName.addTextNode("Name010");
108
109         //create LanguageID Tag
110
name = soapFactory.createName("Address1");
111         SOAPElement JavaDoc address1 = delivery.addChildElement(name);
112         address1.addTextNode("Address1011");
113
114         //create LanguageID Tag
115
name = soapFactory.createName("Address2");
116         SOAPElement JavaDoc address2 = delivery.addChildElement(name);
117         address2.addTextNode("Address2012");
118
119         //create LanguageID Tag
120
name = soapFactory.createName("City");
121         SOAPElement JavaDoc city = delivery.addChildElement(name);
122         city.addTextNode("City013");
123
124         //create LanguageID Tag
125
name = soapFactory.createName("State");
126         SOAPElement JavaDoc state = delivery.addChildElement(name);
127         state.addTextNode("State014");
128
129         //create LanguageID Tag
130
name = soapFactory.createName("PostalCode");
131         SOAPElement JavaDoc 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 JavaDoc {
139         String JavaDoc 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 JavaDoc factory = DocumentBuilderFactory.newInstance();
149         factory.setNamespaceAware(true);
150         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
151         Document JavaDoc payload = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
152
153         MessageFactory JavaDoc soapMsgFactory = MessageFactory.newInstance();
154         SOAPMessage JavaDoc soapMsg = soapMsgFactory.createMessage();
155
156         SOAPPart JavaDoc soapPart = soapMsg.getSOAPPart();
157         SOAPEnvelope JavaDoc soapEnv = soapPart.getEnvelope();
158         SOAPBody JavaDoc 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 JavaDoc currentNode) {
169         switch (currentNode.getNodeType()) {
170             // process a Document node
171
case Node.DOCUMENT_NODE:
172                 Document JavaDoc doc = (Document JavaDoc) 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 JavaDoc attributeNodes =
184                         currentNode.getAttributes();
185                 for (int i = 0; i < attributeNodes.getLength(); i++) {
186                     Attr JavaDoc attribute = (Attr JavaDoc) 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 JavaDoc text = (Text JavaDoc) 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 JavaDoc 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 JavaDoc[] args) throws Exception JavaDoc {
213         TestText tester = new TestText("TestEnvelope");
214         tester.testAddTextNode();
215     }
216 }
217
Popular Tags