KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > wsdl > sequence > SequenceService


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package test.wsdl.sequence;
18
19 import org.apache.axis.utils.XMLUtils;
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import org.w3c.dom.Text JavaDoc;
25
26 /**
27  * Sequence test service. This is a custom built message-style service
28  * which confirms that the XML it receives correctly contains ordered
29  * elements <zero> through <five>.
30  *
31  * @author Glen Daniels (gdaniels@apache.org)
32  */

33 public class SequenceService {
34     private String JavaDoc [] names = new String JavaDoc [] { "zero",
35                                               "one",
36                                               "two",
37                                               "three",
38                                               "four",
39                                               "five" };
40     /**
41      * This is a message-style service because we're just testing the
42      * serialization.
43      *
44      * @return a SOAP response in a DOM Element, either boolean true or false,
45      * indicating the success/failure of the test.
46      */

47     public Element JavaDoc [] testSequence(Element JavaDoc [] elems) throws Exception JavaDoc {
48         Element JavaDoc zero = null;
49         for (int i = 0; i < elems.length; i++) {
50             zero = findTheZero(elems[i]);
51             if (zero != null)
52                 break;
53         }
54
55         Document JavaDoc retDoc = XMLUtils.newDocument();
56         Element JavaDoc [] ret = new Element JavaDoc [1];
57         ret[0] = retDoc.createElementNS("urn:SequenceTest",
58                                         "testSequenceResponse");
59         boolean success = false;
60
61         Element JavaDoc resultElement;
62
63         if (zero != null) {
64             // Check for correct ordering
65
int i = 1;
66             Node JavaDoc sib = zero.getNextSibling();
67             for (i = 1; i < names.length; i++) {
68                 while ((sib != null) && !(sib instanceof Element JavaDoc))
69                     sib = sib.getNextSibling();
70
71                 if ((sib == null) ||
72                         !(names[i].equals(((Element JavaDoc)sib).getLocalName())))
73                     break;
74
75                 sib = sib.getNextSibling();
76             }
77             if (i == names.length)
78                 success = true;
79         }
80
81         resultElement = retDoc.createElementNS(null,"return");
82
83         String JavaDoc resultStr = "false";
84         if (success) {
85             resultStr = "true";
86         }
87         Text JavaDoc text = retDoc.createTextNode(resultStr);
88         resultElement.appendChild(text);
89
90         ret[0].appendChild(resultElement);
91         return ret;
92     }
93
94     /**
95      * Walk an XML tree, looking for a &lt;zero&gt; element
96      * @param start the Element to walk down from
97      * @return an Element named &lt;zero&gt; or null
98      */

99     private Element JavaDoc findTheZero(Element JavaDoc start) {
100         if (names[0].equals(start.getLocalName())) {
101             return start;
102         }
103         NodeList JavaDoc nl = start.getChildNodes();
104         for (int i = 0; i < nl.getLength(); i++) {
105             Node JavaDoc node = nl.item(i);
106             if (node instanceof Element JavaDoc) {
107                 Element JavaDoc result = findTheZero((Element JavaDoc)node);
108                 if (result != null) {
109                     return result;
110                 }
111             }
112         }
113         return null;
114     }
115 }
116
Popular Tags