Skip to content

Commit c588dad

Browse files
Lauretttaadamretter
authored andcommitted
[bugfix] base-uri() was returning empty string instead of the document path
The xml:base lookup in getBaseURI() was returning empty string when the attribute didn't exist. Removed it so we fall through to getDocumentURI() which gives the correct result. Closes #186
1 parent 825e1c4 commit c588dad

4 files changed

Lines changed: 64 additions & 16 deletions

File tree

exist-core/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@
803803
<include>src/test/resources/log4j2.xml</include>
804804
<include>src/test/resources/standalone-webapp/WEB-INF/web.xml</include>
805805
<include>src/main/xjb/rest-api.xjb</include>
806+
<include>src/test/xquery/base-uri.xql</include>
806807
<include>src/test/xquery/parenthesizedLocationStep.xml</include>
807808
<include>src/test/xquery/tail-recursion.xml</include>
808809
<include>src/test/xquery/maps/maps.xqm</include>
@@ -1510,6 +1511,7 @@
15101511
<exclude>src/test/resources/log4j2.xml</exclude>
15111512
<exclude>src/test/resources/standalone-webapp/WEB-INF/web.xml</exclude>
15121513
<exclude>src/main/xjb/rest-api.xjb</exclude>
1514+
<exclude>src/test/xquery/base-uri.xql</exclude>
15131515
<exclude>src/test/xquery/binary-value.xqm</exclude>
15141516
<exclude>src/test/xquery/instance-of.xqm</exclude>
15151517
<exclude>src/test/xquery/operator-mapping.xqm</exclude>

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,26 +1658,18 @@ public XQueryContext getContext() {
16581658

16591659
@Override
16601660
public String getBaseURI() {
1661-
final Element el = getDocumentElement();
1662-
if(el != null) {
1663-
final String baseURI = getDocumentElement().getAttributeNS(Namespaces.XML_NS, "base");
1664-
if(baseURI != null) {
1665-
return baseURI;
1666-
}
1667-
}
16681661
final String docURI = getDocumentURI();
16691662
if(docURI != null) {
16701663
return docURI;
1671-
} else {
1672-
if(context!=null && context.isBaseURIDeclared()) {
1673-
try {
1674-
return context.getBaseURI().getStringValue();
1675-
} catch(final XPathException e) {
1676-
//TODO : make something !
1677-
}
1664+
}
1665+
if(context != null && context.isBaseURIDeclared()) {
1666+
try {
1667+
return context.getBaseURI().getStringValue();
1668+
} catch(final XPathException e) {
1669+
//TODO : make something !
16781670
}
1679-
return XmldbURI.EMPTY_URI.toString();
16801671
}
1672+
return XmldbURI.EMPTY_URI.toString();
16811673
}
16821674

16831675
@Override

exist-core/src/test/java/org/exist/dom/memtree/DocumentImplTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,36 @@ public void testGetInScopePrefix() throws IOException, ParserConfigurationExcept
206206
assertEquals(Namespaces.XHTML_NS, namespaceUri);
207207
}
208208

209+
@Test
210+
public void getBaseURI_withDocumentURI() throws IOException, SAXException, ParserConfigurationException {
211+
final DocumentImpl doc;
212+
try(final InputStream is = new UnsynchronizedByteArrayInputStream("<root/>".getBytes(UTF_8))) {
213+
doc = parseExist(is);
214+
}
215+
doc.setDocumentURI("file:///intranet/xsl/template.xsl");
216+
assertEquals("file:///intranet/xsl/template.xsl", doc.getBaseURI());
217+
}
218+
219+
@Test
220+
public void getBaseURI_prefersXmlBaseOnRoot() throws IOException, SAXException, ParserConfigurationException {
221+
final DocumentImpl doc;
222+
try(final InputStream is = new UnsynchronizedByteArrayInputStream(
223+
"<root xml:base=\"http://example.org/\"/>".getBytes(UTF_8))) {
224+
doc = parseExist(is);
225+
}
226+
doc.setDocumentURI("file:///intranet/xsl/template.xsl");
227+
assertEquals("http://example.org/", doc.getBaseURI());
228+
}
229+
230+
@Test
231+
public void getBaseURI_noDocumentURI() throws IOException, SAXException, ParserConfigurationException {
232+
final DocumentImpl doc;
233+
try(final InputStream is = new UnsynchronizedByteArrayInputStream("<root/>".getBytes(UTF_8))) {
234+
doc = parseExist(is);
235+
}
236+
assertNull("", doc.getBaseURI());
237+
}
238+
209239
private Document parseXerces(final InputStream is) throws ParserConfigurationException, SAXException, IOException {
210240
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
211241
factory.setNamespaceAware(true);

exist-core/src/test/xquery/base-uri.xql

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+
: 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
:
@@ -100,7 +124,7 @@ function baseuri:root() {
100124
};
101125

102126
declare
103-
%test:assertEquals("http://example.org/a/","http://example.org/b/","http://example.org/c/","/yy","zz")
127+
%test:assertEquals("http://example.org/a/","http://example.org/b/","http://example.org/c/","/yy")
104128
function baseuri:sub1() {
105129
for $sub in $baseuri:DOCUMENT//sub
106130
return base-uri($sub)

0 commit comments

Comments
 (0)