}
public void run() throws SaxonApiException {
super.run();
XdmNode stylesheet = stylesheetPipe.read();
if (stylesheet == null) {
throw XProcException.dynamicError(6, step.getNode(), "No stylesheet provided.");
}
Vector<XdmNode> defaultCollection = new Vector<XdmNode> ();
while (sourcePipe.moreDocuments()) {
defaultCollection.add(sourcePipe.read());
}
XdmNode document = null;
if (defaultCollection.size() > 0) {
document = defaultCollection.firstElement();
}
String version = null;
if (getOption(_version) == null) {
XdmNode ssroot = S9apiUtils.getDocumentElement(stylesheet);
version = ssroot.getAttributeValue(new QName("","version"));
if (version == null) {
version = ssroot.getAttributeValue(new QName("http://www.w3.org/1999/XSL/Transform","version"));
}
if (version == null) {
version = "2.0"; // WTF?
}
} else {
version = getOption(_version).getString();
}
if ("3.0".equals(version) && Configuration.softwareEdition.toLowerCase().equals("he")) {
throw XProcException.stepError(38, "XSLT version '" + version + "' is not supported (Saxon PE or EE processor required).");
}
// We used to check if the XSLT version was supported, but I've removed that check.
// If it's not supported by Saxon, we'll get an error from Saxon. Otherwise, we'll
// get the results we get.
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;
String outputBaseURI = null;
RuntimeValue opt = getOption(_initial_mode);
if (opt != null) {
initialMode = opt.getQName();
}
opt = getOption(_template_name);
if (opt != null) {
templateName = opt.getQName();
}
opt = getOption(_output_base_uri);
if (opt != null) {
outputBaseURI = opt.getString();
}
Processor processor = runtime.getProcessor();
Configuration config = processor.getUnderlyingConfiguration();
runtime.getConfigurer().getSaxonConfigurer().configXSLT(config);
OutputURIResolver uriResolver = config.getOutputURIResolver();
CollectionURIResolver collectionResolver = config.getCollectionURIResolver();
UnparsedTextURIResolver unparsedTextURIResolver = runtime.getResolver();
config.setOutputURIResolver(new OutputResolver());
config.setCollectionURIResolver(new CollectionResolver(runtime, defaultCollection, collectionResolver));
XdmDestination result = null;
try {
XsltCompiler compiler = runtime.getProcessor().newXsltCompiler();
compiler.setSchemaAware(processor.isSchemaAware());
XsltExecutable exec = compiler.compile(stylesheet.asSource());
XsltTransformer transformer = exec.load();
for (QName name : params.keySet()) {
RuntimeValue v = params.get(name);
if (runtime.getAllowGeneralExpressions()) {
transformer.setParameter(name, v.getValue());
} else {
transformer.setParameter(name, new XdmAtomicValue(v.getString()));
}
}
if (document != null) {
transformer.setInitialContextNode(document);
}
transformer.setMessageListener(new CatchMessages());
result = new XdmDestination();
transformer.setDestination(result);
if (initialMode != null) {
transformer.setInitialMode(initialMode);
}
if (templateName != null) {
transformer.setInitialTemplate(templateName);
}
if (outputBaseURI != null) {
transformer.setBaseOutputURI(outputBaseURI);
// The following hack works around https://saxonica.plan.io/issues/1724
try {
result.setBaseURI(new URI(outputBaseURI));
} catch (URISyntaxException use) {
// whatever
}
}
transformer.setSchemaValidationMode(ValidationMode.DEFAULT);
transformer.getUnderlyingController().setUnparsedTextURIResolver(unparsedTextURIResolver);
transformer.transform();
} finally {
config.setOutputURIResolver(uriResolver);
config.setCollectionURIResolver(collectionResolver);
}
XdmNode xformed = result.getXdmNode();
// 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);
}
}