Element varElmt;
try {
if (args.get(1) instanceof List) {
List elmts = (List)args.get(1);
if (elmts.size() != 1) throw new XPathFunctionException(
new FaultException(_oxpath.getOwner().constants.qnXsltInvalidSource,
"Second parameter of the bpws:doXslTransform function MUST point to a single " +
"element node."));
varElmt = (Element) elmts.get(0);
} else {
if (args.get(1) instanceof NodeWrapper)
varElmt = (Element) ((NodeWrapper)args.get(1)).getUnderlyingNode();
else varElmt = (Element) args.get(1);
}
} catch (ClassCastException e) {
throw new XPathFunctionException(
new FaultException(_oxpath.getOwner().constants.qnXsltInvalidSource,
"Second parameter of the bpws:doXslTransform function MUST point to a single " +
"element node."));
}
URI xslUri;
try {
xslUri = new URI((String) args.get(0));
} catch (URISyntaxException use) {
// Shouldn't happen, checked at compilation time
throw new XPathFunctionException("First parameter of the bpws:doXslTransform isn't a valid URI!");
}
OXslSheet xslSheet = _oxpath.xslSheets.get(xslUri);
// Shouldn't happen, checked at compilation time
if (xslSheet == null) throw new XPathFunctionException("Couldn't find the XSL sheet " + args.get(0)
+ ", process compilation or deployment was probably incomplete!");
if (!(varElmt instanceof Element)) {
throw new XPathFunctionException(
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));
Object paramElmt;
if (args.get(idx + 1) instanceof NodeWrapper) {
Element tmpElmt = (Element) ((NodeWrapper)args.get(idx + 1)).getUnderlyingNode();
Document paramDoc = DOMUtils.newDocument();
paramDoc.appendChild(paramDoc.importNode(tmpElmt, true));
paramElmt = paramDoc;
if (__log.isDebugEnabled())
__log.debug("Passing parameter " + keyQName + " " + DOMUtils.domToString(paramDoc));
} else if (args.get(idx + 1) instanceof List) {
paramElmt = ((List) args.get(idx + 1)).get(0);
} else paramElmt = args.get(idx + 1);
parametersMap.put(keyQName, paramElmt);
}
}
if (__log.isDebugEnabled())
__log.debug("Executing XSL sheet " + args.get(0) + " on element " + DOMUtils.domToString(varElmt));
Document varDoc = DOMUtils.newDocument();
varDoc.appendChild(varDoc.importNode(varElmt, true));
DOMSource source = new DOMSource(varDoc);
// 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) {
e.printStackTrace();
throw new XPathFunctionException(
new FaultException(_oxpath.getOwner().constants.qnSubLanguageExecutionFault,
e.toString()));
}
writerResult.flush();
String output = writerResult.toString();