Skip to content

Commit

Permalink
Fix #338 don't lose base URI when copying PIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ndw committed Sep 19, 2022
1 parent 17746e7 commit 85b1f2d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/xmlcalabash/util/ProcessMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private void traverse(XdmNode node) throws SaxonApiException, XPathException {
processor.processPI(node);
saw = 0;
} else {
addPI(node.getNodeName().getLocalName(), node.getStringValue());
addPI(node);
}
} else {
throw new UnsupportedOperationException("Unexpected node type");
Expand Down
54 changes: 32 additions & 22 deletions src/main/java/com/xmlcalabash/util/TreeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,24 @@ public void endDocument() {
}

public void addSubtree(XdmNode node) {
try {
receiver.append(node.getUnderlyingNode());
} catch (UnsupportedOperationException use) {
// Do it the hard way
if (node.getNodeKind() == XdmNodeKind.DOCUMENT) {
writeChildren(node);
} else if (node.getNodeKind() == XdmNodeKind.ELEMENT) {
addStartElement(node);
writeChildren(node);
addEndElement();
} else if (node.getNodeKind() == XdmNodeKind.COMMENT) {
addComment(node.getStringValue());
} else if (node.getNodeKind() == XdmNodeKind.TEXT) {
addText(node.getStringValue());
} else if (node.getNodeKind() == XdmNodeKind.PROCESSING_INSTRUCTION) {
addPI(node.getNodeName().getLocalName(), node.getStringValue());
} else {
throw new UnsupportedOperationException("Unexpected node type");
}
} catch (XPathException xpe) {
throw new XProcException(xpe);
// N.B. It's tempting to copy the node with
// receiver.append(node.getUnderlyingNode());
// but that doesn't work: https://saxonica.plan.io/issues/5691

if (node.getNodeKind() == XdmNodeKind.DOCUMENT) {
writeChildren(node);
} else if (node.getNodeKind() == XdmNodeKind.ELEMENT) {
addStartElement(node);
writeChildren(node);
addEndElement();
} else if (node.getNodeKind() == XdmNodeKind.COMMENT) {
addComment(node.getStringValue());
} else if (node.getNodeKind() == XdmNodeKind.TEXT) {
addText(node.getStringValue());
} else if (node.getNodeKind() == XdmNodeKind.PROCESSING_INSTRUCTION) {
addPI(node.getNodeName().getLocalName(), node.getStringValue());
} else {
throw new UnsupportedOperationException("Unexpected node type");
}
}

Expand Down Expand Up @@ -338,11 +335,24 @@ public void addText(String text) {
}

public void addPI(String target, String data) {
addPI(target, data, VoidLocation.instance());
}

public void addPI(String target, String data, Location location) {
try {
receiver.processingInstruction(target, StringView.of(data), VoidLocation.instance(), 0);
receiver.processingInstruction(target, StringView.of(data), location, 0);
} catch (XPathException e) {
throw new XProcException(e);
}
}

public void addPI(XdmNode node) {
Location location = VoidLocation.instance();
if (node.getBaseURI() != null) {
location = new SysIdLocation(node.getBaseURI().toString());
receiver.setSystemId(node.getBaseURI().toString());
}
addPI(node.getNodeName().getLocalName(), node.getStringValue(), location);
}
}

0 comments on commit 85b1f2d

Please sign in to comment.