Skip to content

Commit 2ebb899

Browse files
Victor RudometovPaul Hohensee
authored andcommitted
8287076: Document.normalizeDocument() produces different results
Reviewed-by: phh Backport-of: 1f9521e6cb2f701f8712b4ec941ff1dbb45dad4e
1 parent 378b516 commit 2ebb899

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -39,7 +39,7 @@
3939
*
4040
* @xerces.internal
4141
*
42-
* @LastModified: Oct 2017
42+
* @LastModified: June 2022
4343
*/
4444
public class AttributeMap extends NamedNodeMapImpl {
4545

@@ -117,7 +117,7 @@ public Node setNamedItem(Node arg)
117117
} else {
118118
i = -1 - i; // Insert point (may be end of list)
119119
if (null == nodes) {
120-
nodes = new ArrayList<>(5);
120+
nodes = new ArrayList<>();
121121
}
122122
nodes.add(i, arg);
123123
}
@@ -193,7 +193,7 @@ public Node setNamedItemNS(Node arg)
193193
} else {
194194
i = -1 - i; // Insert point (may be end of list)
195195
if (null == nodes) {
196-
nodes = new ArrayList<>(5);
196+
nodes = new ArrayList<>();
197197
}
198198
nodes.add(i, arg);
199199
}
@@ -591,7 +591,7 @@ protected final int addItem (Node arg) {
591591
else {
592592
i = -1 - i; // Insert point (may be end of list)
593593
if (null == nodes) {
594-
nodes = new ArrayList<>(5);
594+
nodes = new ArrayList<>();
595595
}
596596
nodes.add(i, arg);
597597
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -89,7 +89,7 @@
8989
*
9090
* @author Elena Litani, IBM
9191
* @author Neeraj Bajaj, Sun Microsystems, inc.
92-
* @LastModified: Apr 2019
92+
* @LastModified: June 2022
9393
*/
9494
public class DOMNormalizer implements XMLDocumentHandler {
9595

@@ -140,9 +140,6 @@ public class DOMNormalizer implements XMLDocumentHandler {
140140
/** Stores all namespace bindings on the current element */
141141
protected final NamespaceContext fLocalNSBinder = new NamespaceSupport();
142142

143-
/** list of attributes */
144-
protected final List<Node> fAttributeList = new ArrayList<>(5);
145-
146143
/** DOM Locator - for namespace fixup algorithm */
147144
protected final DOMLocatorImpl fLocator = new DOMLocatorImpl();
148145

@@ -885,9 +882,9 @@ protected final void namespaceFixUp (ElementImpl element, AttributeMap attribute
885882
if (attributes != null) {
886883

887884
// clone content of the attributes
888-
attributes.cloneMap(fAttributeList);
889-
for (int i = 0; i < fAttributeList.size(); i++) {
890-
Attr attr = (Attr) fAttributeList.get(i);
885+
List<Node> attrList = attributes.cloneMap(new ArrayList<>());
886+
for (int i = 0; i < attrList.size(); i++) {
887+
Attr attr = (Attr) attrList.get(i);
891888
fLocator.fRelatedNode = attr;
892889

893890
if (DEBUG) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -56,7 +56,7 @@
5656
* @xerces.internal
5757
*
5858
* @since PR-DOM-Level-1-19980818.
59-
* @LastModified: Jan 2018
59+
* @LastModified: June 2022
6060
*/
6161
public class NamedNodeMapImpl
6262
implements NamedNodeMap, Serializable {
@@ -196,7 +196,7 @@ public Node setNamedItem(Node arg)
196196
} else {
197197
i = -1 - i; // Insert point (may be end of list)
198198
if (null == nodes) {
199-
nodes = new ArrayList<>(5);
199+
nodes = new ArrayList<>();
200200
}
201201
nodes.add(i, arg);
202202
}
@@ -246,7 +246,7 @@ public Node setNamedItemNS(Node arg)
246246
} else {
247247
i = -1 - i; // Insert point (may be end of list)
248248
if (null == nodes) {
249-
nodes = new ArrayList<>(5);
249+
nodes = new ArrayList<>();
250250
}
251251
nodes.add(i, arg);
252252
}
@@ -561,7 +561,7 @@ protected int addItem (Node arg) {
561561
else {
562562
i = -1 - i; // Insert point (may be end of list)
563563
if (null == nodes) {
564-
nodes = new ArrayList<>(5);
564+
nodes = new ArrayList<>();
565565
}
566566
nodes.add(i, arg);
567567
}

test/jaxp/javax/xml/jaxp/unittest/dom/DocumentTest.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,15 +34,20 @@
3434
import org.testng.Assert;
3535
import org.testng.annotations.Listeners;
3636
import org.testng.annotations.Test;
37+
import org.w3c.dom.Attr;
3738
import org.w3c.dom.Document;
3839
import org.w3c.dom.Element;
3940
import org.w3c.dom.Node;
41+
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
4042
import org.w3c.dom.events.Event;
4143
import org.w3c.dom.events.EventListener;
44+
import org.w3c.dom.ls.DOMImplementationLS;
45+
import org.w3c.dom.ls.LSInput;
46+
import org.w3c.dom.ls.LSParser;
4247

4348
/*
4449
* @test
45-
* @bug 8213117 8222743
50+
* @bug 8213117 8222743 8287076
4651
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
4752
* @modules java.xml
4853
* @modules java.xml/com.sun.org.apache.xerces.internal.dom
@@ -56,6 +61,53 @@ public class DocumentTest {
5661
static final int DOC1 = 1;
5762
static final int DOC2 = 2;
5863

64+
/*
65+
* @bug 8287076
66+
* Verifies that Document::normalizeDocument returns the same result as that
67+
* prior to JDK 10 (JDK-8181150).
68+
* Attribute Name:
69+
* JDK 9: NS1:wsu and NS2:wsu2
70+
* After the JDK 10 change: wsu and wsu2
71+
*/
72+
@Test
73+
public void testNormalizeDocument() throws Exception {
74+
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
75+
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
76+
final LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
77+
final LSInput input = impl.createLSInput();
78+
input.setStringData("<xml/>");
79+
final Document document = builder.parse(input);
80+
final Element root = document.getDocumentElement();
81+
82+
// Generate a single element
83+
final Element element = document.createElement("token");
84+
final Attr attr = element.getOwnerDocument().createAttributeNS("http://blah.xsd", "wsu");
85+
attr.setValue("Id");
86+
element.setAttributeNodeNS(attr);
87+
88+
final Attr attr2 = element.getOwnerDocument().createAttributeNS("http://blah2.xsd", "wsu2");
89+
element.setAttributeNodeNS(attr2);
90+
91+
final Attr attr3 = element.getOwnerDocument().createAttribute("aa");
92+
element.setAttributeNodeNS(attr3);
93+
94+
final Attr attr4 = element.getOwnerDocument().createAttribute("zz");
95+
element.setAttributeNodeNS(attr4);
96+
97+
final Attr attr5 = element.getOwnerDocument().createAttribute("tt");
98+
element.setAttributeNodeNS(attr5);
99+
100+
root.appendChild(element);
101+
102+
document.normalizeDocument();
103+
104+
Node wsu = element.getAttributes().getNamedItemNS("http://blah.xsd", "wsu");
105+
Node wsu2 = element.getAttributes().getNamedItemNS("http://blah2.xsd", "wsu2");
106+
107+
Assert.assertEquals(wsu.getNodeName(), "NS1:wsu");
108+
Assert.assertEquals(wsu2.getNodeName(), "NS2:wsu2");
109+
}
110+
59111
/**
60112
* Verifies the adoptNode method. Before a node from a deferred DOM can be
61113
* adopted, it needs to be fully expanded.

0 commit comments

Comments
 (0)