Skip to content

Commit ec385f3

Browse files
authored
Merge pull request #218 from evolvedbinary/6.x.x/bugfix/base-uri-returns-db-instead-of-document-path
[6.x.x] Correct an issue with the Base URI of in-memory documents
2 parents 53d1e8a + 4425ce8 commit ec385f3

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
@@ -777,6 +777,7 @@
777777
<include>src/test/resources/log4j2.xml</include>
778778
<include>src/test/resources/standalone-webapp/WEB-INF/web.xml</include>
779779
<include>src/main/xjb/rest-api.xjb</include>
780+
<include>src/test/xquery/base-uri.xql</include>
780781
<include>src/test/xquery/parenthesizedLocationStep.xml</include>
781782
<include>src/test/xquery/tail-recursion.xml</include>
782783
<include>src/test/xquery/maps/maps.xqm</include>
@@ -864,6 +865,7 @@
864865
<include>src/main/java/org/exist/dom/QName.java</include>
865866
<include>src/main/java/org/exist/dom/memtree/AbstractCharacterData.java</include>
866867
<include>src/main/java/org/exist/dom/memtree/AttrImpl.java</include>
868+
<include>src/main/java/org/exist/dom/memtree/CommentImpl.java</include>
867869
<include>src/main/java/org/exist/dom/memtree/DocumentBuilderReceiver.java</include>
868870
<include>src/main/java/org/exist/dom/memtree/DocumentImpl.java</include>
869871
<include>src/test/java/org/exist/dom/memtree/DocumentImplTest.java</include>
@@ -895,6 +897,7 @@
895897
<include>src/main/java/org/exist/dom/persistent/LockToken.java</include>
896898
<include>src/main/java/org/exist/dom/persistent/Match.java</include>
897899
<include>src/main/java/org/exist/dom/persistent/NewArrayNodeSet.java</include>
900+
<include>src/main/java/org/exist/dom/persistent/NodeImpl.java</include>
898901
<include>src/main/java/org/exist/dom/persistent/NodeProxy.java</include>
899902
<include>src/main/java/org/exist/dom/persistent/NodeSet.java</include>
900903
<include>src/test/java/org/exist/dom/persistent/NodeTest.java</include>
@@ -1483,6 +1486,7 @@
14831486
<exclude>src/test/resources/log4j2.xml</exclude>
14841487
<exclude>src/test/resources/standalone-webapp/WEB-INF/web.xml</exclude>
14851488
<exclude>src/main/xjb/rest-api.xjb</exclude>
1489+
<exclude>src/test/xquery/base-uri.xql</exclude>
14861490
<exclude>src/test/xquery/binary-value.xqm</exclude>
14871491
<exclude>src/test/xquery/parenthesizedLocationStep.xml</exclude>
14881492
<exclude>src/test/xquery/pi.xqm</exclude>
@@ -1574,6 +1578,7 @@
15741578
<exclude>src/main/java/org/exist/dom/QName.java</exclude>
15751579
<exclude>src/main/java/org/exist/dom/memtree/AbstractCharacterData.java</exclude>
15761580
<exclude>src/main/java/org/exist/dom/memtree/AttrImpl.java</exclude>
1581+
<include>src/main/java/org/exist/dom/memtree/CommentImpl.java</include>
15771582
<exclude>src/main/java/org/exist/dom/memtree/DocumentBuilderReceiver.java</exclude>
15781583
<exclude>src/main/java/org/exist/dom/memtree/DocumentImpl.java</exclude>
15791584
<exclude>src/test/java/org/exist/dom/memtree/DocumentImplTest.java</exclude>
@@ -1613,6 +1618,7 @@
16131618
<exclude>src/main/java/org/exist/dom/persistent/LockToken.java</exclude>
16141619
<exclude>src/main/java/org/exist/dom/persistent/Match.java</exclude>
16151620
<exclude>src/main/java/org/exist/dom/persistent/NewArrayNodeSet.java</exclude>
1621+
<exclude>src/main/java/org/exist/dom/persistent/NodeImpl.java</exclude>
16161622
<exclude>src/main/java/org/exist/dom/persistent/NodeProxy.java</exclude>
16171623
<exclude>src/main/java/org/exist/dom/persistent/NodeSet.java</exclude>
16181624
<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
@@ -804,7 +804,7 @@ public void removeDuplicates() {
804804
}
805805

806806
@Override
807-
public String getBaseURI() {
807+
public @Nullable String getBaseURI() {
808808
return null;
809809
}
810810

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;
@@ -430,9 +431,9 @@ public boolean isId() {
430431
}
431432

432433
@Override
433-
public String getBaseURI() {
434-
final Element e = getOwnerElement();
435-
if(e != null) {
434+
public @Nullable String getBaseURI() {
435+
@Nullable final Element e = getOwnerElement();
436+
if (e != null) {
436437
return e.getBaseURI();
437438
}
438439
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)