Skip to content

Commit fd6c79e

Browse files
committed
[bugfix] Node#getBaseURI() and the XPath function fn:base-uri() now return a URI. Instead of '/db/a/b/c.xml' the result is now 'xmldb:exist:///db/a/b/c.xml'. This fixes a number of issues with relative XSLT stylesheet imports.
Closes #169
1 parent 07de7d6 commit fd6c79e

16 files changed

Lines changed: 231 additions & 30 deletions

File tree

exist-core/pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@
806806
<include>src/test/xquery/base-uri.xql</include>
807807
<include>src/test/xquery/parenthesizedLocationStep.xml</include>
808808
<include>src/test/xquery/tail-recursion.xml</include>
809+
<include>src/test/xquery/indexing/indexes-base-uri.xq</include>
809810
<include>src/test/xquery/maps/maps.xqm</include>
810811
<include>src/test/xquery/numbers/format-numbers.xql</include>
811812
<include>src/test/xquery/util/util.xml</include>
@@ -894,6 +895,7 @@
894895
<include>src/main/java/org/exist/dom/QName.java</include>
895896
<include>src/main/java/org/exist/dom/memtree/AbstractCharacterData.java</include>
896897
<include>src/main/java/org/exist/dom/memtree/AttrImpl.java</include>
898+
<include>src/main/java/org/exist/dom/memtree/CDATASectionImpl.java</include>
897899
<include>src/main/java/org/exist/dom/memtree/CommentImpl.java</include>
898900
<include>src/main/java/org/exist/dom/memtree/DocumentBuilderReceiver.java</include>
899901
<include>src/main/java/org/exist/dom/memtree/DocumentImpl.java</include>
@@ -913,6 +915,7 @@
913915
<include>src/test/java/org/exist/dom/persistent/BasicNodeSetTest.java</include>
914916
<include>src/main/java/org/exist/dom/persistent/BinaryDocument.java</include>
915917
<include>src/test/java/org/exist/dom/persistent/CDataIntergationTest.java</include>
918+
<include>src/main/java/org/exist/dom/persistent/CDATASectionImpl.java</include>
916919
<include>src/main/java/org/exist/dom/persistent/CommentImpl.java</include>
917920
<include>src/test/java/org/exist/dom/persistent/CommentTest.java</include>
918921
<include>src/test/java/org/exist/dom/persistent/DefaultDocumentSetTest.java</include>
@@ -1115,6 +1118,7 @@
11151118
<include>src/test/java/org/exist/util/CollationsTest.java</include>
11161119
<include>src/main/java/org/exist/util/Configuration.java</include>
11171120
<include>src/test/java/org/exist/util/DOMSerializerTest.java</include>
1121+
<include>src/main/java/org/exist/util/EXistURISchemeURIResolver.java</include>
11181122
<include>src/test/java/org/exist/util/LeasableTest.java</include>
11191123
<include>src/test/java/org/exist/util/MediaTypeResolverTest.java</include>
11201124
<include>src/main/java/org/exist/util/MimeTable.java</include>
@@ -1417,6 +1421,7 @@
14171421
<include>src/test/java/org/exist/xquery/functions/validate/JingSchematronTest.java</include>
14181422
<include>src/test/java/org/exist/xquery/functions/validate/JingXsdTest.java</include>
14191423
<include>src/main/java/org/exist/xquery/functions/validation/Jaxp.java</include>
1424+
<include>src/main/java/org/exist/xquery/functions/validation/Shared.java</include>
14201425
<include>src/test/java/org/exist/xquery/functions/xmldb/DbStore2Test.java</include>
14211426
<include>src/main/java/org/exist/xquery/functions/xmldb/FunXCollection.java</include>
14221427
<include>src/main/java/org/exist/xquery/functions/xmldb/XMLDBGetMimeType.java</include>
@@ -1522,6 +1527,7 @@
15221527
<exclude>src/test/xquery/pi.xqm</exclude>
15231528
<exclude>src/test/xquery/tail-recursion.xml</exclude>
15241529
<exclude>src/test/xquery/type-promotion.xqm</exclude>
1530+
<exclude>src/test/xquery/indexing/indexes-base-uri.xq</exclude>
15251531
<exclude>src/test/xquery/maps/maps.xqm</exclude>
15261532
<exclude>src/test/xquery/numbers/format-numbers.xql</exclude>
15271533
<exclude>src/test/xquery/securitymanager/acl.xqm</exclude>
@@ -1616,7 +1622,8 @@
16161622
<exclude>src/main/java/org/exist/dom/QName.java</exclude>
16171623
<exclude>src/main/java/org/exist/dom/memtree/AbstractCharacterData.java</exclude>
16181624
<exclude>src/main/java/org/exist/dom/memtree/AttrImpl.java</exclude>
1619-
<include>src/main/java/org/exist/dom/memtree/CommentImpl.java</include>
1625+
<exclude>src/main/java/org/exist/dom/memtree/CDATASectionImpl.java</exclude>
1626+
<exclude>src/main/java/org/exist/dom/memtree/CommentImpl.java</exclude>
16201627
<exclude>src/main/java/org/exist/dom/memtree/DocumentBuilderReceiver.java</exclude>
16211628
<exclude>src/main/java/org/exist/dom/memtree/DocumentImpl.java</exclude>
16221629
<exclude>src/test/java/org/exist/dom/memtree/DocumentImplTest.java</exclude>
@@ -1643,6 +1650,7 @@
16431650
<exclude>src/test/java/org/exist/dom/persistent/BasicNodeSetTest.java</exclude>
16441651
<exclude>src/main/java/org/exist/dom/persistent/BinaryDocument.java</exclude>
16451652
<exclude>src/test/java/org/exist/dom/persistent/CDataIntergationTest.java</exclude>
1653+
<exclude>src/main/java/org/exist/dom/persistent/CDATASectionImpl.java</exclude>
16461654
<exclude>src/main/java/org/exist/dom/persistent/CommentImpl.java</exclude>
16471655
<exclude>src/test/java/org/exist/dom/persistent/CommentTest.java</exclude>
16481656
<exclude>src/test/java/org/exist/dom/persistent/DefaultDocumentSetTest.java</exclude>
@@ -1913,6 +1921,7 @@
19131921
<exclude>src/test/java/org/exist/util/CollectionOfArrayIteratorTest.java</exclude>
19141922
<exclude>src/main/java/org/exist/util/Configuration.java</exclude>
19151923
<exclude>src/test/java/org/exist/util/DOMSerializerTest.java</exclude>
1924+
<exclude>src/main/java/org/exist/util/EXistURISchemeURIResolver.java</exclude>
19161925
<exclude>src/main/java/org/exist/util/IPUtil.java</exclude>
19171926
<exclude>src/main/java/org/exist/util/JREUtil.java</exclude>
19181927
<exclude>src/test/java/org/exist/util/LeasableTest.java</exclude>
@@ -2254,6 +2263,7 @@
22542263
<exclude>src/test/java/org/exist/xquery/functions/validate/JingSchematronTest.java</exclude>
22552264
<exclude>src/test/java/org/exist/xquery/functions/validate/JingXsdTest.java</exclude>
22562265
<exclude>src/main/java/org/exist/xquery/functions/validation/Jaxp.java</exclude>
2266+
<exclude>src/main/java/org/exist/xquery/functions/validation/Shared.java</exclude>
22572267
<exclude>src/test/java/org/exist/xquery/functions/xmldb/AbstractXMLDBTest.java</exclude>
22582268
<exclude>src/test/java/org/exist/xquery/functions/xmldb/DbStore2Test.java</exclude>
22592269
<exclude>src/main/java/org/exist/xquery/functions/xmldb/FunXCollection.java</exclude>
@@ -2848,4 +2858,4 @@ The BaseX Team. The original license statement is also included below.]]></pream
28482858
</profile>
28492859
</profiles>
28502860

2851-
</project>
2861+
</project>

exist-core/src/main/java/org/exist/dom/memtree/CDATASectionImpl.java

Lines changed: 36 additions & 0 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+
* admin@evolvedbinary.com
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,8 +49,11 @@
2549
import org.exist.xquery.value.Type;
2650
import org.w3c.dom.CDATASection;
2751
import org.w3c.dom.DOMException;
52+
import org.w3c.dom.Node;
2853
import org.w3c.dom.Text;
2954

55+
import javax.annotation.Nullable;
56+
3057

3158
/**
3259
* Represents a CDATA section.
@@ -48,6 +75,15 @@ public int getItemType() {
4875
return Type.CDATA_SECTION;
4976
}
5077

78+
@Override
79+
public @Nullable String getBaseURI() {
80+
@Nullable final Node parent = getParentNode();
81+
if (parent == null) {
82+
return null;
83+
}
84+
return parent.getBaseURI();
85+
}
86+
5187
@Override
5288
public Text splitText(final int offset) throws DOMException {
5389
return null;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.exist.util.XMLString;
5858
import org.exist.util.pool.NodePool;
5959
import org.exist.util.serializer.AttrList;
60+
import org.exist.xmldb.XmldbURI;
6061
import org.exist.xquery.Expression;
6162
import org.w3c.dom.*;
6263

@@ -425,7 +426,12 @@ public boolean isId() {
425426
public @Nullable String getBaseURI() {
426427
@Nullable final Element e = getOwnerElement();
427428
if (e != null) {
428-
return e.getBaseURI();
429+
@Nullable String strBaseUri = e.getBaseURI();
430+
if (strBaseUri != null && strBaseUri.startsWith("/db/")) {
431+
// Must be a URI!
432+
strBaseUri = XmldbURI.EMBEDDED_SERVER_URI_PREFIX + strBaseUri;
433+
}
434+
return strBaseUri;
429435
}
430436
return null;
431437
}

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

Lines changed: 42 additions & 0 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+
* admin@evolvedbinary.com
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
*
@@ -27,12 +51,15 @@
2751
import org.exist.util.ByteConversion;
2852
import org.exist.util.UTF8;
2953
import org.exist.util.XMLString;
54+
import org.exist.xmldb.XmldbURI;
3055
import org.exist.xquery.Expression;
3156
import org.w3c.dom.CDATASection;
3257
import org.w3c.dom.DOMException;
3358
import org.w3c.dom.Node;
3459
import org.w3c.dom.Text;
3560

61+
import javax.annotation.Nullable;
62+
3663
public class CDATASectionImpl extends AbstractCharacterData implements CDATASection {
3764

3865
public CDATASectionImpl() {
@@ -77,6 +104,20 @@ public CDATASectionImpl(final Expression expression, final String data) {
77104
}
78105

79106
@Override
107+
public @Nullable String getBaseURI() {
108+
@Nullable final Node parent = getParentNode();
109+
if (parent != null) {
110+
@Nullable String strBaseUri = parent.getBaseURI();
111+
if (strBaseUri != null && strBaseUri.startsWith("/db/")) {
112+
// Must be a URI!
113+
strBaseUri = XmldbURI.EMBEDDED_SERVER_URI_PREFIX + strBaseUri;
114+
}
115+
return strBaseUri;
116+
} else {
117+
return null;
118+
}
119+
}
120+
80121
/**
81122
* Serializes a (persistent DOM) CDATA Section to a byte array
82123
*
@@ -94,6 +135,7 @@ public CDATASectionImpl(final Expression expression, final String data) {
94135
* @return the returned byte array after use must be returned to the ByteArrayPool
95136
* by calling {@link ByteArrayPool#releaseByteArray(byte[])}
96137
*/
138+
@Override
97139
public byte[] serialize() {
98140
final int nodeIdLen = nodeId.size();
99141
final byte[] data = ByteArrayPool.getByteArray(LENGTH_SIGNATURE_LENGTH + NodeId.LENGTH_NODE_ID_UNITS +

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.exist.storage.Signatures;
5050
import org.exist.util.ByteConversion;
5151
import org.exist.util.pool.NodePool;
52+
import org.exist.xmldb.XmldbURI;
5253
import org.exist.xquery.Expression;
5354
import org.w3c.dom.Comment;
5455
import org.w3c.dom.Node;
@@ -92,7 +93,12 @@ public String toString() {
9293
public @Nullable String getBaseURI() {
9394
@Nullable final Node parent = getParentNode();
9495
if (parent != null) {
95-
return parent.getBaseURI();
96+
@Nullable String strBaseUri = parent.getBaseURI();
97+
if (strBaseUri != null && strBaseUri.startsWith("/db/")) {
98+
// Must be a URI!
99+
strBaseUri = XmldbURI.EMBEDDED_SERVER_URI_PREFIX + strBaseUri;
100+
}
101+
return strBaseUri;
96102
} else {
97103
return null;
98104
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ public Node renameNode(final Node n, final String namespaceURI, final String qua
16811681

16821682
@Override
16831683
public String getBaseURI() {
1684-
return getURI().toString();
1684+
return XmldbURI.EMBEDDED_SERVER_URI_PREFIX + getURI().getCollectionPath();
16851685
}
16861686

16871687
@Override

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,12 @@ public void setIdAttributeNode(final Attr idAttr, final boolean isId) throws DOM
20032003
public @Nullable String getBaseURI() {
20042004
@Nullable final XmldbURI baseURI = calculateBaseURI();
20052005
if (baseURI != null) {
2006-
return baseURI.toString();
2006+
String strBaseUri = baseURI.toString();
2007+
if (strBaseUri.startsWith("/db/")) {
2008+
// Must be a URI!
2009+
strBaseUri = XmldbURI.EMBEDDED_SERVER_URI_PREFIX + baseURI;
2010+
}
2011+
return strBaseUri;
20072012
}
20082013

20092014
return null;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.exist.storage.Signatures;
5151
import org.exist.util.ByteConversion;
5252
import org.exist.util.pool.NodePool;
53+
import org.exist.xmldb.XmldbURI;
5354
import org.exist.xquery.Expression;
5455
import org.w3c.dom.DOMException;
5556
import org.w3c.dom.Node;
@@ -154,7 +155,12 @@ public void setData(final String data) {
154155
public @Nullable String getBaseURI() {
155156
@Nullable final StoredNode parent = getParentStoredNode();
156157
if (parent != null) {
157-
return parent.getBaseURI();
158+
@Nullable String strBaseUri = parent.getBaseURI();
159+
if (strBaseUri != null && strBaseUri.startsWith("/db/")) {
160+
// Must be a URI!
161+
strBaseUri = XmldbURI.EMBEDDED_SERVER_URI_PREFIX + strBaseUri;
162+
}
163+
return strBaseUri;
158164
} else {
159165
return getOwnerDocument().getBaseURI();
160166
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.exist.util.ByteConversion;
5353
import org.exist.util.UTF8;
5454
import org.exist.util.pool.NodePool;
55+
import org.exist.xmldb.XmldbURI;
5556
import org.exist.xquery.Expression;
5657
import org.w3c.dom.DOMException;
5758
import org.w3c.dom.Node;
@@ -180,7 +181,12 @@ public Text splitText(final int offset) throws DOMException {
180181
public @Nullable String getBaseURI() {
181182
@Nullable final Node parent = getParentNode();
182183
if (parent != null) {
183-
return parent.getBaseURI();
184+
@Nullable String strBaseUri = parent.getBaseURI();
185+
if (strBaseUri != null && strBaseUri.startsWith("/db/")) {
186+
// Must be a URI!
187+
strBaseUri = XmldbURI.EMBEDDED_SERVER_URI_PREFIX + strBaseUri;
188+
}
189+
return strBaseUri;
184190
} else {
185191
return null;
186192
}

exist-core/src/main/java/org/exist/util/EXistURISchemeURIResolver.java

Lines changed: 26 additions & 0 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+
* admin@evolvedbinary.com
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
*
@@ -56,6 +80,8 @@ private String rewriteScheme(String uri) {
5680
uri = uri.replace("exist://localhost/db", "/db");
5781
} else if (uri.startsWith("exist://")) {
5882
uri = uri.replace("exist://", "xmldb:exist://");
83+
} else if (uri.startsWith("exist:/")) {
84+
uri = uri.replace("exist:/", "xmldb:exist:///");
5985
}
6086
}
6187

0 commit comments

Comments
 (0)