Skip to content

Commit

Permalink
Merge pull request #7 from ndw/fix-base-uri
Browse files Browse the repository at this point in the history
Fix base URI when using the file convenience method
  • Loading branch information
ndw authored Nov 4, 2022
2 parents 87002aa + 0fa61d2 commit edea8ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ org.gradle.jvmargs=-Xmx4096m

basename=sinclude
sincludeTitle=Saxon XInclude
sincludeVersion=4.2.0
sincludeVersion=4.2.1

saxonVersion=11.4
22 changes: 20 additions & 2 deletions src/main/java/com/nwalsh/sinclude/XInclude.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public void expandXIncludes(File input, File output) throws SaxonApiException, X
logger = new DebuggingLogger(node.getUnderlyingNode().getConfiguration().getLogger());

TreeWalker walker = new TreeWalker();
walker.setXmlBase(input.toURI());
walker.register(xi_include, new XiIncludeHandler(this));
walker.register(xi_fallback, new XiFallbackHandler());

Expand Down Expand Up @@ -547,6 +548,8 @@ public XdmNode process(XdmNode node) {

private class TreeWalker {
private HashMap<QName,ElementHandler> handlers = new HashMap<>();
private URI overrideBaseURI = null;
private boolean root = true;

public void register(QName name, ElementHandler handler) {
if (handlers.containsKey(name)) {
Expand All @@ -555,6 +558,10 @@ public void register(QName name, ElementHandler handler) {
handlers.put(name, handler);
}

public void setXmlBase(URI base) {
overrideBaseURI = base;
}

public XdmNode walk(XdmNode node) throws XPathException {
XdmDestination destination = ReceiverUtils.makeDestination(node);
Receiver receiver = ReceiverUtils.makeReceiver(node, destination);
Expand All @@ -565,7 +572,7 @@ public XdmNode walk(XdmNode node) throws XPathException {
return destination.getXdmNode();
}

public void traverse(Receiver receiver, XdmNode node) throws XPathException {
private void traverse(Receiver receiver, XdmNode node) throws XPathException {
XdmSequenceIterator<XdmNode> iter = null;

if (node.getNodeKind() == XdmNodeKind.DOCUMENT) {
Expand All @@ -584,7 +591,18 @@ public void traverse(Receiver receiver, XdmNode node) throws XPathException {
} else {
NodeInfo inode = node.getUnderlyingNode();
FingerprintedQName name = new FingerprintedQName(inode.getPrefix(), inode.getURI(), inode.getLocalPart());
receiver.startElement(name, inode.getSchemaType(), inode.attributes(), inode.getAllNamespaces(), inode.saveLocation(), 0);

final AttributeMap amap;
if (root && fixupXmlBase && overrideBaseURI != null && inode.getAttributeValue(NS_XML, "base") == null) {
FingerprintedQName xml_base = new FingerprintedQName("xml", NS_XML, "base");
AttributeInfo base = new AttributeInfo(xml_base, BuiltInAtomicType.ANY_URI, overrideBaseURI.toString(), inode.saveLocation(), 0);
amap = inode.attributes().put(base);
} else {
amap = inode.attributes();
}
root = false;

receiver.startElement(name, inode.getSchemaType(), amap, inode.getAllNamespaces(), inode.saveLocation(), 0);
iter = node.axisIterator(Axis.CHILD);
while (iter.hasNext()) {
XdmNode item = iter.next();
Expand Down

0 comments on commit edea8ad

Please sign in to comment.