Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
8287076: Document.normalizeDocument() produces different results
Backport-of: 1f9521e6cb2f701f8712b4ec941ff1dbb45dad4e
  • Loading branch information
Yuri Nesterenko committed Dec 9, 2022
1 parent 1f5bae3 commit 057c00e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 20 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
Expand Down Expand Up @@ -39,7 +39,7 @@
*
* @xerces.internal
*
* @LastModified: Oct 2017
* @LastModified: June 2022
*/
public class AttributeMap extends NamedNodeMapImpl {

Expand Down Expand Up @@ -117,7 +117,7 @@ public Node setNamedItem(Node arg)
} else {
i = -1 - i; // Insert point (may be end of list)
if (null == nodes) {
nodes = new ArrayList<>(5);
nodes = new ArrayList<>();
}
nodes.add(i, arg);
}
Expand Down Expand Up @@ -193,7 +193,7 @@ public Node setNamedItemNS(Node arg)
} else {
i = -1 - i; // Insert point (may be end of list)
if (null == nodes) {
nodes = new ArrayList<>(5);
nodes = new ArrayList<>();
}
nodes.add(i, arg);
}
Expand Down Expand Up @@ -591,7 +591,7 @@ protected final int addItem (Node arg) {
else {
i = -1 - i; // Insert point (may be end of list)
if (null == nodes) {
nodes = new ArrayList<>(5);
nodes = new ArrayList<>();
}
nodes.add(i, arg);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
Expand Down Expand Up @@ -89,7 +89,7 @@
*
* @author Elena Litani, IBM
* @author Neeraj Bajaj, Sun Microsystems, inc.
* @LastModified: Apr 2019
* @LastModified: June 2022
*/
public class DOMNormalizer implements XMLDocumentHandler {

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

/** list of attributes */
protected final List<Node> fAttributeList = new ArrayList<>(5);

/** DOM Locator - for namespace fixup algorithm */
protected final DOMLocatorImpl fLocator = new DOMLocatorImpl();

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

// clone content of the attributes
attributes.cloneMap(fAttributeList);
for (int i = 0; i < fAttributeList.size(); i++) {
Attr attr = (Attr) fAttributeList.get(i);
List<Node> attrList = attributes.cloneMap(new ArrayList<>());
for (int i = 0; i < attrList.size(); i++) {
Attr attr = (Attr) attrList.get(i);
fLocator.fRelatedNode = attr;

if (DEBUG) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
Expand Down Expand Up @@ -56,7 +56,7 @@
* @xerces.internal
*
* @since PR-DOM-Level-1-19980818.
* @LastModified: Jan 2018
* @LastModified: June 2022
*/
public class NamedNodeMapImpl
implements NamedNodeMap, Serializable {
Expand Down Expand Up @@ -196,7 +196,7 @@ public Node setNamedItem(Node arg)
} else {
i = -1 - i; // Insert point (may be end of list)
if (null == nodes) {
nodes = new ArrayList<>(5);
nodes = new ArrayList<>();
}
nodes.add(i, arg);
}
Expand Down Expand Up @@ -246,7 +246,7 @@ public Node setNamedItemNS(Node arg)
} else {
i = -1 - i; // Insert point (may be end of list)
if (null == nodes) {
nodes = new ArrayList<>(5);
nodes = new ArrayList<>();
}
nodes.add(i, arg);
}
Expand Down Expand Up @@ -561,7 +561,7 @@ protected int addItem (Node arg) {
else {
i = -1 - i; // Insert point (may be end of list)
if (null == nodes) {
nodes = new ArrayList<>(5);
nodes = new ArrayList<>();
}
nodes.add(i, arg);
}
Expand Down
56 changes: 54 additions & 2 deletions test/jaxp/javax/xml/jaxp/unittest/dom/DocumentTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, 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
Expand Down Expand Up @@ -34,15 +34,20 @@
import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSParser;

/*
* @test
* @bug 8213117 8222743
* @bug 8213117 8222743 8287076
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @modules java.xml
* @modules java.xml/com.sun.org.apache.xerces.internal.dom
Expand All @@ -56,6 +61,53 @@ public class DocumentTest {
static final int DOC1 = 1;
static final int DOC2 = 2;

/*
* @bug 8287076
* Verifies that Document::normalizeDocument returns the same result as that
* prior to JDK 10 (JDK-8181150).
* Attribute Name:
* JDK 9: NS1:wsu and NS2:wsu2
* After the JDK 10 change: wsu and wsu2
*/
@Test
public void testNormalizeDocument() throws Exception {
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
final LSInput input = impl.createLSInput();
input.setStringData("<xml/>");
final Document document = builder.parse(input);
final Element root = document.getDocumentElement();

// Generate a single element
final Element element = document.createElement("token");
final Attr attr = element.getOwnerDocument().createAttributeNS("http://blah.xsd", "wsu");
attr.setValue("Id");
element.setAttributeNodeNS(attr);

final Attr attr2 = element.getOwnerDocument().createAttributeNS("http://blah2.xsd", "wsu2");
element.setAttributeNodeNS(attr2);

final Attr attr3 = element.getOwnerDocument().createAttribute("aa");
element.setAttributeNodeNS(attr3);

final Attr attr4 = element.getOwnerDocument().createAttribute("zz");
element.setAttributeNodeNS(attr4);

final Attr attr5 = element.getOwnerDocument().createAttribute("tt");
element.setAttributeNodeNS(attr5);

root.appendChild(element);

document.normalizeDocument();

Node wsu = element.getAttributes().getNamedItemNS("http://blah.xsd", "wsu");
Node wsu2 = element.getAttributes().getNamedItemNS("http://blah2.xsd", "wsu2");

Assert.assertEquals(wsu.getNodeName(), "NS1:wsu");
Assert.assertEquals(wsu2.getNodeName(), "NS2:wsu2");
}

/**
* Verifies the adoptNode method. Before a node from a deferred DOM can be
* adopted, it needs to be fully expanded.
Expand Down

1 comment on commit 057c00e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.