You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by di...@apache.org on 2004/11/29 04:40:18 UTC

cvs commit: ws-axis/java/test/saaj TestText.java

dims        2004/11/28 19:40:18

  Modified:    java/src/org/apache/axis/message SOAPDocumentImpl.java
               java/test/saaj TestText.java
  Log:
  - adding traverse dom test case
  - fixed getChildNodes in SOAPDocumentImpl
  
  Revision  Changes    Path
  1.11      +12 -1     ws-axis/java/src/org/apache/axis/message/SOAPDocumentImpl.java
  
  Index: SOAPDocumentImpl.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/message/SOAPDocumentImpl.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- SOAPDocumentImpl.java	8 Aug 2004 17:03:00 -0000	1.10
  +++ SOAPDocumentImpl.java	29 Nov 2004 03:40:18 -0000	1.11
  @@ -518,7 +518,18 @@
       }
   
       public NodeList getChildNodes() {
  -        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "");
  +        try {
  +            if (soapPart != null) {
  +                NodeListImpl children = new NodeListImpl();
  +                children.addNode(soapPart.getEnvelope());
  +                return children;
  +            } else {
  +                return NodeListImpl.EMPTY_NODELIST;
  +            }
  +        } catch (SOAPException se) {
  +            throw new DOMException(DOMException.INVALID_STATE_ERR, "");
  +        }
  +
       }
   
       /**
  
  
  
  1.2       +86 -0     ws-axis/java/test/saaj/TestText.java
  
  Index: TestText.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/test/saaj/TestText.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestText.java	7 Nov 2004 11:26:37 -0000	1.1
  +++ TestText.java	29 Nov 2004 03:40:18 -0000	1.2
  @@ -1,5 +1,12 @@
   package test.saaj;
   
  +import org.w3c.dom.Document;
  +import org.w3c.dom.Node;
  +import org.w3c.dom.NamedNodeMap;
  +import org.w3c.dom.Attr;
  +import org.w3c.dom.Text;
  +import org.w3c.dom.NodeList;
  +
   import javax.xml.soap.MessageFactory;
   import javax.xml.soap.Name;
   import javax.xml.soap.SOAPBody;
  @@ -8,6 +15,11 @@
   import javax.xml.soap.SOAPMessage;
   import javax.xml.soap.SOAPFactory;
   import javax.xml.soap.SOAPBodyElement;
  +import javax.xml.soap.SOAPEnvelope;
  +import javax.xml.soap.SOAPPart;
  +import javax.xml.parsers.DocumentBuilderFactory;
  +import javax.xml.parsers.DocumentBuilder;
  +import java.io.ByteArrayInputStream;
   
   public class TestText extends junit.framework.TestCase {
   
  @@ -122,7 +134,81 @@
           System.out.println("The message is lll:\n");
           message.writeTo(System.out);
       }
  +    
  +    public void testTraverseDOM() throws Exception {
  +        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
  +                "<book year=\"1992\">\n" +
  +                "  <title>Advanced Programming in the Unix environment</title>\n" +
  +                "  <author><last>Stevens</last><first>W.</first></author>\n" +
  +                "  <publisher>Addison-Wesley</publisher>\n" +
  +                "  <price>65.95</price>\n" +
  +                "</book>\n" +
  +                "";
  +
  +        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  +        factory.setNamespaceAware(true);
  +        DocumentBuilder builder = factory.newDocumentBuilder();
  +        Document payload = builder.parse(new ByteArrayInputStream(xml.getBytes()));
  +
  +        MessageFactory soapMsgFactory = MessageFactory.newInstance();
  +        SOAPMessage soapMsg = soapMsgFactory.createMessage();
  +
  +        SOAPPart soapPart = soapMsg.getSOAPPart();
  +        SOAPEnvelope soapEnv = soapPart.getEnvelope();
  +        SOAPBody soapBody = soapEnv.getBody();
  +
  +        soapBody.addDocument(payload);
  +
  +        System.out.println("***************");
  +        soapMsg.writeTo(System.out);
  +
  +        processNode(soapPart);
  +    }
  +
  +    private void processNode(Node currentNode) {
  +        switch (currentNode.getNodeType()) {
  +            // process a Document node
  +            case Node.DOCUMENT_NODE:
  +                Document doc = (Document) currentNode;
  +                System.out.println("Document node: " + doc.getNodeName() +
  +                        "\nRoot element: " +
  +                        doc.getDocumentElement().getNodeName());
  +                processChildNodes(doc.getChildNodes());
  +                break;
  +
  +                // process an Element node
  +            case Node.ELEMENT_NODE:
  +                System.out.println("\nElement node: " +
  +                        currentNode.getNodeName());
  +                NamedNodeMap attributeNodes =
  +                        currentNode.getAttributes();
  +                for (int i = 0; i < attributeNodes.getLength(); i++) {
  +                    Attr attribute = (Attr) attributeNodes.item(i);
  +                    System.out.println("\tAttribute: " +
  +                            attribute.getNodeName() + " ; Value = " +
  +                            attribute.getNodeValue());
  +                }
  +                processChildNodes(currentNode.getChildNodes());
  +                break;
  +
  +                // process a text node and a CDATA section
  +            case Node.CDATA_SECTION_NODE:
  +            case Node.TEXT_NODE:
  +                Text text = (Text) currentNode;
  +                if (!text.getNodeValue().trim().equals(""))
  +                    System.out.println("\tText: " +
  +                            text.getNodeValue());
  +                break;
  +        }
  +    }
  +
  +    private void processChildNodes(NodeList children) {
  +        if (children.getLength() != 0)
  +            for (int i = 0; i < children.getLength(); i++)
  +                processNode(children.item(i));
  +    }
   
  +    
       public static void main(String[] args) throws Exception {
           TestText tester = new TestText("TestEnvelope");
           tester.testAddTextNode();