| 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; |
| 21 |
import org.w3c.dom.Element; |
| 22 |
import org.w3c.dom.Node; |
| 23 |
import org.w3c.dom.NodeList; |
| 24 |
import org.w3c.dom.Text; |
| 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 ([email protected]) |
| 32 |
*/ |
| 33 |
public class SequenceService { |
| 34 |
private String [] names = new String [] { "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 [] testSequence(Element [] elems) throws Exception { |
| 48 |
Element 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 retDoc = XMLUtils.newDocument(); |
| 56 |
Element [] ret = new Element [1]; |
| 57 |
ret[0] = retDoc.createElementNS("urn:SequenceTest", |
| 58 |
"testSequenceResponse"); |
| 59 |
boolean success = false; |
| 60 |
|
| 61 |
Element resultElement; |
| 62 |
|
| 63 |
if (zero != null) { |
| 64 |
// Check for correct ordering |
| 65 |
int i = 1; |
| 66 |
Node sib = zero.getNextSibling(); |
| 67 |
for (i = 1; i < names.length; i++) { |
| 68 |
while ((sib != null) && !(sib instanceof Element)) |
| 69 |
sib = sib.getNextSibling(); |
| 70 |
|
| 71 |
if ((sib == null) || |
| 72 |
!(names[i].equals(((Element)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 resultStr = "false"; |
| 84 |
if (success) { |
| 85 |
resultStr = "true"; |
| 86 |
} |
| 87 |
Text 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 <zero> element |
| 96 |
* @param start the Element to walk down from |
| 97 |
* @return an Element named <zero> or null |
| 98 |
*/ |
| 99 |
private Element findTheZero(Element start) { |
| 100 |
if (names[0].equals(start.getLocalName())) { |
| 101 |
return start; |
| 102 |
} |
| 103 |
NodeList nl = start.getChildNodes(); |
| 104 |
for (int i = 0; i < nl.getLength(); i++) { |
| 105 |
Node node = nl.item(i); |
| 106 |
if (node instanceof Element) { |
| 107 |
Element result = findTheZero((Element)node); |
| 108 |
if (result != null) { |
| 109 |
return result; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
return null; |
| 114 |
} |
| 115 |
} |