Skip to content

Commit 7033b8c

Browse files
author
joehw
committed
8213117: adoptNode corrupts attribute values
Reviewed-by: lancea
1 parent 4a8e5bd commit 7033b8c

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/CoreDocumentImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -83,7 +83,7 @@
8383
* @author Andy Clark, IBM
8484
* @author Ralf Pfeiffer, IBM
8585
* @since PR-DOM-Level-1-19980818.
86-
* @LastModified: Nov 2017
86+
* @LastModified: Nov 2018
8787
*/
8888
public class CoreDocumentImpl
8989
extends ParentNode implements Document {
@@ -1797,6 +1797,11 @@ public Node adoptNode(Node source) {
17971797
return null;
17981798
}
17991799
}
1800+
// Adopting from a deferred DOM into another deferred DOM
1801+
else if (otherImpl instanceof DeferredDOMImplementationImpl) {
1802+
// traverse the DOM and expand deferred nodes and then allow adoption
1803+
undeferChildren (node);
1804+
}
18001805
}
18011806

18021807
switch (node.getNodeType()) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package dom;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import javax.xml.parsers.DocumentBuilder;
29+
import javax.xml.parsers.DocumentBuilderFactory;
30+
import org.testng.Assert;
31+
import org.testng.annotations.Listeners;
32+
import org.testng.annotations.Test;
33+
import org.w3c.dom.Document;
34+
import org.w3c.dom.Element;
35+
import org.w3c.dom.Node;
36+
import org.xml.sax.SAXException;
37+
38+
/*
39+
* @test
40+
* @bug 8213117
41+
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
42+
* @run testng dom.DocumentTest
43+
* @summary Tests functionalities for Document.
44+
*/
45+
@Listeners({jaxp.library.BasePolicy.class})
46+
public class DocumentTest {
47+
48+
private static final String XML1 = "<root><oldNode oldAttrib1=\"old value 1\" oldAttrib2=\"old value 2\"></oldNode></root>";
49+
private static final String XML2 = "<root><newNode newAttrib=\"new value\"></newNode></root>";
50+
51+
/**
52+
* Verifies the adoptNode method. Before a node from a deferred DOM can be
53+
* adopted, it needs to be fully expanded.
54+
*/
55+
@Test
56+
public void testAdoptNode() throws Exception {
57+
58+
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
59+
.newDocumentBuilder();
60+
61+
Document doc1 = getDocument(builder, XML1);
62+
Document doc2 = getDocument(builder, XML2);
63+
64+
Element newNode = (Element) doc2.getFirstChild().getFirstChild();
65+
Element replacementNode = (Element) doc1.adoptNode(newNode);
66+
67+
Node oldNode = doc1.getFirstChild().getFirstChild();
68+
doc1.getDocumentElement().replaceChild(replacementNode, oldNode);
69+
70+
String attrValue = doc1.getFirstChild().getFirstChild().getAttributes()
71+
.getNamedItem("newAttrib").getNodeValue();
72+
Assert.assertEquals(attrValue, "new value");
73+
}
74+
75+
private static Document getDocument(DocumentBuilder builder, String xml) throws SAXException, IOException {
76+
InputStream a = new ByteArrayInputStream(xml.getBytes());
77+
Document out = builder.parse(a);
78+
return out;
79+
}
80+
81+
}

0 commit comments

Comments
 (0)