public void run() throws SaxonApiException {
super.run();
String cssClass = runtime.getConfiguration().cssProcessor;
if (cssClass == null) {
throw new XProcException("No CSS processor class defined");
}
final CssProcessor provider;
try {
provider = (CssProcessor) Class.forName(cssClass).newInstance();
provider.initialize(runtime,step,options);
} catch (Exception e) {
logger.debug(e.getMessage(), e);
throw new XProcException(step.getNode(), "Failed to instantiate CSS provider");
}
while (css.moreDocuments()) {
XdmNode style = css.read();
provider.addStylesheet(style);
}
final String contentType;
if (getOption(_content_type) != null) {
contentType = getOption(_content_type).getString();
} else {
contentType = "application/pdf";
}
String href = getOption(_href).getString();
String base = getOption(_href).getBaseURI().toASCIIString();
try {
DataStore store = runtime.getDataStore();
URI id = store.writeEntry(href, base, contentType, new DataWriter() {
public void store(OutputStream out) throws IOException {
try {
provider.format(source.read(),out,contentType);
} catch(SaxonApiException e) {
throw new IOException(e);
}
}
});
TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(step.getNode().getBaseURI());
tree.addStartElement(XProcConstants.c_result);
tree.startContent();
tree.addText(id.toASCIIString());
tree.addEndElement();
tree.endDocument();
result.write(tree.getResult());
} catch (XProcException e) {
throw e;
} catch (Exception e) {
if (e.getCause() instanceof SaxonApiException) {
throw (SaxonApiException) e.getCause();
}
throw new XProcException(step.getNode(), "Failed to style with CSS document", e);
}
}