Skip to content
Permalink
Browse files

Fix bug in pxp:set-base-uri where the base URI of descendant nodes wa…

…sn't changed; refactored TreeWriter a bit
  • Loading branch information
ndw committed Feb 8, 2014
1 parent 6b5b11d commit d9644413ee9c69d9a63f3e03d305ae100b4e8bd6
Showing with 34 additions and 30 deletions.
  1. +25 −4 src/com/xmlcalabash/extensions/SetBaseURI.java
  2. +9 −26 src/com/xmlcalabash/util/TreeWriter.java
@@ -26,6 +26,8 @@
private XProcRuntime runtime = null;
private ReadablePipe source = null;
private WritablePipe result = null;
private TreeWriter tree = null;
private URI baseURI = null;

/**
* Creates a new instance of SetBaseURI
@@ -56,18 +58,37 @@ public void run() throws SaxonApiException {
throw new XProcException("URI is required");
}

URI uri = getOption(_uri).getBaseURI().resolve(uris);
baseURI = getOption(_uri).getBaseURI().resolve(uris);

XdmNode doc = source.read();
TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(uri);
tree = new TreeWriter(runtime);
tree.startDocument(baseURI);

for (XdmNode node : new RelevantNodes(doc, Axis.CHILD, true)) {
tree.addSubtree(node);
write(node);
}

tree.endDocument();

result.write(tree.getResult());
}

private void write(XdmNode node) {
switch (node.getNodeKind()) {
case ELEMENT:
tree.addStartElement(node, baseURI);
break;
case TEXT:
tree.addSubtree(node);
break;
case COMMENT:
tree.addSubtree(node);
break;
case PROCESSING_INSTRUCTION:
tree.addSubtree(node);
break;
default:
throw new XProcException("Unexpected node kind!?");
}
}
}
@@ -158,34 +158,18 @@ protected void writeChildren(XdmNode node) {
}

public void addStartElement(XdmNode node) {
NodeInfo inode = node.getUnderlyingNode();

NamespaceBinding inscopeNS[] = null;
if (seenRoot) {
inscopeNS = inode.getDeclaredNamespaces(null);
} else {
int count = 0;
Iterator<NamespaceBinding> nsiter = NamespaceIterator.iterateNamespaces(inode);
while (nsiter.hasNext()) {
count++;
nsiter.next();
}
inscopeNS = new NamespaceBinding[count];
nsiter = NamespaceIterator.iterateNamespaces(inode);
count = 0;
while (nsiter.hasNext()) {
inscopeNS[count] = nsiter.next();
count++;
}
seenRoot = true;
}
addStartElement(node, node.getNodeName(), node.getBaseURI());
}

URI nodeBaseURI = node.getBaseURI();
receiver.setSystemId(nodeBaseURI.toASCIIString());
addStartElement(new NameOfNode(inode), inode.getSchemaType(), inscopeNS);
public void addStartElement(XdmNode node, URI overrideBaseURI) {
addStartElement(node, node.getNodeName(), overrideBaseURI);
}

public void addStartElement(XdmNode node, QName newName) {
addStartElement(node, newName, node.getBaseURI());
}

public void addStartElement(XdmNode node, QName newName, URI overrideBaseURI) {
NodeInfo inode = node.getUnderlyingNode();

NamespaceBinding inscopeNS[] = null;
@@ -231,8 +215,7 @@ public void addStartElement(XdmNode node, QName newName) {
}
}

URI nodeBaseURI = node.getBaseURI();
receiver.setSystemId(nodeBaseURI.toASCIIString());
receiver.setSystemId(overrideBaseURI.toASCIIString());
FingerprintedQName newNameOfNode = new FingerprintedQName(newName.getPrefix(),newName.getNamespaceURI(),newName.getLocalName());
addStartElement(newNameOfNode, inode.getSchemaType(), inscopeNS);
}

0 comments on commit d964441

Please sign in to comment.
You can’t perform that action at this time.