Skip to content

Commit

Permalink
[bugfix] Do not conflate Node Type with XDM Type
Browse files Browse the repository at this point in the history
  • Loading branch information
adamretter committed Oct 9, 2024
1 parent 2a72d93 commit edc62fb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
25 changes: 5 additions & 20 deletions exist-core/src/main/java/org/exist/dom/persistent/NodeProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public int compareTo(final NodeProxy other) {
*/
@Override
public boolean equals(final Object other) {
if(!(other instanceof NodeProxy otherNode)) {
if(!(other instanceof final NodeProxy otherNode)) {
return false;
}

Expand Down Expand Up @@ -671,25 +671,10 @@ public String debugContext() {
// methods of interface Item
@Override
public int getType() {
return nodeType2XQuery(nodeType);
}

public static int nodeType2XQuery(final short nodeType) {
return switch (nodeType) {
case Node.ELEMENT_NODE ->
//TODO : return Type.DOCUMENT for some in-memory nodes :
//http://sourceforge.net/tracker/index.php?func=detail&aid=1730690&group_id=17691&atid=117691
//Ideally compute this when proxy is constructed
Type.ELEMENT;
case Node.ATTRIBUTE_NODE -> Type.ATTRIBUTE;
case Node.TEXT_NODE -> Type.TEXT;
case Node.CDATA_SECTION_NODE -> Type.CDATA_SECTION;
case Node.PROCESSING_INSTRUCTION_NODE -> Type.PROCESSING_INSTRUCTION;
case Node.COMMENT_NODE -> Type.COMMENT;
case Node.DOCUMENT_NODE -> Type.DOCUMENT;
//(yet) unknown type : return generic
default -> Type.NODE;
};
if (nodeType == UNKNOWN_NODE_TYPE) {
return Type.NODE;
}
return Type.fromDomNodeType(nodeType);
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions exist-core/src/main/java/org/exist/xqj/Marshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ public static void marshallItem(final DBBroker broker, final Item item, final Co
throws SAXException, XPathException {
final AttributesImpl attrs = new AttributesImpl();
int type = item.getType();
if (type == Type.NODE)
{type = ((NodeValue)item).getNode().getNodeType();}
if (type == Type.NODE) {
final short nodeType = ((NodeValue)item).getNode().getNodeType();
type = Type.fromDomNodeType(nodeType);
}
attrs.addAttribute("", ATTR_TYPE, ATTR_TYPE, "CDATA", Type.getTypeName(type));
if (Type.subTypeOf(item.getType(), Type.NODE)) {
handler.startElement(NAMESPACE, VALUE_ELEMENT, VALUE_ELEMENT_QNAME, attrs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathExc
itemType = item.getType();
if (itemType == NodeProxy.UNKNOWN_NODE_TYPE) {
//Retrieve the actual node
itemType = NodeProxy.nodeType2XQuery(((NodeProxy) item).getNode().getNodeType());
itemType = Type.fromDomNodeType(((NodeProxy) item).getNode().getNodeType());
}
}
if (!Type.subTypeOf(itemType, test.getType())) {
Expand Down
41 changes: 41 additions & 0 deletions exist-core/src/main/java/org/exist/xquery/value/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.exist.dom.QName;
import org.exist.xquery.Expression;
import org.exist.xquery.XPathException;
import org.w3c.dom.Node;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -707,4 +708,44 @@ public static int primitiveTypeOf(final int type) throws IllegalArgumentExceptio
}
return primitiveType;
}

/**
* Get the XDM equivalent type of a DOM Node type (i.e. {@link Node#getNodeType()}).
*
* @param domNodeType the DOM node type as defined in {@link Node}.
*
* @return the equivalent XDM type.
*
* @throws IllegalArgumentException if the provided argument is not a DOM Node Type.
*/
public static int fromDomNodeType(final short domNodeType) {
switch (domNodeType) {
case Node.ELEMENT_NODE:
return Type.ELEMENT;
case Node.ATTRIBUTE_NODE:
return Type.ATTRIBUTE;
case Node.TEXT_NODE:
return Type.TEXT;
case Node.CDATA_SECTION_NODE:
return Type.CDATA_SECTION;
case Node.PROCESSING_INSTRUCTION_NODE:
return Type.PROCESSING_INSTRUCTION;
case Node.COMMENT_NODE:
return Type.COMMENT;
case Node.DOCUMENT_NODE:
return Type.DOCUMENT;

// un-mappable Node types, so just return the XDM Node type
case Node.ENTITY_REFERENCE_NODE:
case Node.ENTITY_NODE:
case Node.DOCUMENT_TYPE_NODE:
case Node.DOCUMENT_FRAGMENT_NODE:
case Node.NOTATION_NODE:
return Type.NODE;

// unknown
default:
throw new IllegalArgumentException("Unknown DOM Node type: " + domNodeType);
}
}
}

0 comments on commit edc62fb

Please sign in to comment.