Skip to content

Commit c238197

Browse files
committed
[bugfix] W3C DOM spec states that getBaseURI() can return null
1 parent 8ba7f50 commit c238197

12 files changed

Lines changed: 103 additions & 95 deletions

File tree

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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.w3c.dom.Comment;
3030
import org.w3c.dom.Node;
3131

32+
import javax.annotation.Nullable;
33+
3234
public class CommentImpl extends AbstractCharacterData implements Comment {
3335

3436
public CommentImpl(final DocumentImpl doc, final int nodeNumber) {
@@ -53,9 +55,9 @@ public AtomicValue atomize() throws XPathException {
5355
}
5456

5557
@Override
56-
public String getBaseURI() {
57-
final Node parent = getParentNode();
58-
if(parent == null) {
58+
public @Nullable String getBaseURI() {
59+
@Nullable final Node parent = getParentNode();
60+
if (parent == null) {
5961
return null;
6062
}
6163
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
@@ -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/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/ElementImpl.java

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

20202020
@Override
2021-
public String getBaseURI() {
2022-
final XmldbURI baseURI = calculateBaseURI();
2023-
if(baseURI != null) {
2021+
public @Nullable String getBaseURI() {
2022+
@Nullable final XmldbURI baseURI = calculateBaseURI();
2023+
if (baseURI != null) {
20242024
return baseURI.toString();
20252025
}
20262026

2027-
return ""; //UNDERSTAND: is it ok?
2027+
return null;
20282028
}
20292029

20302030
// NOTE(AR) please keep in sync with org.exist.dom.memtree.ElementImpl
20312031
private XmldbURI calculateBaseURI() {
2032-
XmldbURI baseURI = null;
2032+
@Nullable XmldbURI baseURI = null;
20332033

20342034
final String nodeBaseURI = getAttributeNS(Namespaces.XML_NS, "base");
20352035
if (!nodeBaseURI.isEmpty()) {
@@ -2040,28 +2040,35 @@ private XmldbURI calculateBaseURI() {
20402040
}
20412041

20422042
final IStoredNode<?> parent = getParentStoredNode();
2043+
20432044
if (parent != null) {
20442045
if (nodeBaseURI.isEmpty()) {
20452046
baseURI = ((ElementImpl) parent).calculateBaseURI();
20462047
} else {
2047-
final XmldbURI parentsBaseURI = ((ElementImpl) parent).calculateBaseURI();
2048-
if (parentsBaseURI.toString().endsWith("/") || !parentsBaseURI.toString().contains("/")) {
2049-
baseURI = parentsBaseURI.append(baseURI);
2048+
@Nullable final XmldbURI parentsBaseURI = ((ElementImpl) parent).calculateBaseURI();
2049+
if (parentsBaseURI == null) {
2050+
baseURI = null;
20502051
} else {
2051-
// there is a filename, remove it
2052-
baseURI = parentsBaseURI.removeLastSegment().append(baseURI);
2052+
if (parentsBaseURI.toString().endsWith("/") || !parentsBaseURI.toString().contains("/")) {
2053+
baseURI = parentsBaseURI.append(baseURI);
2054+
} else {
2055+
// there is a filename, remove it
2056+
baseURI = parentsBaseURI.removeLastSegment().append(baseURI);
2057+
}
20532058
}
20542059
}
20552060
} else {
2056-
if (nodeBaseURI.isEmpty()) {
2057-
return XmldbURI.create(getOwnerDocument().getBaseURI(), false);
2061+
@Nullable final String docBaseURI = getOwnerDocument().getBaseURI();
2062+
if (docBaseURI == null) {
2063+
baseURI = null;
2064+
} else if (nodeBaseURI.isEmpty()) {
2065+
baseURI = XmldbURI.create(docBaseURI, false);
20582066
} else {
2059-
final String docBaseURI = getOwnerDocument().getBaseURI();
20602067
if (docBaseURI.endsWith("/")) {
2061-
baseURI = XmldbURI.create(getOwnerDocument().getBaseURI(), false);
2068+
baseURI = XmldbURI.create(docBaseURI, false);
20622069
baseURI.append(baseURI);
20632070
} else {
2064-
baseURI = XmldbURI.create(getOwnerDocument().getBaseURI(), false);
2071+
baseURI = XmldbURI.create(docBaseURI, false);
20652072
baseURI = baseURI.removeLastSegment();
20662073
baseURI.append(baseURI);
20672074
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.exist.xquery.Expression;
3131
import org.w3c.dom.*;
3232

33+
import javax.annotation.Nullable;
3334
import javax.xml.XMLConstants;
3435

3536
public abstract class NodeImpl<T extends NodeImpl> implements INode<DocumentImpl, T> {
@@ -187,8 +188,8 @@ public void normalize() {
187188
}
188189

189190
@Override
190-
public String getBaseURI() {
191-
throw unsupported();
191+
public @Nullable String getBaseURI() {
192+
return null;
192193
}
193194

194195
@Override

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import org.w3c.dom.Node;
5656
import org.w3c.dom.ProcessingInstruction;
5757

58+
import javax.annotation.Nullable;
59+
5860
import static java.nio.charset.StandardCharsets.UTF_8;
5961

6062
/**
@@ -148,13 +150,10 @@ public void setData(final String data) {
148150
this.data = data;
149151
}
150152

151-
/**
152-
* ? @see org.w3c.dom.Node#getBaseURI()
153-
*/
154153
@Override
155-
public String getBaseURI() {
156-
final StoredNode parent = getParentStoredNode();
157-
if(parent != null) {
154+
public @Nullable String getBaseURI() {
155+
@Nullable final StoredNode parent = getParentStoredNode();
156+
if (parent != null) {
158157
return parent.getBaseURI();
159158
} else {
160159
return getOwnerDocument().getBaseURI();

0 commit comments

Comments
 (0)