Skip to content

Commit 8ca1971

Browse files
committed
[bugfix] Make sure the correct context sequence is used with util:eval-with-context
Closes #3039
1 parent e2bf08c commit 8ca1971

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

exist-core/src/main/java/org/exist/xquery/RootNode.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,30 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
9898
// get statically known documents from the context
9999
DocumentSet ds = context.getStaticallyKnownDocuments();
100100
if (ds == null || ds.getDocumentCount() == 0) {return Sequence.EMPTY_SEQUENCE;}
101-
101+
102+
// fix for util:eval-with-context
103+
if (contextSequence != null) {
104+
if (!contextSequence.isEmpty()) {
105+
final Item item = contextSequence.itemAt(0);
106+
// context item must be a node
107+
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
108+
throw new XPathException(this, ErrorCodes.XPTY0020, "Context item is not a node");
109+
}
110+
final NodeValue node = (NodeValue)item;
111+
// return fn:root(self::node()) treat as document-node()
112+
if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
113+
return new NodeProxy(((NodeProxy)item).getOwnerDocument());
114+
} else {
115+
if (node.getType() == Type.DOCUMENT) {
116+
return node;
117+
}
118+
return (org.exist.dom.memtree.DocumentImpl) node.getOwnerDocument();
119+
}
120+
} else {
121+
return Sequence.EMPTY_SEQUENCE;
122+
}
123+
}
124+
102125
// // if the expression occurs in a nested context, we might have cached the
103126
// // document set
104127
// // TODO: disabled cache for now as it may cause concurrency issues

exist-core/src/test/java/org/exist/storage/ShutdownTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void storeAndShutdown() throws EXistException, PermissionDeniedException,
9090

9191
final XQuery xquery = pool.getXQueryService();
9292
assertNotNull(xquery);
93-
final Sequence result = xquery.execute(broker, "//SPEECH[contains(LINE, 'love')]", Sequence.EMPTY_SEQUENCE);
93+
final Sequence result = xquery.execute(broker, "//SPEECH[contains(LINE, 'love')]", null);
9494
assertNotNull(result);
9595
assertEquals(187, result.getItemCount());
9696

0 commit comments

Comments
 (0)