parser.setFeature("http://xml.org/sax/features/namespaces", false);
parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
parser.setProperty("http://cyberneko.org/html/properties/names/attrs", "lower");
parser.parse(new InputSource(scriptStream));
} catch(Exception e) {
final ScriptException se = new ScriptException("Error parsing script " + scriptPath);
se.initCause(e);
throw se;
}
final Document template = parser.getDocument();
// compute default rendering
final StringWriter defaultRendering = new StringWriter();
if(n!=null) {
final PrintWriter pw = new PrintWriter(defaultRendering);
htmlRenderer.render(pw, r, n, pageTitle);
pw.flush();
}
// compute currentNode values in JSON format
final StringWriter jsonData = new StringWriter();
if(n != null) {
final PrintWriter pw = new PrintWriter(jsonData);
final JsonItemWriter j = new JsonItemWriter(null);
final int maxRecursionLevels = 1;
pw.print("var currentNode=");
j.dump(n, pw, maxRecursionLevels);
pw.print(";");
pw.flush();
}
// run XSLT transform on script, passing parameter
// for our computed values
final String xslt = "/xslt/script-transform.xsl";
InputStream xslTransform = getClass().getResourceAsStream(xslt);
if(xslTransform == null) {
throw new ScriptException("XSLT transform " + xslt + " not found");
}
try {
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer t = tf.newTransformer(new StreamSource(xslTransform));
t.setParameter("pageTitle", pageTitle);
t.setParameter("slingScriptPath", fullPath(request,SLING_JS_PATH));
t.setParameter("jstScriptPath", fullPath(request, scriptPath + ".jst.js"));
t.setParameter("defaultRendering", defaultRendering.toString());
t.setParameter("jsonData", jsonData.toString());
final Result result = new StreamResult(output);
final DOMSource source = new DOMSource(template);
t.transform(source, result);
} catch (Exception e) {
final ScriptException se = new ScriptException("Error in XSLT transform for " + scriptPath);
se.initCause(e);
throw se;
} finally {
xslTransform.close();
}