Skip to content

Commit 9a313b7

Browse files
committed
[bugfix] base-uri() was returning empty string instead of the document path
Closes #186 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.
1 parent 825e1c4 commit 9a313b7

2 files changed

Lines changed: 37 additions & 15 deletions

File tree

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:///C:/intranet/xsl/template.xsl");
216+
assertEquals("file:///C:/intranet/xsl/template.xsl", doc.getBaseURI());
217+
}
218+
219+
@Test
220+
public void getBaseURI_ignoresXmlBaseOnRoot() 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:///C:/intranet/xsl/template.xsl");
227+
assertEquals("file:///C:/intranet/xsl/template.xsl", 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+
assertEquals("", 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);

0 commit comments

Comments
 (0)