Skip to content

Commit 78d601b

Browse files
committed
[bugfix] W3C DOM spec states that getBaseURI() can return null
1 parent dd176b3 commit 78d601b

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
@@ -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
}

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)