Skip to content
Permalink
Browse files

Added support for using an XSLT 1.0 processor for XSLT 1.0 stylesheets

  • Loading branch information
ndw committed Feb 20, 2012
1 parent 5eeb55f commit 6e74e86715978ece5713d90b7ef81353fe9b1342
@@ -31,19 +31,14 @@
import com.xmlcalabash.util.S9apiUtils;
import net.sf.saxon.s9api.Axis;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XQueryCompiler;
import net.sf.saxon.s9api.XQueryEvaluator;
import net.sf.saxon.s9api.XQueryExecutable;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XdmSequenceIterator;
import org.xml.sax.InputSource;

import javax.xml.transform.sax.SAXSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -166,6 +161,7 @@ public void run(String[] args) throws SaxonApiException, IOException, URISyntaxE
if (cmd.jsonFlavor != null) {
config.jsonFlavor = cmd.jsonFlavor;
}
config.useXslt10 |= cmd.useXslt10;

debug = config.debug;

@@ -128,6 +128,8 @@ public SequenceIterator call(SequenceIterator[] arguments, XPathContext context)
value = runtime.getAllowGeneralExpressions() ? "true" : "false";
} else if ("xpointer-on-text".equals(local)) {
value = runtime.getAllowXPointerOnText() ? "true" : "false";
} else if ("use-xslt-1.0".equals(local) || "use-xslt-10".equals(local)) {
value = runtime.getUseXslt10Processor() ? "true" : "false";
} else if ("saxon-version".equals(local)) {
value = runtime.getConfiguration().getProcessor().getSaxonProductVersion();
} else if ("saxon-edition".equals(local)) {
@@ -24,7 +24,13 @@
import java.util.Hashtable;
import java.util.Vector;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;

import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.core.XProcRuntime;
@@ -38,6 +44,7 @@
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.CollectionURIResolver;
import net.sf.saxon.lib.OutputURIResolver;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.XdmDestination;
import net.sf.saxon.s9api.SaxonApiException;
@@ -51,6 +58,7 @@
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.event.Receiver;
import com.xmlcalabash.runtime.XAtomicStep;
import org.xml.sax.InputSource;

/**
*
@@ -61,7 +69,6 @@
private static final QName _template_name = new QName("", "template-name");
private static final QName _output_base_uri = new QName("", "output-base-uri");
private static final QName _version = new QName("", "version");
// FIXME: doesn't support sequence input yet!
private ReadablePipe sourcePipe = null;
private ReadablePipe stylesheetPipe = null;
private WritablePipe resultPipe = null;
@@ -143,6 +150,11 @@ public void run() throws SaxonApiException {
if ("1.0".equals(version) && defaultCollection.size() > 1) {
throw XProcException.stepError(39);
}

if ("1.0".equals(version) && runtime.getUseXslt10Processor()) {
run10(stylesheet, document);
return;
}

QName initialMode = null;
QName templateName = null;
@@ -179,9 +191,6 @@ public void run() throws SaxonApiException {
XsltExecutable exec = compiler.compile(stylesheet.asSource());
XsltTransformer transformer = exec.load();

// NDW debugging, ignore this
// transformer.getUnderlyingController().setBaseOutputURI("http://example.com/");

for (QName name : params.keySet()) {
RuntimeValue v = params.get(name);
if (runtime.getAllowGeneralExpressions()) {
@@ -229,6 +238,46 @@ public void run() throws SaxonApiException {
resultPipe.write(xformed);
}
}

public void run10(XdmNode stylesheet, XdmNode document) {
try {
InputSource is = S9apiUtils.xdmToInputSource(runtime, stylesheet);

TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer(new SAXSource(is));

transformer.setURIResolver(runtime.getResolver());

for (QName name : params.keySet()) {
RuntimeValue v = params.get(name);
transformer.setParameter(name.getClarkName(), v.getString());
}

DOMResult result = new DOMResult();
is = S9apiUtils.xdmToInputSource(runtime, document);
transformer.transform(new SAXSource(is), result);

DocumentBuilder xdmBuilder = runtime.getConfiguration().getProcessor().newDocumentBuilder();
XdmNode xformed = xdmBuilder.build(new DOMSource(result.getNode()));

// Can be null when nothing is written to the principle result tree...
if (xformed != null) {
if (document != null
&& (xformed.getBaseURI() == null
|| "".equals(xformed.getBaseURI().toASCIIString()))) {
String sysId = document.getBaseURI().toASCIIString();
xformed.getUnderlyingNode().setSystemId(sysId);
}
resultPipe.write(xformed);
}
} catch (SaxonApiException sae) {
throw new XProcException(sae);
} catch (TransformerConfigurationException tce) {
throw new XProcException(tce);
} catch (TransformerException te) {
throw new XProcException(te);
}
}

class OutputResolver implements OutputURIResolver {
public OutputResolver() {
@@ -52,6 +52,7 @@
public boolean allowXPointerOnText = false;
public boolean transparentJSON = false;
public String jsonFlavor = null;
public boolean useXslt10 = false;

private String[] args = null;
private int argpos = 0;
@@ -177,6 +178,8 @@ public void parse(String[] args) {
extensionValues = true;
} else if ("xpointer-on-text".equals(ext)) {
allowXPointerOnText = true;
} else if ("use-xslt-1.0".equals(ext) || "use-xslt-10".equals(ext)) {
useXslt10 = true;
} else if ("transparent-json".equals(ext)) {
transparentJSON = true;
} else if (ext.startsWith("json-flavor=")) {

0 comments on commit 6e74e86

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