You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by di...@apache.org on 2004/08/08 19:03:01 UTC

cvs commit: ws-axis/java/test/saaj TestImport.java PackageTests.java

dims        2004/08/08 10:03:01

  Modified:    java/src/org/apache/axis/message SOAPDocumentImpl.java
                        SOAPEnvelope.java
               java/test/saaj PackageTests.java
  Added:       java/test/saaj TestImport.java
  Log:
  Fix for AXIS-1484 - SAAJ/DOM importNode problem
  from Jongjin Choi
  
  Revision  Changes    Path
  1.10      +106 -3    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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SOAPDocumentImpl.java	27 Jul 2004 11:01:07 -0000	1.9
  +++ SOAPDocumentImpl.java	8 Aug 2004 17:03:00 -0000	1.10
  @@ -16,9 +16,11 @@
   
   package org.apache.axis.message;
   
  +import javax.xml.namespace.QName;
   import org.apache.axis.AxisFault;
   import org.apache.axis.Constants;
   import org.apache.axis.SOAPPart;
  +import org.apache.axis.utils.Mapping;
   import org.apache.axis.utils.XMLUtils;
   import org.w3c.dom.Attr;
   import org.w3c.dom.CDATASection;
  @@ -234,9 +236,110 @@
           "createEntityReference");
       }
   
  -    public Node importNode(Node importedNode, boolean deep)
  -    throws DOMException {
  -        throw new java.lang.UnsupportedOperationException("importNode");
  +    // implemented by yoonforh 2004-07-30 02:48:50
  +    public Node importNode(Node importedNode, boolean deep) throws DOMException {
  +    	Node targetNode = null;
  +    
  +    	int type = importedNode.getNodeType();
  +    	switch (type) {
  +    	case ELEMENT_NODE :
  +    	    Element el = (Element) importedNode;
  +    	    if (deep) {
  +        		targetNode = new SOAPBodyElement(el);
  +        		break;
  +    	    }
  +    
  +    	    SOAPBodyElement target = new SOAPBodyElement();
  +    	    org.w3c.dom.NamedNodeMap attrs = el.getAttributes();
  +    	    for (int i = 0; i < attrs.getLength(); i++) {
  +        		org.w3c.dom.Node att = attrs.item(i);
  +        		if (att.getNamespaceURI() != null &&
  +        		    att.getPrefix() != null &&
  +        		    att.getNamespaceURI().equals(Constants.NS_URI_XMLNS) &&
  +        		    att.getPrefix().equals("xmlns")) {
  +        		    Mapping map = new Mapping(att.getNodeValue(), att.getLocalName());
  +        		    target.addMapping(map);
  +        		}
  +        		if (att.getLocalName() != null) {
  +        		    target.addAttribute(att.getPrefix(),
  +        					att.getNamespaceURI(),
  +        					att.getLocalName(),
  +        					att.getNodeValue());
  +        		} else if (att.getNodeName() != null) {
  +        		    target.addAttribute(att.getPrefix(),
  +        					att.getNamespaceURI(),
  +        					att.getNodeName(),
  +        					att.getNodeValue());
  +        		}
  +    	    }
  +    
  +    	    if (el.getLocalName() == null) {
  +    	        target.setName(el.getNodeName());
  +    	    } else {
  +    	        target.setQName(new QName(el.getNamespaceURI(), el.getLocalName()));
  +    	    }
  +    	    targetNode = target;
  +    	    break;
  +    
  +    	case ATTRIBUTE_NODE :
  +    	    if (importedNode.getLocalName() == null) {
  +    	        targetNode = createAttribute(importedNode.getNodeName());
  +    	    } else {
  +    	        targetNode = createAttributeNS(importedNode.getNamespaceURI(),
  +    					       importedNode.getLocalName());
  +    	    }
  +    	    break;
  +    
  +    	case TEXT_NODE :
  +    	    targetNode = createTextNode(importedNode.getNodeValue());
  +    	    break;
  +    
  +    	case CDATA_SECTION_NODE :
  +    	    targetNode = createCDATASection(importedNode.getNodeValue());
  +    	    break;
  +    
  +    	case COMMENT_NODE :
  +    	    targetNode = createComment(importedNode.getNodeValue());
  +    	    break;
  +    
  +    	case DOCUMENT_FRAGMENT_NODE :
  +    	    targetNode = createDocumentFragment();
  +    	    if (deep) {
  +        		org.w3c.dom.NodeList children = importedNode.getChildNodes();
  +        		for (int i = 0; i < children.getLength(); i++){
  +        		    targetNode.appendChild(importNode(children.item(i), true));
  +        		}
  +    	    }
  +    	    break;
  +    
  +    	case ENTITY_REFERENCE_NODE :
  +    	    targetNode = createEntityReference(importedNode.getNodeName());
  +    	    break;
  +    
  +    	case PROCESSING_INSTRUCTION_NODE :
  +    	    ProcessingInstruction pi = (ProcessingInstruction) importedNode;
  +    	    targetNode = createProcessingInstruction(pi.getTarget(), pi.getData());
  +    	    break;
  +    
  +    	case ENTITY_NODE :
  +    	    // TODO : ...
  +    	    throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Entity nodes are not supported.");
  +    
  +    	case NOTATION_NODE :
  +    	    // TODO : any idea?
  +    	    throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Notation nodes are not supported.");
  +    
  +    	case DOCUMENT_TYPE_NODE :
  +    	    throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DocumentType nodes cannot be imported.");
  +    
  +    	case DOCUMENT_NODE :
  +    	    throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Document nodes cannot be imported.");
  +    
  +    	default :
  +    	    throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Node type (" + type + ") cannot be imported.");
  +    	}
  +    
  +    	return targetNode;
       }
   
       /**
  
  
  
  1.100     +14 -0     ws-axis/java/src/org/apache/axis/message/SOAPEnvelope.java
  
  Index: SOAPEnvelope.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/message/SOAPEnvelope.java,v
  retrieving revision 1.99
  retrieving revision 1.100
  diff -u -r1.99 -r1.100
  --- SOAPEnvelope.java	29 Jul 2004 21:40:27 -0000	1.99
  +++ SOAPEnvelope.java	8 Aug 2004 17:03:01 -0000	1.100
  @@ -31,6 +31,7 @@
   import org.apache.commons.logging.Log;
   import org.w3c.dom.DOMException;
   import org.w3c.dom.Node;
  +import org.w3c.dom.NodeList;
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
   
  @@ -40,6 +41,7 @@
   import java.util.ArrayList;
   import java.util.Enumeration;
   import java.util.Iterator;
  +import java.util.List;
   import java.util.Vector;
   
   /**
  @@ -622,9 +624,21 @@
           super.setOwnerDocument(sp);
           if(body != null) {
               body.setOwnerDocument(sp);
  +            setOwnerDocumentForChildren(((NodeImpl)body).getChildNodes(), sp);
           }
           if(header != null){
               header.setOwnerDocument(sp);
  +            setOwnerDocumentForChildren(((NodeImpl)body).getChildNodes(), sp);
           }
  +    }
  +    
  +    private void setOwnerDocumentForChildren(NodeList children, org.apache.axis.SOAPPart sp) {
  +    	if (children == null)
  +    		return;
  +        for (int i = 0; i < children.getLength(); i++) {
  +            NodeImpl node = (NodeImpl) children.item(i);
  +    		node.setOwnerDocument(sp);
  +            setOwnerDocumentForChildren(node.getChildNodes(), sp);  // recursively
  +    	}
       }
   }
  
  
  
  1.10      +1 -0      ws-axis/java/test/saaj/PackageTests.java
  
  Index: PackageTests.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/test/saaj/PackageTests.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- PackageTests.java	28 Jun 2004 12:52:24 -0000	1.9
  +++ PackageTests.java	8 Aug 2004 17:03:01 -0000	1.10
  @@ -19,6 +19,7 @@
             }
           }catch( Throwable t){;}
           suite.addTestSuite(test.saaj.TestEnvelope.class);
  +        suite.addTestSuite(test.saaj.TestImport.class);
           suite.addTestSuite(test.saaj.TestSOAPFaultDetail.class);
           suite.addTestSuite(test.saaj.TestHeaders.class);
           suite.addTestSuite(test.saaj.TestPrefixes.class);
  
  
  
  1.1                  ws-axis/java/test/saaj/TestImport.java
  
  Index: TestImport.java
  ===================================================================
  package test.saaj;
  
  import org.apache.axis.utils.XMLUtils;
  import org.w3c.dom.Document;
  import org.w3c.dom.Node;
  import test.AxisTestBase;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.soap.MessageFactory;
  import javax.xml.soap.MimeHeaders;
  import javax.xml.soap.SOAPBody;
  import javax.xml.soap.SOAPMessage;
  import javax.xml.soap.SOAPPart;
  import java.io.ByteArrayInputStream;
  
  public class TestImport extends AxisTestBase {
  
      public TestImport(String name) {
          super(name);
      }
  
      private static final String SAMPLE_1 =
              "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
              "\n" +
              "<SOAP-ENV:Body> " + "\n" +
              "<m:GetLastTradePrice xmlns:m=\"http://wombat.ztrade.com\">" +
              "\n" +
              "<symbol>SUNW</symbol> " + "\n" +
              "</m:GetLastTradePrice> " + "\n" +
              "</SOAP-ENV:Body> " + "\n" +
              "</SOAP-ENV:Envelope>";
  
      private SOAPMessage getSOAPMessageFromString(String str) throws Exception {
          MimeHeaders mimeHeaders = new MimeHeaders();
          mimeHeaders.addHeader("content-type", "text/xml");
          SOAPMessage message = MessageFactory.newInstance().createMessage(
                  mimeHeaders,
                  new ByteArrayInputStream(str.getBytes()));
          return message;
      }
  
      public void testImports() throws Exception {
          //DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          //DocumentBuilder db = dbf.newDocumentBuilder();
          //Document doc1 = db.parse(new ByteArrayInputStream(SAMPLE_1.getBytes()));
  
          Document doc2 = testImportFromSaajToDom();
          Document body = testImportFromDomToSaaj(doc2);
          XMLUtils.PrettyDocumentToStream(body, System.out);
          //assertXMLEqual(doc1, body);
          //assertXMLEqual(doc2, body);
          //assertXMLEqual(doc1, doc2);
      }
  
      private Document testImportFromSaajToDom() throws Exception {
          SOAPMessage message = getSOAPMessageFromString(SAMPLE_1);
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.newDocument();
          org.w3c.dom.Node fromNode = message.getSOAPBody().getFirstChild();
          Node n = doc.importNode(fromNode, true);
          doc.appendChild(n);
          return doc;
      }
  
      private Document testImportFromDomToSaaj(Document doc) throws Exception {
          SOAPMessage sm = MessageFactory.newInstance().createMessage();
          SOAPPart sp = sm.getSOAPPart();
          SOAPBody body = sm.getSOAPBody();
          org.w3c.dom.Node node = sp.importNode(doc.getDocumentElement(), true);
          body.appendChild(node);
          return sp;
      }
  }