try {
transformer = tFactory.newTransformer(styleSource);
} catch (Exception e) {
logger.error("Exception from Xerces XSLT:", e);
throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
"Exception from Xerces XSLT: " + e, e);
}
// get the src to work on: if a bean named "xslt:src" is registered
// and its a Node, then use it as the source. If its not a Node, then
// if its a URL parse it, if not treat it as a file and make a URL and
// parse it and go. If no xslt:src is found, use an empty document
// (stylesheet is treated as a literal result element stylesheet)
Object srcObj = mgr.lookupBean ("xslt:src");
Object xis = null;
if (srcObj != null) {
if (srcObj instanceof Node) {
xis = new DOMSource((Node)srcObj);
} else {
try {
String mesg = "as anything";
if (srcObj instanceof Reader) {
xis = new StreamSource ((Reader) srcObj);
mesg = "as a Reader";
} else if (srcObj instanceof File) {
xis = new StreamSource ((File) srcObj);
mesg = "as a file";
} else {
String srcObjstr=srcObj.toString();
xis = new StreamSource (new StringReader(srcObjstr));
if (srcObj instanceof URL) {
mesg = "as a URL";
} else {
((StreamSource) xis).setPublicId (srcObjstr);
mesg = "as an XML string";
}
}
if (xis == null) {
throw new Exception ("Unable to get input from '" +
srcObj + "' " + mesg);
}
} catch (Exception e) {
throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
"BSF:XSLTEngine: unable to get " +
"input from '" + srcObj + "' as XML", e);
}
}
} else {
// create an empty document - real src must come into the
// stylesheet using "doc(...)" [see XSLT spec] or the stylesheet
// must be of literal result element type
xis = new StreamSource();
}
// set all declared beans as parameters.
for (int i = 0; i < declaredBeans.size (); i++) {
BSFDeclaredBean b = (BSFDeclaredBean) declaredBeans.elementAt (i);
transformer.setParameter (b.name, new XObject (b.bean));
}
// declare a "bsf" parameter which is the BSF handle so that
// the script can do BSF stuff if it wants to
transformer.setParameter ("bsf",
new XObject (new BSFFunctions (mgr, this)));
// do it
try {
DOMResult result = new DOMResult();
transformer.transform ((StreamSource) xis, result);
return new XSLTResultNode (result.getNode());
} catch (Exception e) {
throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
"exception while eval'ing XSLT script" + e, e);
}
}