URI xslUri;
try {
xslUri = new URI((String) args.get(0));
} catch (URISyntaxException use) {
// Shouldn't happen, checked at compilation time
throw new FunctionCallException("First parameter of the bpws:doXslTransform isn't a valid URI!", use);
}
OXslSheet xslSheet = _oxpath.xslSheets.get(xslUri);
// Shouldn't happen, checked at compilation time
if (xslSheet == null) throw new FunctionCallException("Couldn't find the XSL sheet " + args.get(0)
+ ", process compilation or deployment was probably incomplete!");
if (!(varElmt instanceof Element)) {
throw new WrappedFaultException.JaxenFunctionException(
new FaultException(_oxpath.getOwner().constants.qnXsltInvalidSource,
"Second parameter of the bpws:doXslTransform function MUST point to a single " +
"element node."));
}
HashMap<QName, Object> parametersMap = null;
if (args.size() > 2) {
parametersMap = new HashMap<QName, Object>();
for (int idx = 2; idx < args.size(); idx+=2) {
QName keyQName = _oxpath.namespaceCtx.derefQName((String) args.get(idx));
parametersMap.put(keyQName, args.get(idx + 1));
}
}
DOMSource source = new DOMSource(varElmt);
// Using a StreamResult as a DOMResult doesn't behaves properly when the result
// of the transformation is just a string.
StringWriter writerResult = new StringWriter();
StreamResult result = new StreamResult(writerResult);
XslRuntimeUriResolver resolver = new XslRuntimeUriResolver(_oxpath);
XslTransformHandler.getInstance().cacheXSLSheet(xslUri, xslSheet.sheetBody, resolver);
try {
XslTransformHandler.getInstance().transform(xslUri, source, result, parametersMap, resolver);
} catch (Exception e) {
throw new WrappedFaultException.JaxenFunctionException(
new FaultException(_oxpath.getOwner().constants.qnSubLanguageExecutionFault,
e.toString()));
}
writerResult.flush();
String output = writerResult.toString();
// I'm not really proud of that but hey, it does the job and I don't think there's
// any other easy way.
if (output.startsWith("<?xml")) {
try {
return DOMUtils.stringToDOM(writerResult.toString());
} catch (SAXException e) {
throw new FunctionCallException(e);
} catch (IOException e) {
throw new FunctionCallException(e);
}
} else {
return output;
}
}