Skip to content

Commit 22633bc

Browse files
committed
[bugfix] W3C DOM spec states that getBaseURI() can return null
1 parent c588dad commit 22633bc

13 files changed

Lines changed: 155 additions & 95 deletions

File tree

exist-core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@
894894
<include>src/main/java/org/exist/dom/QName.java</include>
895895
<include>src/main/java/org/exist/dom/memtree/AbstractCharacterData.java</include>
896896
<include>src/main/java/org/exist/dom/memtree/AttrImpl.java</include>
897+
<include>src/main/java/org/exist/dom/memtree/CommentImpl.java</include>
897898
<include>src/main/java/org/exist/dom/memtree/DocumentBuilderReceiver.java</include>
898899
<include>src/main/java/org/exist/dom/memtree/DocumentImpl.java</include>
899900
<include>src/test/java/org/exist/dom/memtree/DocumentImplTest.java</include>
@@ -924,6 +925,7 @@
924925
<include>src/main/java/org/exist/dom/persistent/EmptyNodeSet.java</include>
925926
<include>src/main/java/org/exist/dom/persistent/LockToken.java</include>
926927
<include>src/main/java/org/exist/dom/persistent/NewArrayNodeSet.java</include>
928+
<include>src/main/java/org/exist/dom/persistent/NodeImpl.java</include>
927929
<include>src/main/java/org/exist/dom/persistent/NodeProxy.java</include>
928930
<include>src/main/java/org/exist/dom/persistent/NodeSet.java</include>
929931
<include>src/test/java/org/exist/dom/persistent/NodeTest.java</include>
@@ -1614,6 +1616,7 @@
16141616
<exclude>src/main/java/org/exist/dom/QName.java</exclude>
16151617
<exclude>src/main/java/org/exist/dom/memtree/AbstractCharacterData.java</exclude>
16161618
<exclude>src/main/java/org/exist/dom/memtree/AttrImpl.java</exclude>
1619+
<include>src/main/java/org/exist/dom/memtree/CommentImpl.java</include>
16171620
<exclude>src/main/java/org/exist/dom/memtree/DocumentBuilderReceiver.java</exclude>
16181621
<exclude>src/main/java/org/exist/dom/memtree/DocumentImpl.java</exclude>
16191622
<exclude>src/test/java/org/exist/dom/memtree/DocumentImplTest.java</exclude>
@@ -1652,6 +1655,7 @@
16521655
<exclude>src/main/java/org/exist/dom/persistent/EmptyNodeSet.java</exclude>
16531656
<exclude>src/main/java/org/exist/dom/persistent/LockToken.java</exclude>
16541657
<exclude>src/main/java/org/exist/dom/persistent/NewArrayNodeSet.java</exclude>
1658+
<exclude>src/main/java/org/exist/dom/persistent/NodeImpl.java</exclude>
16551659
<exclude>src/main/java/org/exist/dom/persistent/NodeProxy.java</exclude>
16561660
<exclude>src/main/java/org/exist/dom/persistent/NodeSet.java</exclude>
16571661
<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: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,19 +1657,29 @@ public XQueryContext getContext() {
16571657
}
16581658

16591659
@Override
1660-
public String getBaseURI() {
1661-
final String docURI = getDocumentURI();
1662-
if(docURI != null) {
1663-
return docURI;
1660+
public @Nullable String getBaseURI() {
1661+
@Nullable final Element el = getDocumentElement();
1662+
if (el != null) {
1663+
final String baseURI = getDocumentElement().getAttributeNS(Namespaces.XML_NS, "base");
1664+
if (!baseURI.isEmpty()) {
1665+
return baseURI;
1666+
}
16641667
}
1665-
if(context != null && context.isBaseURIDeclared()) {
1666-
try {
1667-
return context.getBaseURI().getStringValue();
1668-
} catch(final XPathException e) {
1669-
//TODO : make something !
1668+
1669+
@Nullable final String docURI = getDocumentURI();
1670+
if (docURI != null) {
1671+
return docURI;
1672+
} else {
1673+
if (context != null && context.isBaseURIDeclared()) {
1674+
try {
1675+
return context.getBaseURI().getStringValue();
1676+
} catch (final XPathException e) {
1677+
// no-op
1678+
}
16701679
}
1680+
1681+
return null;
16711682
}
1672-
return XmldbURI.EMPTY_URI.toString();
16731683
}
16741684

16751685
@Override

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/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/ElementImpl.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,18 +2000,18 @@ public void setIdAttributeNode(final Attr idAttr, final boolean isId) throws DOM
20002000
}
20012001

20022002
@Override
2003-
public String getBaseURI() {
2004-
final XmldbURI baseURI = calculateBaseURI();
2005-
if(baseURI != null) {
2003+
public @Nullable String getBaseURI() {
2004+
@Nullable final XmldbURI baseURI = calculateBaseURI();
2005+
if (baseURI != null) {
20062006
return baseURI.toString();
20072007
}
20082008

2009-
return ""; //UNDERSTAND: is it ok?
2009+
return null;
20102010
}
20112011

20122012
// NOTE(AR) please keep in sync with org.exist.dom.memtree.ElementImpl
20132013
private XmldbURI calculateBaseURI() {
2014-
XmldbURI baseURI = null;
2014+
@Nullable XmldbURI baseURI = null;
20152015

20162016
final String nodeBaseURI = getAttributeNS(Namespaces.XML_NS, "base");
20172017
if (!nodeBaseURI.isEmpty()) {
@@ -2022,28 +2022,35 @@ private XmldbURI calculateBaseURI() {
20222022
}
20232023

20242024
final IStoredNode<?> parent = getParentStoredNode();
2025+
20252026
if (parent != null) {
20262027
if (nodeBaseURI.isEmpty()) {
20272028
baseURI = ((ElementImpl) parent).calculateBaseURI();
20282029
} else {
2029-
final XmldbURI parentsBaseURI = ((ElementImpl) parent).calculateBaseURI();
2030-
if (parentsBaseURI.toString().endsWith("/") || !parentsBaseURI.toString().contains("/")) {
2031-
baseURI = parentsBaseURI.append(baseURI);
2030+
@Nullable final XmldbURI parentsBaseURI = ((ElementImpl) parent).calculateBaseURI();
2031+
if (parentsBaseURI == null) {
2032+
baseURI = null;
20322033
} else {
2033-
// there is a filename, remove it
2034-
baseURI = parentsBaseURI.removeLastSegment().append(baseURI);
2034+
if (parentsBaseURI.toString().endsWith("/") || !parentsBaseURI.toString().contains("/")) {
2035+
baseURI = parentsBaseURI.append(baseURI);
2036+
} else {
2037+
// there is a filename, remove it
2038+
baseURI = parentsBaseURI.removeLastSegment().append(baseURI);
2039+
}
20352040
}
20362041
}
20372042
} else {
2038-
if (nodeBaseURI.isEmpty()) {
2039-
return XmldbURI.create(getOwnerDocument().getBaseURI(), false);
2043+
@Nullable final String docBaseURI = getOwnerDocument().getBaseURI();
2044+
if (docBaseURI == null) {
2045+
baseURI = null;
2046+
} else if (nodeBaseURI.isEmpty()) {
2047+
baseURI = XmldbURI.create(docBaseURI, false);
20402048
} else {
2041-
final String docBaseURI = getOwnerDocument().getBaseURI();
20422049
if (docBaseURI.endsWith("/")) {
2043-
baseURI = XmldbURI.create(getOwnerDocument().getBaseURI(), false);
2050+
baseURI = XmldbURI.create(docBaseURI, false);
20442051
baseURI.append(baseURI);
20452052
} else {
2046-
baseURI = XmldbURI.create(getOwnerDocument().getBaseURI(), false);
2053+
baseURI = XmldbURI.create(docBaseURI, false);
20472054
baseURI = baseURI.removeLastSegment();
20482055
baseURI.append(baseURI);
20492056
}

0 commit comments

Comments
 (0)