Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4427165

Document.importNode() fails import subtree from parsed document

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      1.1.1
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Backports

        Description



        Name: eaR10174 Date: 03/19/2001


           Method org.w3c.dom.Document.importNode(Node importedNode, boolean deep)
        throws exception while recursive importing the element along with its children
        from document which is created by DocumentBuilder.parse() method (See test1.java
        below). Moreover, it throws a DOMException with the WRONG_DOCUMENT_ERR code, but
        such an error code isn't allowed by spec:

           Definition of the method in p. 1.2 "Fundamental Interfaces" of DOM Level 2 Core
        (http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#i-Document)
        reads:
          
           "Exceptions
               DOMException NOT_SUPPORTED_ERR: Raised if the type of node being imported
        is not supported."
           
           This bug appears in the build jaxp-1.1.1-b23-16_mar_2001 and affects the new
        test in TCK JAXP 1.1

           api/org_w3c_dom/Document/ImportNodeTests.html#importNodeTest009

           If the method is invoked with deep=false in the test1.java sample the method
        executes without exceptions.
           The bug does not appear if the subtree is imported from the document which is
        created by DocumentBuilder.newDocument() (See test2.java below).
           
        ------------------------------------test1.java-----------------------------
        import org.w3c.dom.Document;
        import org.w3c.dom.Element;
        import org.w3c.dom.Node;
        import javax.xml.parsers.DocumentBuilderFactory;
        import javax.xml.parsers.DocumentBuilder;
        import java.io.ByteArrayInputStream;

        public class test1 {
            
            public static void main(String argv[]) {
                try {
                    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance()
                                                     .newDocumentBuilder();
                                                     
                    String data =
                    "<?xml version=\"1.0\" ?>" +
                    "<root>" +
                    " <child>" +
                    " <child1/>" +
                    " </child>" +
                    "</root>";
                    
                    ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
                    Document doc1 = docBuilder.parse(in);
                    
                    Document doc2 = docBuilder.newDocument();
                    Node impElmt = doc1.getElementsByTagName("child").item(0);
                    
                    System.out.println("Node name: " + impElmt.getNodeName());
                    doc2.importNode(impElmt, true);
                    System.out.println("OK");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        ---------------------------------------------------------------------------
        ---------------------------------------------------------------------------
        JAXP build version: jaxp-1.1.1-b23-16_mar_2001

        % java -showversion -cp .:./jaxp.jar:./crimson.jar test1
        java version "1.3.0"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
        Java HotSpot(TM) Client VM (build 1.3.0-C, interpreted mode)

        Node name: child
        org.apache.crimson.tree.DomEx: WRONG_DOCUMENT_ERR: That node doesn't belong in
        this document.
                at org.apache.crimson.tree.ParentNode.checkDocument(ParentNode.java:250)
                at org.apache.crimson.tree.ParentNode.appendChild(ParentNode.java:333)
                at
        org.apache.crimson.tree.ElementNode2.createCopyForImportNode(ElementNode2.java:165
        )
                at org.apache.crimson.tree.XmlDocument.importNode(XmlDocument.java:1257)
                at test.main(test.java:26)
        ---------------------------------------------------------------------------
        JAXP build version: jaxp-1.1.1-b23-15_mar_2001

        % java -showversion -cp .:./jaxp.jar:./crimson.jar test1
        java version "1.3.0"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
        Java HotSpot(TM) Client VM (build 1.3.0-C, interpreted mode)

        Node name: child
        OK
        ---------------------------------------------------------------------------
        ------------------------------------test2.java-----------------------------
        import org.w3c.dom.Document;
        import org.w3c.dom.Element;
        import org.w3c.dom.Node;
        import javax.xml.parsers.DocumentBuilderFactory;
        import javax.xml.parsers.DocumentBuilder;

        public class test2 {
            
            public static void main(String argv[]) {
                try {
                    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance()
                                                     .newDocumentBuilder();
                                                     
                    Document doc1 = docBuilder.newDocument();
                    Document doc2 = docBuilder.newDocument();
                                       
                    Element root = doc1.createElement("root");
                    Element child = doc1.createElement("child");
                    Element child1 = doc1.createElement("child1");
                    child.appendChild(child1);
                    root.appendChild(child);
                    doc1.appendChild(root);
                    
                    Node impElmt = doc1.getElementsByTagName("child").item(0);
                    
                    System.out.println("Node name: " + impElmt.getNodeName());
                    
                    doc2.importNode(impElmt, true);
                    
                    System.out.println("OK");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        ---------------------------------------------------------------------------
        ---------------------------------------------------------------------------
        JAXP build version: jaxp-1.1.1-b23-16_mar_2001

        % java -showversion -cp .:./jaxp.jar:./crimson.jar test2
        java version "1.3.0"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
        Java HotSpot(TM) Client VM (build 1.3.0-C, interpreted mode)

        Node name: child
        OK
        ---------------------------------------------------------------------------

        ======================================================================

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                egoei Edwin Goei (Inactive)
                Reporter:
                evgsunw Evg Evg (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: