Skip to content

Commit 3be238a

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

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
@@ -850,6 +850,7 @@
850850
<include>src/test/java/org/exist/dom/persistent/NodeTest.java</include>
851851
<include>src/test/java/org/exist/dom/persistent/PersistentDomTest.java</include>
852852
<include>src/main/java/org/exist/dom/persistent/ProcessingInstructionImpl.java</include>
853+
<include>src/main/java/org/exist/dom/persistent/SortedNodeSet.java</include>
853854
<include>src/main/java/org/exist/dom/persistent/StoredNode.java</include>
854855
<include>src/main/java/org/exist/dom/persistent/SymbolTable.java</include>
855856
<include>src/main/java/org/exist/dom/persistent/TextImpl.java</include>
@@ -954,6 +955,7 @@
954955
<include>src/main/java/org/exist/xmldb/RemoteRestoreService.java</include>
955956
<include>src/test/java/org/exist/xmldb/ResourceTest.java</include>
956957
<include>src/test/java/org/exist/xmldb/TestEXistXMLSerialize.java</include>
958+
<include>src/test/java/org/exist/xmldb/TreeLevelOrderTest.java</include>
957959
<include>src/test/java/org/exist/xmldb/concurrent/XMLGenerator.java</include>
958960
<include>src/main/java/org/exist/xmlrpc/ExistRpcTypeFactory.java</include>
959961
<include>src/main/java/org/exist/xmlrpc/RpcConnection.java</include>
@@ -971,6 +973,7 @@
971973
<include>src/main/java/org/exist/xquery/XPathUtil.java</include>
972974
<include>src/main/java/org/exist/xquery/XQueryContext.java</include>
973975
<include>src/test/java/org/exist/xquery/XQueryFunctionsTest.java</include>
976+
<include>src/test/java/org/exist/xquery/XQueryTest.java</include>
974977
<include>src/main/java/org/exist/xquery/functions/array/ArrayType.java</include>
975978
<include>src/test/java/org/exist/xquery/functions/fn/DocTest.java</include>
976979
<include>src/main/java/org/exist/xquery/functions/fn/FnModule.java</include>
@@ -1134,6 +1137,7 @@
11341137
<exclude>src/test/java/org/exist/dom/persistent/NodeTest.java</exclude>
11351138
<exclude>src/test/java/org/exist/dom/persistent/PersistentDomTest.java</exclude>
11361139
<exclude>src/main/java/org/exist/dom/persistent/ProcessingInstructionImpl.java</exclude>
1140+
<exclude>src/main/java/org/exist/dom/persistent/SortedNodeSet.java</exclude>
11371141
<exclude>src/main/java/org/exist/dom/persistent/StoredNode.java</exclude>
11381142
<exclude>src/main/java/org/exist/dom/persistent/SymbolTable.java</exclude>
11391143
<exclude>src/main/java/org/exist/dom/persistent/TextImpl.java</exclude>
@@ -1299,6 +1303,7 @@
12991303
<exclude>src/main/java/org/exist/xmldb/RemoteRestoreService.java</exclude>
13001304
<exclude>src/test/java/org/exist/xmldb/ResourceTest.java</exclude>
13011305
<exclude>src/test/java/org/exist/xmldb/TestEXistXMLSerialize.java</exclude>
1306+
<exclude>src/test/java/org/exist/xmldb/TreeLevelOrderTest.java</exclude>
13021307
<exclude>src/test/java/org/exist/xmldb/concurrent/XMLGenerator.java</exclude>
13031308
<exclude>src/main/java/org/exist/xmlrpc/ACEAiderParser.java</exclude>
13041309
<exclude>src/main/java/org/exist/xmlrpc/ACEAiderSerializer.java</exclude>
@@ -1330,6 +1335,7 @@
13301335
<exclude>src/main/java/org/exist/xquery/XQueryContext.java</exclude>
13311336
<exclude>src/test/java/org/exist/xquery/XQueryContextAttributesTest.java</exclude>
13321337
<exclude>src/test/java/org/exist/xquery/XQueryFunctionsTest.java</exclude>
1338+
<exclude>src/test/java/org/exist/xquery/XQueryTest.java</exclude>
13331339
<exclude>src/main/java/org/exist/xquery/functions/array/ArrayType.java</exclude>
13341340
<exclude>src/test/java/org/exist/xquery/functions/fn/DocTest.java</exclude>
13351341
<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
@@ -793,12 +793,14 @@ public void triggerDefrag() {
793793
}
794794

795795
/**
796-
* The method <code>getNode</code>
796+
* Retrieve a node from the document
797+
* by its nodeId
797798
*
798-
* @param nodeId a <code>NodeId</code> value
799-
* @return a <code>Node</code> value
799+
* @param nodeId the nodeId of the node to find.
800+
*
801+
* @return the node, or null if it does not exist.
800802
*/
801-
public Node getNode(final NodeId nodeId) {
803+
@Nullable Node getNode(final NodeId nodeId) {
802804
try(final DBBroker broker = pool.getBroker()) {
803805
return broker.objectWith(this, nodeId);
804806
} catch(final EXistException e) {
@@ -808,16 +810,21 @@ public Node getNode(final NodeId nodeId) {
808810
}
809811

810812
/**
811-
* The method <code>getNode</code>
813+
* Retrieve a node from the document
814+
* by its nodeProxy
812815
*
813-
* @param p a <code>NodeProxy</code> value
814-
* @return a <code>Node</code> value
816+
* This should only be called from {@link NodeProxy#getNode()}.
817+
*
818+
* @param p the proxy of the node to find.
819+
*
820+
* @return the node, or null if it does not exist.
815821
*/
816-
public Node getNode(final NodeProxy p) {
817-
if(p.getNodeId().getTreeLevel() == 1) {
818-
return getDocumentElement();
822+
Node getNode(final NodeProxy p) {
823+
if (p.getNodeId() == NodeId.DOCUMENT_NODE || p.getNodeId().equals(NodeId.DOCUMENT_NODE)) {
824+
return this;
819825
}
820-
try(final DBBroker broker = pool.getBroker()) {
826+
827+
try (final DBBroker broker = pool.getBroker()) {
821828
return broker.objectWith(p);
822829
} catch(final Exception e) {
823830
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
@@ -327,7 +327,7 @@ public Node getContentAsDOM() throws XMLDBException {
327327
} else {
328328
result = read((document, broker, transaction) -> {
329329
if (proxy != null) {
330-
return document.getNode(proxy);
330+
return proxy.getNode();
331331
} else {
332332
// <[email protected]> return a full to get root PI and comments
333333
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;
@@ -63,7 +87,6 @@
6387
import java.util.Arrays;
6488

6589
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
66-
import static org.custommonkey.xmlunit.XMLUnit.compareXML;
6790
import static org.junit.Assert.*;
6891
import static org.junit.Assume.assumeTrue;
6992

@@ -620,7 +643,9 @@ public void typedVariables() throws XMLDBException {
620643
assertEquals("XQuery: " + query, 1, result.getSize());
621644
//TODO : no way to test the node type ?
622645
//assertEquals( "XQuery: " + query, Node.DOCUMENT_NODE, ((XMLResource)result.getResource(0)));
623-
assertEquals("XQuery: " + query, "test", ((XMLResource) result.getResource(0)).getContentAsDOM().getNodeName());
646+
final Node n = ((XMLResource) result.getResource(0)).getContentAsDOM();
647+
assertTrue(n instanceof Document);
648+
assertEquals("XQuery: " + query, "test", ((Document) n).getDocumentElement().getNodeName());
624649
}
625650

626651
@Test
@@ -1270,9 +1295,16 @@ public void functionDoc() throws XMLDBException, IOException, SAXException {
12701295
result = service.query(query);
12711296
assertEquals("XQuery: " + query, 1, result.getSize());
12721297

1273-
Node n = ((XMLResource) result.getResource(0)).getContentAsDOM();
1274-
DetailedDiff d = new DetailedDiff(compareXML(numbers, n.toString()));
1275-
assertEquals(0, d.getAllDifferences().size());
1298+
final Node n = ((XMLResource) result.getResource(0)).getContentAsDOM();
1299+
assertTrue(n instanceof Document);
1300+
final Source expected = Input.fromString(numbers).build();
1301+
final Source actual = Input.fromNode(n).build();
1302+
1303+
final Diff diff = DiffBuilder.compare(expected).withTest(actual)
1304+
.checkForSimilar()
1305+
.build();
1306+
assertFalse(diff.toString(), diff.hasDifferences());
1307+
12761308
//ignore eXist namespace's attributes
12771309
//assertEquals(1, d.getAllDifferences().size());
12781310

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)