Skip to content

Commit a205460

Browse files
committed
[bugfix] Retrieval of Nodes from Document was previously not returning all child nodes of the document node
Closes #4
1 parent 1eaa8c3 commit a205460

File tree

8 files changed

+131
-29
lines changed

8 files changed

+131
-29
lines changed

exist-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@
797797
<include>src/test/java/org/exist/dom/persistent/NodeTest.java</include>
798798
<include>src/test/java/org/exist/dom/persistent/PersistentDomTest.java</include>
799799
<include>src/main/java/org/exist/dom/persistent/ProcessingInstructionImpl.java</include>
800+
<include>src/main/java/org/exist/dom/persistent/SortedNodeSet.java</include>
800801
<include>src/main/java/org/exist/dom/persistent/StoredNode.java</include>
801802
<include>src/main/java/org/exist/dom/persistent/SymbolTable.java</include>
802803
<include>src/main/java/org/exist/dom/persistent/TextImpl.java</include>
@@ -900,6 +901,7 @@
900901
<include>src/main/java/org/exist/xmldb/RemoteRestoreService.java</include>
901902
<include>src/test/java/org/exist/xmldb/ResourceTest.java</include>
902903
<include>src/test/java/org/exist/xmldb/TestEXistXMLSerialize.java</include>
904+
<include>src/test/java/org/exist/xmldb/TreeLevelOrderTest.java</include>
903905
<include>src/test/java/org/exist/xmldb/concurrent/XMLGenerator.java</include>
904906
<include>src/main/java/org/exist/xmlrpc/ExistRpcTypeFactory.java</include>
905907
<include>src/main/java/org/exist/xmlrpc/RpcConnection.java</include>
@@ -917,6 +919,7 @@
917919
<include>src/main/java/org/exist/xquery/XPathUtil.java</include>
918920
<include>src/main/java/org/exist/xquery/XQueryContext.java</include>
919921
<include>src/test/java/org/exist/xquery/XQueryFunctionsTest.java</include>
922+
<include>src/test/java/org/exist/xquery/XQueryTest.java</include>
920923
<include>src/main/java/org/exist/xquery/functions/array/ArrayType.java</include>
921924
<include>src/test/java/org/exist/xquery/functions/fn/DocTest.java</include>
922925
<include>src/main/java/org/exist/xquery/functions/fn/FnModule.java</include>
@@ -1068,6 +1071,7 @@
10681071
<exclude>src/test/java/org/exist/dom/persistent/NodeTest.java</exclude>
10691072
<exclude>src/test/java/org/exist/dom/persistent/PersistentDomTest.java</exclude>
10701073
<exclude>src/main/java/org/exist/dom/persistent/ProcessingInstructionImpl.java</exclude>
1074+
<exclude>src/main/java/org/exist/dom/persistent/SortedNodeSet.java</exclude>
10711075
<exclude>src/main/java/org/exist/dom/persistent/StoredNode.java</exclude>
10721076
<exclude>src/main/java/org/exist/dom/persistent/SymbolTable.java</exclude>
10731077
<exclude>src/main/java/org/exist/dom/persistent/TextImpl.java</exclude>
@@ -1227,6 +1231,7 @@
12271231
<exclude>src/main/java/org/exist/xmldb/RemoteRestoreService.java</exclude>
12281232
<exclude>src/test/java/org/exist/xmldb/ResourceTest.java</exclude>
12291233
<exclude>src/test/java/org/exist/xmldb/TestEXistXMLSerialize.java</exclude>
1234+
<exclude>src/test/java/org/exist/xmldb/TreeLevelOrderTest.java</exclude>
12301235
<exclude>src/test/java/org/exist/xmldb/concurrent/XMLGenerator.java</exclude>
12311236
<exclude>src/main/java/org/exist/xmlrpc/ACEAiderParser.java</exclude>
12321237
<exclude>src/main/java/org/exist/xmlrpc/ACEAiderSerializer.java</exclude>
@@ -1257,6 +1262,7 @@
12571262
<exclude>src/main/java/org/exist/xquery/XQueryContext.java</exclude>
12581263
<exclude>src/test/java/org/exist/xquery/XQueryContextAttributesTest.java</exclude>
12591264
<exclude>src/test/java/org/exist/xquery/XQueryFunctionsTest.java</exclude>
1265+
<exclude>src/test/java/org/exist/xquery/XQueryTest.java</exclude>
12601266
<exclude>src/main/java/org/exist/xquery/functions/array/ArrayType.java</exclude>
12611267
<exclude>src/test/java/org/exist/xquery/functions/fn/DocTest.java</exclude>
12621268
<exclude>src/main/java/org/exist/xquery/functions/fn/FnModule.java</exclude>

exist-core/src/main/java/org/exist/dom/persistent/DocumentImpl.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -731,12 +731,14 @@ public void triggerDefrag() {
731731
}
732732

733733
/**
734-
* The method <code>getNode</code>
734+
* Retrieve a node from the document
735+
* by its nodeId
735736
*
736-
* @param nodeId a <code>NodeId</code> value
737-
* @return a <code>Node</code> value
737+
* @param nodeId the nodeId of the node to find.
738+
*
739+
* @return the node, or null if it does not exist.
738740
*/
739-
public Node getNode(final NodeId nodeId) {
741+
@Nullable Node getNode(final NodeId nodeId) {
740742
try(final DBBroker broker = pool.getBroker()) {
741743
return broker.objectWith(this, nodeId);
742744
} catch(final EXistException e) {
@@ -746,16 +748,21 @@ public Node getNode(final NodeId nodeId) {
746748
}
747749

748750
/**
749-
* The method <code>getNode</code>
751+
* Retrieve a node from the document
752+
* by its nodeProxy
750753
*
751-
* @param p a <code>NodeProxy</code> value
752-
* @return a <code>Node</code> value
754+
* This should only be called from {@link NodeProxy#getNode()}.
755+
*
756+
* @param p the proxy of the node to find.
757+
*
758+
* @return the node, or null if it does not exist.
753759
*/
754-
public Node getNode(final NodeProxy p) {
755-
if(p.getNodeId().getTreeLevel() == 1) {
756-
return getDocumentElement();
760+
Node getNode(final NodeProxy p) {
761+
if (p.getNodeId() == NodeId.DOCUMENT_NODE || p.getNodeId().equals(NodeId.DOCUMENT_NODE)) {
762+
return this;
757763
}
758-
try(final DBBroker broker = pool.getBroker()) {
764+
765+
try (final DBBroker broker = pool.getBroker()) {
759766
return broker.objectWith(p);
760767
} catch(final Exception e) {
761768
LOG.warn("Error occurred while retrieving node: {}", e.getMessage(), e);

exist-core/src/main/java/org/exist/dom/persistent/ElementImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ private Node getLastChild(final boolean attributesAreChildren) {
11081108
Node node = null;
11091109
if (!isDirty) {
11101110
final NodeId child = nodeId.getChild(children);
1111-
node = ownerDocument.getNode(new NodeProxy(getExpression(), ownerDocument, child));
1111+
node = new NodeProxy(getExpression(), ownerDocument, child).getNode();
11121112
}
11131113
if (node == null) {
11141114
final NodeList cl;

exist-core/src/main/java/org/exist/dom/persistent/SortedNodeSet.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -196,7 +220,7 @@ public long getItemCountLong() {
196220
@Override
197221
public Node item(final int pos) {
198222
final NodeProxy p = ((IteratorItem) list.get(pos)).proxy;
199-
return p == null ? null : p.getOwnerDocument().getNode(p);
223+
return p == null ? null : p.getNode();
200224
}
201225

202226
//TODO : evaluate both semantics (item/itemAt)

exist-core/src/main/java/org/exist/xmldb/LocalXMLResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public Node getContentAsDOM() throws XMLDBException {
254254
} else {
255255
result = read((document, broker, transaction) -> {
256256
if (proxy != null) {
257-
return document.getNode(proxy);
257+
return proxy.getNode();
258258
} else {
259259
// <[email protected]> return a full to get root PI and comments
260260
return document;

exist-core/src/test/java/org/exist/xmldb/TreeLevelOrderTest.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -25,13 +49,16 @@
2549
import org.exist.test.ExistXmldbEmbeddedServer;
2650
import org.junit.ClassRule;
2751
import org.junit.Test;
52+
import org.w3c.dom.Document;
53+
import org.w3c.dom.Element;
2854
import org.xmldb.api.base.*;
2955
import org.w3c.dom.Node;
3056
import org.w3c.dom.NodeList;
3157
import org.xmldb.api.modules.XMLResource;
3258
import org.xmldb.api.modules.XQueryService;
3359

3460
import static org.junit.Assert.assertNotNull;
61+
import static org.junit.Assert.assertTrue;
3562

3663
/**
3764
* Tests the TreeLevelOrder function.
@@ -75,16 +102,18 @@ public void treeLevelOrder() throws XMLDBException, IllegalAccessException, Inst
75102
store(DOC1, DOC1_NAME);
76103

77104
// read document back from database
78-
Node elem = load(DOC1_NAME);
79-
assertNotNull(elem);
105+
final Node doc = load(DOC1_NAME);
106+
assertNotNull(doc);
107+
assertTrue(doc instanceof Document);
80108

81109
//get node using DOM
82110
String strTo = null;
83111

84-
NodeList rootChildren = elem.getChildNodes();
85-
for (int r = 0; r < rootChildren.getLength(); r++) {
86-
if (rootChildren.item(r).getLocalName().equals("to")) {
87-
Node to = rootChildren.item(r);
112+
final Element elem = ((Document) doc).getDocumentElement();
113+
final NodeList elemChildNodes = elem.getChildNodes();
114+
for (int r = 0; r < elemChildNodes.getLength(); r++) {
115+
if (elemChildNodes.item(r).getLocalName().equals("to")) {
116+
final Node to = elemChildNodes.item(r);
88117
strTo = to.getTextContent();
89118
break;
90119
}

exist-core/src/test/java/org/exist/xquery/XQueryTest.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -23,7 +47,6 @@
2347

2448
import org.apache.logging.log4j.LogManager;
2549
import org.apache.logging.log4j.Logger;
26-
import org.custommonkey.xmlunit.DetailedDiff;
2750
import org.exist.EXistException;
2851
import org.exist.dom.QName;
2952
import org.exist.security.PermissionDeniedException;
@@ -40,6 +63,7 @@
4063
import org.exist.xquery.value.Sequence;
4164
import org.exist.xquery.value.Type;
4265
import org.junit.*;
66+
import org.w3c.dom.Document;
4367
import org.w3c.dom.Element;
4468
import org.w3c.dom.Node;
4569
import org.xml.sax.SAXException;
@@ -66,7 +90,6 @@
6690
import java.util.Arrays;
6791

6892
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
69-
import static org.custommonkey.xmlunit.XMLUnit.compareXML;
7093
import static org.junit.Assert.*;
7194
import static org.junit.Assume.assumeTrue;
7295

@@ -631,7 +654,9 @@ public void typedVariables() throws XMLDBException {
631654
assertEquals("XQuery: " + query, 1, result.getSize());
632655
//TODO : no way to test the node type ?
633656
//assertEquals( "XQuery: " + query, Node.DOCUMENT_NODE, ((XMLResource)result.getResource(0)));
634-
assertEquals("XQuery: " + query, "test", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
657+
final Node n = ((XMLResource) result.getResource(0)).getContentAsDOM();
658+
assertTrue(n instanceof Document);
659+
assertEquals("XQuery: " + query, "test", ((Document) n).getDocumentElement().getNodeName());
635660
}
636661

637662
@Test
@@ -1284,9 +1309,16 @@ public void functionDoc() throws XMLDBException, IOException, SAXException {
12841309
result = service.query(query);
12851310
assertEquals("XQuery: " + query, 1, result.getSize());
12861311

1287-
Node n = ((XMLResource) result.getResource(0)).getContentAsDOM();
1288-
DetailedDiff d = new DetailedDiff(compareXML(numbers, n.toString()));
1289-
assertEquals(0, d.getAllDifferences().size());
1312+
final Node n = ((XMLResource) result.getResource(0)).getContentAsDOM();
1313+
assertTrue(n instanceof Document);
1314+
final Source expected = Input.fromString(numbers).build();
1315+
final Source actual = Input.fromNode(n).build();
1316+
1317+
final Diff diff = DiffBuilder.compare(expected).withTest(actual)
1318+
.checkForSimilar()
1319+
.build();
1320+
assertFalse(diff.toString(), diff.hasDifferences());
1321+
12901322
//ignore eXist namespace's attributes
12911323
//assertEquals(1, d.getAllDifferences().size());
12921324

exist-core/src/test/java/org/exist/xquery/functions/fn/DocTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
import org.exist.xmldb.EXistResource;
6767
import org.exist.xmldb.LocalXMLResource;
68+
import org.w3c.dom.Document;
6869
import org.w3c.dom.Node;
6970

7071
import org.xml.sax.InputSource;
@@ -152,23 +153,26 @@ public void testURIResolveWithEval() throws XMLDBException {
152153
LocalXMLResource res = (LocalXMLResource)result.getResource(0);
153154
assertNotNull(res);
154155
Node n = res.getContentAsDOM();
155-
assertEquals("y", n.getLocalName());
156+
assertTrue(n instanceof Document);
157+
assertEquals("y", ((Document) n).getDocumentElement().getLocalName());
156158

157159
query = "util:eval(xs:anyURI('/db/test/test1.xq'), false(), ())";
158160
result = existEmbeddedServer.executeQuery(query);
159161

160162
res = (LocalXMLResource)result.getResource(0);
161163
assertNotNull(res);
162164
n = res.getContentAsDOM();
163-
assertEquals("x", n.getLocalName());
165+
assertTrue(n instanceof Document);
166+
assertEquals("x", ((Document) n).getDocumentElement().getLocalName());
164167

165168
query = "util:eval(xs:anyURI('/db/test/test2.xq'), false(), ())";
166169
result = existEmbeddedServer.executeQuery(query);
167170

168171
res = (LocalXMLResource)result.getResource(0);
169172
assertNotNull(res);
170173
n = res.getContentAsDOM();
171-
assertEquals("x", n.getLocalName());
174+
assertTrue(n instanceof Document);
175+
assertEquals("x", ((Document) n).getDocumentElement().getLocalName());
172176
}
173177

174178
@Test

0 commit comments

Comments
 (0)