changeset 52721:732bec44c89e jdk-12+22

8213117: adoptNode corrupts attribute values Reviewed-by: lancea
author joehw
date Wed, 28 Nov 2018 10:30:15 -0800
parents fab77e2d8146
children 19de50eb561d
files src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/CoreDocumentImpl.java test/jaxp/javax/xml/jaxp/unittest/dom/DocumentTest.java
diffstat 2 files changed, 88 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/CoreDocumentImpl.java	Wed Nov 28 07:51:15 2018 -0800
+++ b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/CoreDocumentImpl.java	Wed Nov 28 10:30:15 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -83,7 +83,7 @@
  * @author Andy Clark, IBM
  * @author Ralf Pfeiffer, IBM
  * @since  PR-DOM-Level-1-19980818.
- * @LastModified: Nov 2017
+ * @LastModified: Nov 2018
  */
 public class CoreDocumentImpl
         extends ParentNode implements Document {
@@ -1797,6 +1797,11 @@
                     return null;
                 }
             }
+            // Adopting from a deferred DOM into another deferred DOM
+            else if (otherImpl instanceof DeferredDOMImplementationImpl) {
+                // traverse the DOM and expand deferred nodes and then allow adoption
+                undeferChildren (node);
+            }
         }
 
         switch (node.getNodeType()) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jaxp/javax/xml/jaxp/unittest/dom/DocumentTest.java	Wed Nov 28 10:30:15 2018 -0800
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package dom;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import org.testng.Assert;
+import org.testng.annotations.Listeners;
+import org.testng.annotations.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+/*
+ * @test
+ * @bug 8213117
+ * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
+ * @run testng dom.DocumentTest
+ * @summary Tests functionalities for Document.
+ */
+@Listeners({jaxp.library.BasePolicy.class})
+public class DocumentTest {
+
+    private static final String XML1 = "<root><oldNode oldAttrib1=\"old value 1\" oldAttrib2=\"old value 2\"></oldNode></root>";
+    private static final String XML2 = "<root><newNode newAttrib=\"new value\"></newNode></root>";
+
+    /**
+     * Verifies the adoptNode method. Before a node from a deferred DOM can be
+     * adopted, it needs to be fully expanded.
+     */
+    @Test
+    public void testAdoptNode() throws Exception {
+
+        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
+                .newDocumentBuilder();
+
+        Document doc1 = getDocument(builder, XML1);
+        Document doc2 = getDocument(builder, XML2);
+
+        Element newNode = (Element) doc2.getFirstChild().getFirstChild();
+        Element replacementNode = (Element) doc1.adoptNode(newNode);
+
+        Node oldNode = doc1.getFirstChild().getFirstChild();
+        doc1.getDocumentElement().replaceChild(replacementNode, oldNode);
+
+        String attrValue = doc1.getFirstChild().getFirstChild().getAttributes()
+                .getNamedItem("newAttrib").getNodeValue();
+        Assert.assertEquals(attrValue, "new value");
+    }
+
+    private static Document getDocument(DocumentBuilder builder, String xml) throws SAXException, IOException {
+        InputStream a = new ByteArrayInputStream(xml.getBytes());
+        Document out = builder.parse(a);
+        return out;
+    }
+
+}