|
package com.nwalsh.drivers;
|
|
|
|
import net.sf.saxon.event.PipelineConfiguration;
|
|
import net.sf.saxon.event.Receiver;
|
|
import net.sf.saxon.om.EmptyAttributeMap;
|
|
import net.sf.saxon.om.FingerprintedQName;
|
|
import net.sf.saxon.s9api.Axis;
|
|
import net.sf.saxon.s9api.DocumentBuilder;
|
|
import net.sf.saxon.s9api.Processor;
|
|
import net.sf.saxon.s9api.SaxonApiException;
|
|
import net.sf.saxon.s9api.XdmDestination;
|
|
import net.sf.saxon.s9api.XdmNode;
|
|
import net.sf.saxon.s9api.XdmNodeKind;
|
|
import net.sf.saxon.s9api.XdmSequenceIterator;
|
|
import net.sf.saxon.serialize.SerializationProperties;
|
|
import net.sf.saxon.trans.XPathException;
|
|
import org.xml.sax.InputSource;
|
|
|
|
import javax.xml.transform.sax.SAXSource;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.net.URI;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
public class Main {
|
|
public static void main(String[] argv) throws SaxonApiException {
|
|
Processor processor = new Processor(false);
|
|
String docString = "<doc><p1/>text<p2/><p3/></doc>";
|
|
DocumentBuilder builder = processor.newDocumentBuilder();
|
|
builder.setBaseURI(URI.create("http://foo.com/"));
|
|
XdmNode document = builder.build(new SAXSource(new InputSource(new ByteArrayInputStream(docString.getBytes(StandardCharsets.UTF_8)))));
|
|
|
|
XdmSequenceIterator<XdmNode> iter = document.axisIterator(Axis.CHILD);
|
|
XdmNode doc = iter.next();
|
|
|
|
System.err.println("Original wrapper: " + doc.getBaseURI());
|
|
iter = doc.axisIterator(Axis.CHILD);
|
|
while (iter.hasNext()) {
|
|
XdmNode child = iter.next();
|
|
if (child.getNodeKind() == XdmNodeKind.ELEMENT) {
|
|
System.err.println("Original E:" + child.getNodeName() + ": " + child.getBaseURI());
|
|
} else {
|
|
System.err.println("Original T:" + child.getStringValue() + ": " + child.getBaseURI());
|
|
}
|
|
}
|
|
|
|
iter = doc.axisIterator(Axis.CHILD);
|
|
XdmNode newdoc = null;
|
|
XdmDestination destination = new XdmDestination();
|
|
//destination.setBaseURI(URI.create("http://destination.com/"));
|
|
PipelineConfiguration pipe = processor.getUnderlyingConfiguration().makePipelineConfiguration();
|
|
Receiver receiver = destination.getReceiver(pipe, new SerializationProperties());
|
|
try {
|
|
receiver.open();
|
|
receiver.startDocument(0);
|
|
|
|
FingerprintedQName wrapper = new FingerprintedQName("", "", "wrapper");
|
|
receiver.setSystemId("http://example.com/");
|
|
receiver.startElement(wrapper, doc.getUnderlyingNode().getSchemaType(), EmptyAttributeMap.getInstance(),
|
|
doc.getUnderlyingNode().getAllNamespaces(), doc.getUnderlyingNode().saveLocation(), 0);
|
|
|
|
while (iter.hasNext()) {
|
|
XdmNode node = iter.next();
|
|
if (node.getNodeKind() == XdmNodeKind.ELEMENT) {
|
|
receiver.setSystemId("http://bar.com/");
|
|
receiver.append(node.getUnderlyingNode());
|
|
} else {
|
|
receiver.append(node.getUnderlyingNode());
|
|
}
|
|
}
|
|
|
|
receiver.endElement();
|
|
receiver.endDocument();
|
|
receiver.close();
|
|
newdoc = destination.getXdmNode();
|
|
} catch (XPathException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
iter = newdoc.axisIterator(Axis.CHILD);
|
|
doc = iter.next();
|
|
|
|
System.err.println("Inserted wrapper: " + doc.getBaseURI());
|
|
iter = doc.axisIterator(Axis.CHILD);
|
|
while (iter.hasNext()) {
|
|
XdmNode child = iter.next();
|
|
if (child.getNodeKind() == XdmNodeKind.ELEMENT) {
|
|
System.err.println("Inserted E:" + child.getNodeName() + ": " + child.getBaseURI());
|
|
} else {
|
|
System.err.println("Inserted T:" + child.getStringValue() + ": " + child.getBaseURI());
|
|
}
|
|
}
|
|
}
|
|
}
|