Skip to content

Commit 9721b50

Browse files
authored
Merge pull request #204 from evolvedbinary/7.x.x/bugfix/base-uri-returns-db-instead-of-document-path
[7.x.x] Correct an issue with the Base URI of in-memory documents
2 parents 825e1c4 + 67f02a1 commit 9721b50

17 files changed

Lines changed: 228 additions & 96 deletions

File tree

exist-core/pom.xml

Lines changed: 6 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>
@@ -893,6 +894,7 @@
893894
<include>src/main/java/org/exist/dom/QName.java</include>
894895
<include>src/main/java/org/exist/dom/memtree/AbstractCharacterData.java</include>
895896
<include>src/main/java/org/exist/dom/memtree/AttrImpl.java</include>
897+
<include>src/main/java/org/exist/dom/memtree/CommentImpl.java</include>
896898
<include>src/main/java/org/exist/dom/memtree/DocumentBuilderReceiver.java</include>
897899
<include>src/main/java/org/exist/dom/memtree/DocumentImpl.java</include>
898900
<include>src/test/java/org/exist/dom/memtree/DocumentImplTest.java</include>
@@ -923,6 +925,7 @@
923925
<include>src/main/java/org/exist/dom/persistent/EmptyNodeSet.java</include>
924926
<include>src/main/java/org/exist/dom/persistent/LockToken.java</include>
925927
<include>src/main/java/org/exist/dom/persistent/NewArrayNodeSet.java</include>
928+
<include>src/main/java/org/exist/dom/persistent/NodeImpl.java</include>
926929
<include>src/main/java/org/exist/dom/persistent/NodeProxy.java</include>
927930
<include>src/main/java/org/exist/dom/persistent/NodeSet.java</include>
928931
<include>src/test/java/org/exist/dom/persistent/NodeTest.java</include>
@@ -1510,6 +1513,7 @@
15101513
<exclude>src/test/resources/log4j2.xml</exclude>
15111514
<exclude>src/test/resources/standalone-webapp/WEB-INF/web.xml</exclude>
15121515
<exclude>src/main/xjb/rest-api.xjb</exclude>
1516+
<exclude>src/test/xquery/base-uri.xql</exclude>
15131517
<exclude>src/test/xquery/binary-value.xqm</exclude>
15141518
<exclude>src/test/xquery/instance-of.xqm</exclude>
15151519
<exclude>src/test/xquery/operator-mapping.xqm</exclude>
@@ -1612,6 +1616,7 @@
16121616
<exclude>src/main/java/org/exist/dom/QName.java</exclude>
16131617
<exclude>src/main/java/org/exist/dom/memtree/AbstractCharacterData.java</exclude>
16141618
<exclude>src/main/java/org/exist/dom/memtree/AttrImpl.java</exclude>
1619+
<include>src/main/java/org/exist/dom/memtree/CommentImpl.java</include>
16151620
<exclude>src/main/java/org/exist/dom/memtree/DocumentBuilderReceiver.java</exclude>
16161621
<exclude>src/main/java/org/exist/dom/memtree/DocumentImpl.java</exclude>
16171622
<exclude>src/test/java/org/exist/dom/memtree/DocumentImplTest.java</exclude>
@@ -1650,6 +1655,7 @@
16501655
<exclude>src/main/java/org/exist/dom/persistent/EmptyNodeSet.java</exclude>
16511656
<exclude>src/main/java/org/exist/dom/persistent/LockToken.java</exclude>
16521657
<exclude>src/main/java/org/exist/dom/persistent/NewArrayNodeSet.java</exclude>
1658+
<exclude>src/main/java/org/exist/dom/persistent/NodeImpl.java</exclude>
16531659
<exclude>src/main/java/org/exist/dom/persistent/NodeProxy.java</exclude>
16541660
<exclude>src/main/java/org/exist/dom/persistent/NodeSet.java</exclude>
16551661
<exclude>src/test/java/org/exist/dom/persistent/NodeTest.java</exclude>

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import org.exist.xquery.value.Type;
5454
import org.w3c.dom.*;
5555

56+
import javax.annotation.Nullable;
57+
5658

5759
public class AttrImpl extends NodeImpl implements Attr {
5860

@@ -90,9 +92,9 @@ public int getType() {
9092
}
9193

9294
@Override
93-
public String getBaseURI() {
94-
final Node parent = document.getNode(document.attrParent[nodeNumber]);
95-
if(parent == null) {
95+
public @Nullable String getBaseURI() {
96+
@Nullable final Node parent = document.getNode(document.attrParent[nodeNumber]);
97+
if (parent == null) {
9698
return null;
9799
}
98100
return parent.getBaseURI();

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

Lines changed: 29 additions & 3 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
*
@@ -29,6 +53,8 @@
2953
import org.w3c.dom.Comment;
3054
import org.w3c.dom.Node;
3155

56+
import javax.annotation.Nullable;
57+
3258
public class CommentImpl extends AbstractCharacterData implements Comment {
3359

3460
public CommentImpl(final DocumentImpl doc, final int nodeNumber) {
@@ -53,9 +79,9 @@ public AtomicValue atomize() throws XPathException {
5379
}
5480

5581
@Override
56-
public String getBaseURI() {
57-
final Node parent = getParentNode();
58-
if(parent == null) {
82+
public @Nullable String getBaseURI() {
83+
@Nullable final Node parent = getParentNode();
84+
if (parent == null) {
5985
return null;
6086
}
6187
return parent.getBaseURI();

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,26 +1657,28 @@ public XQueryContext getContext() {
16571657
}
16581658

16591659
@Override
1660-
public String getBaseURI() {
1661-
final Element el = getDocumentElement();
1662-
if(el != null) {
1660+
public @Nullable String getBaseURI() {
1661+
@Nullable final Element el = getDocumentElement();
1662+
if (el != null) {
16631663
final String baseURI = getDocumentElement().getAttributeNS(Namespaces.XML_NS, "base");
1664-
if(baseURI != null) {
1664+
if (!baseURI.isEmpty()) {
16651665
return baseURI;
16661666
}
16671667
}
1668-
final String docURI = getDocumentURI();
1669-
if(docURI != null) {
1668+
1669+
@Nullable final String docURI = getDocumentURI();
1670+
if (docURI != null) {
16701671
return docURI;
16711672
} else {
1672-
if(context!=null && context.isBaseURIDeclared()) {
1673+
if (context != null && context.isBaseURIDeclared()) {
16731674
try {
16741675
return context.getBaseURI().getStringValue();
1675-
} catch(final XPathException e) {
1676-
//TODO : make something !
1676+
} catch (final XPathException e) {
1677+
// no-op
16771678
}
16781679
}
1679-
return XmldbURI.EMPTY_URI.toString();
1680+
1681+
return null;
16801682
}
16811683
}
16821684

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.exist.xquery.value.ValueSequence;
6262
import org.w3c.dom.*;
6363

64+
import javax.annotation.Nullable;
6465
import javax.xml.XMLConstants;
6566
import java.util.HashMap;
6667
import java.util.HashSet;
@@ -619,18 +620,18 @@ public int getItemType() {
619620
}
620621

621622
@Override
622-
public String getBaseURI() {
623-
final XmldbURI baseURI = calculateBaseURI();
624-
if(baseURI != null) {
623+
public @Nullable String getBaseURI() {
624+
@Nullable final XmldbURI baseURI = calculateBaseURI();
625+
if (baseURI != null) {
625626
return baseURI.toString();
626627
}
627628

628-
return "";//UNDERSTAND: is it ok?
629+
return null;
629630
}
630631

631632
// NOTE(AR) please keep in sync with org.exist.dom.persistent.ElementImpl
632-
private XmldbURI calculateBaseURI() {
633-
XmldbURI baseURI = null;
633+
private @Nullable XmldbURI calculateBaseURI() {
634+
@Nullable XmldbURI baseURI = null;
634635

635636
final String nodeBaseURI = getAttributeNS(Namespaces.XML_NS, "base");
636637
if (!nodeBaseURI.isEmpty()) {
@@ -648,27 +649,32 @@ private XmldbURI calculateBaseURI() {
648649

649650
if (parent != -1) {
650651
if (nodeBaseURI.isEmpty()) {
651-
baseURI = ((ElementImpl) document.getNode(parent))
652-
.calculateBaseURI();
652+
baseURI = ((ElementImpl) document.getNode(parent)).calculateBaseURI();
653653
} else {
654-
final XmldbURI parentsBaseURI = ((ElementImpl) document.getNode(parent)).calculateBaseURI();
655-
if (parentsBaseURI.toString().endsWith("/") || !parentsBaseURI.toString().contains("/")) {
656-
baseURI = parentsBaseURI.append(baseURI);
654+
@Nullable final XmldbURI parentsBaseURI = ((ElementImpl) document.getNode(parent)).calculateBaseURI();
655+
if (parentsBaseURI == null) {
656+
baseURI = null;
657657
} else {
658-
// there is a filename, remove it
659-
baseURI = parentsBaseURI.removeLastSegment().append(baseURI);
658+
if (parentsBaseURI.toString().endsWith("/") || !parentsBaseURI.toString().contains("/")) {
659+
baseURI = parentsBaseURI.append(baseURI);
660+
} else {
661+
// there is a filename, remove it
662+
baseURI = parentsBaseURI.removeLastSegment().append(baseURI);
663+
}
660664
}
661665
}
662666
} else {
663-
if (nodeBaseURI.isEmpty()) {
664-
return XmldbURI.create(getOwnerDocument().getBaseURI(), false);
667+
@Nullable final String docBaseURI = getOwnerDocument().getBaseURI();
668+
if (docBaseURI == null) {
669+
baseURI = null;
670+
} else if (nodeBaseURI.isEmpty()) {
671+
baseURI = XmldbURI.create(docBaseURI, false);
665672
} else if (nodeNumber != 1) {
666-
final String docBaseURI = getOwnerDocument().getBaseURI();
667673
if (docBaseURI.endsWith("/")) {
668-
baseURI = XmldbURI.create(getOwnerDocument().getBaseURI(), false);
674+
baseURI = XmldbURI.create(docBaseURI, false);
669675
baseURI.append(baseURI);
670676
} else {
671-
baseURI = XmldbURI.create(getOwnerDocument().getBaseURI(), false);
677+
baseURI = XmldbURI.create(docBaseURI, false);
672678
baseURI = baseURI.removeLastSegment();
673679
baseURI.append(baseURI);
674680
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ public void removeDuplicates() {
770770
}
771771

772772
@Override
773-
public String getBaseURI() {
773+
public @Nullable String getBaseURI() {
774774
return null;
775775
}
776776

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

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import org.w3c.dom.Node;
5858
import org.w3c.dom.ProcessingInstruction;
5959

60+
import javax.annotation.Nullable;
61+
6062

6163
public class ProcessingInstructionImpl extends NodeImpl implements ProcessingInstruction {
6264

@@ -102,37 +104,12 @@ public void setData(final String data) throws DOMException {
102104
}
103105

104106
@Override
105-
public String getBaseURI() {
106-
String baseURI = "";
107-
int parent = -1;
108-
int test = document.getParentNodeFor(nodeNumber);
109-
110-
if(document.nodeKind[test] != Node.DOCUMENT_NODE) {
111-
parent = test;
112-
}
113-
114-
// fixme! Testa med 0/ljo
115-
while((parent != -1) && (document.getNode(parent).getBaseURI() != null)) {
116-
117-
if(baseURI.isEmpty()) {
118-
baseURI = document.getNode(parent).getBaseURI();
119-
} else {
120-
baseURI = document.getNode(parent).getBaseURI() + "/" + baseURI;
121-
}
122-
123-
test = document.getParentNodeFor(parent);
124-
125-
if(document.nodeKind[test] == Node.DOCUMENT_NODE) {
126-
return (baseURI);
127-
} else {
128-
parent = test;
129-
}
130-
}
131-
132-
if(baseURI.isEmpty()) {
133-
baseURI = getOwnerDocument().getBaseURI();
107+
public @Nullable String getBaseURI() {
108+
@Nullable final Node parent = getParentNode();
109+
if (parent == null) {
110+
return null;
134111
}
135-
return (baseURI);
112+
return parent.getBaseURI();
136113
}
137114

138115
@Override

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import org.exist.xquery.Expression;
2525
import org.exist.xquery.value.Type;
2626
import org.w3c.dom.DOMException;
27+
import org.w3c.dom.Node;
2728
import org.w3c.dom.Text;
2829

30+
import javax.annotation.Nullable;
31+
2932

3033
public class TextImpl extends AbstractCharacterData implements Text {
3134

@@ -42,6 +45,15 @@ public int getItemType() {
4245
return Type.TEXT;
4346
}
4447

48+
@Override
49+
public @Nullable String getBaseURI() {
50+
@Nullable final Node parent = getParentNode();
51+
if (parent == null) {
52+
return null;
53+
}
54+
return parent.getBaseURI();
55+
}
56+
4557
@Override
4658
public Text splitText(final int offset) throws DOMException {
4759
return null;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.exist.xquery.Expression;
6161
import org.w3c.dom.*;
6262

63+
import javax.annotation.Nullable;
6364
import javax.xml.XMLConstants;
6465

6566
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -421,9 +422,9 @@ public boolean isId() {
421422
}
422423

423424
@Override
424-
public String getBaseURI() {
425-
final Element e = getOwnerElement();
426-
if(e != null) {
425+
public @Nullable String getBaseURI() {
426+
@Nullable final Element e = getOwnerElement();
427+
if (e != null) {
427428
return e.getBaseURI();
428429
}
429430
return null;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import org.w3c.dom.Comment;
5454
import org.w3c.dom.Node;
5555

56+
import javax.annotation.Nullable;
57+
5658
import static java.nio.charset.StandardCharsets.UTF_8;
5759

5860
public class CommentImpl extends AbstractCharacterData<CommentImpl> implements Comment {
@@ -86,6 +88,16 @@ public String toString() {
8688
return "<!-- " + cdata.toString() + " -->";
8789
}
8890

91+
@Override
92+
public @Nullable String getBaseURI() {
93+
@Nullable final Node parent = getParentNode();
94+
if (parent != null) {
95+
return parent.getBaseURI();
96+
} else {
97+
return null;
98+
}
99+
}
100+
89101
/**
90102
* Serializes a (persistent DOM) Comment to a byte array
91103
*

0 commit comments

Comments
 (0)