1 16 package org.apache.axis2.om; 17 18 import junit.framework.TestCase; 19 import org.apache.axis2.om.impl.llom.factory.OMXMLBuilderFactory; 20 import org.apache.axis2.soap.SOAPEnvelope; 21 import org.w3c.dom.*; 22 23 import javax.xml.stream.XMLInputFactory; 24 import javax.xml.stream.XMLStreamReader; 25 import java.io.File ; 26 import java.io.FileReader ; 27 import java.util.Iterator ; 28 29 public class OMTestUtils { 30 public static OMXMLParserWrapper getOMBuilder(File file) throws Exception { 31 XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(new FileReader (file)); 32 OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXSOAPModelBuilder(OMAbstractFactory.getSOAP11Factory(), parser); 33 return builder; 34 } 35 36 public static void walkThrough(OMElement omEle) { 37 Iterator attibIt = omEle.getAttributes(); 38 if (attibIt != null) { 39 while (attibIt.hasNext()) { 40 TestCase.assertNotNull("once the has next is not null, the " + 41 "element should not be null", attibIt.next()); 42 } 43 } 44 Iterator it = omEle.getChildren(); 45 if (it != null) { 46 while (it.hasNext()) { 47 OMNode ele = (OMNode) it.next(); 48 TestCase.assertNotNull("once the has next is not null, the " + 49 "element should not be null", ele); 50 if (ele instanceof OMElement) { 51 walkThrough((OMElement) ele); 52 } 53 } 54 } 55 } 56 57 public static void compare(Element ele, OMElement omele) throws Exception { 58 if (ele == null && omele == null) { 59 return; 60 } else if (ele != null && omele != null) { 61 TestCase.assertEquals("Element name not correct", ele.getLocalName(), omele.getLocalName()); 62 if (omele.getNamespace() != null) { 63 TestCase.assertEquals("Namespace URI not correct", ele.getNamespaceURI(), omele.getNamespace().getName()); 64 65 } 66 67 NamedNodeMap map = ele.getAttributes(); 69 Iterator attIterator = omele.getAttributes(); 70 OMAttribute omattribute; 71 Attr domAttribute; 72 String DOMAttrName; 73 while (attIterator != null && attIterator.hasNext() && map == null) { 74 omattribute = (OMAttribute) attIterator.next(); 75 Node node = map.getNamedItemNS(omattribute.getNamespace().getName(), omattribute.getLocalName()); 76 if (node.getNodeType() == Node.ATTRIBUTE_NODE) { 77 Attr attr = (Attr) node; 78 TestCase.assertEquals(attr.getValue(), omattribute.getValue()); 79 } else { 80 throw new OMException("return type is not a Attribute"); 81 } 82 83 } 84 Iterator it = omele.getChildren(); 85 NodeList list = ele.getChildNodes(); 86 for (int i = 0; i < list.getLength(); i++) { 87 Node node = list.item(i); 88 if (node.getNodeType() == Node.ELEMENT_NODE) { 89 TestCase.assertTrue(it.hasNext()); 90 OMNode tempOmNode = (OMNode) it.next(); 91 while (tempOmNode.getType() != OMNode.ELEMENT_NODE) { 92 TestCase.assertTrue(it.hasNext()); 93 tempOmNode = (OMNode) it.next(); 94 } 95 compare((Element) node, (OMElement) tempOmNode); 96 } 97 } 98 99 100 } else { 101 throw new Exception ("One is null"); 102 } 103 } 104 105 public static SOAPEnvelope createOM(File file) throws Exception { 106 return (SOAPEnvelope) getOMBuilder(file).getDocumentElement(); 107 } 108 109 } 110 | Popular Tags |