/**
* The entry point for transforms initiate on module load,
* fetches settings from the <code>application/xslt+xml style</code> element
*/
public void doTransformation() {
Logger logger = Logger.getLogger("Xstl20Processor");
try {
NodeList<Element> scripts = getScriptsOnLoad();
String sourceURI = null;
String styleURI = null;
String initialMode = null;
String initialTemplate = null;
boolean styleElementExists = false;
for (int i=0; i<scripts.getLength(); i++) {
String type = scripts.getItem(i).getAttribute("type");
if (type.equals("application/xslt+xml")) {
styleElementExists = true;
styleURI = scripts.getItem(i).getAttribute("src");
sourceURI = scripts.getItem(i).getAttribute("data-source");
initialMode = scripts.getItem(i).getAttribute("data-initial-mode");
initialTemplate = scripts.getItem(i).getAttribute("data-initial-template");
break;
}
}
if (!styleElementExists) {
logger.info("Saxon-CE API initialised");
return;
} else if (styleURI == null)
{ throw new XPathException("No XSLT stylesheet reference found"); }
JavaScriptObject sourceDoc = null;
String absSourceURI = null;
if (sourceURI != null && sourceURI.length() != 0) {
String pageHref = Window.Location.getHref();
absSourceURI = (new URI(pageHref).resolve(sourceURI)).toString();
if (pageHref.equals(absSourceURI)) {
throw new XPathException("Cannot load XML with same URI as the host page");
}
sourceDoc = SaxonceApi.createAsyncDoc(absSourceURI); // config.buildDocument(absSourceURI);
} else if (initialTemplate == null) {
throw new XPathException("No data-source attribute or data-initial-template value found - one is required");
}
String absStyleURI = (new URI(Window.Location.getHref()).resolve(styleURI)).toString();
DocumentInfo styleDoc;
try {
styleDoc = config.buildDocument(absStyleURI);
} catch (XPathException e) {
String reportURI = (absSourceURI != null)? absSourceURI : styleURI;
throw new XPathException("Failed to load XSLT stylesheet " + reportURI + ": " + e.getMessage());
}
config.getDocumentPool().add(styleDoc, absStyleURI); // where document('') can find it
Element body = getBodyElement();
localController.setInitialMode(initialMode);
localController.setInitialTemplate(initialTemplate);
localController.setApiCommand(APIcommand.UPDATE_HTML);
localController.setTargetNode(Document.get()); // target node is the document node
renderXML(sourceDoc, styleDoc, body); // principle output is to the body element
} catch (Exception err) {
logger.log(Level.SEVERE, err.getMessage());
}
}