final MemTreeBuilder builder = context.getDocumentBuilder();
final DocumentBuilderReceiver builderReceiver = new DocumentBuilderReceiver(builder, true);
final SAXResult result = new SAXResult(builderReceiver);
result.setLexicalHandler(builderReceiver); //preserve comments etc... from xslt output
handler.setResult(result);
final Receiver receiver = new ReceiverToSAX(handler);
final Serializer serializer = context.getBroker().getSerializer();
serializer.reset();
try {
serializer.setProperties(serializationProps);
serializer.setReceiver(receiver, true);
if (expandXIncludes) {
String xipath = serializationProps.getProperty(EXistOutputKeys.XINCLUDE_PATH);
if (xipath != null) {
final File f = new File(xipath);
if (!f.isAbsolute())
{xipath = new File(context.getModuleLoadPath(), xipath).getAbsolutePath();}
} else
{xipath = context.getModuleLoadPath();}
serializer.getXIncludeFilter().setModuleLoadPath(xipath);
}
serializer.toSAX(inputNode, 1, inputNode.getItemCount(), false, false);
} catch (final Exception e) {
throw new XPathException(this, "Exception while transforming node: " + e.getMessage(), e);
}
errorListener.checkForErrors();
Node next = builder.getDocument().getFirstChild();
while (next != null) {
seq.add((NodeValue) next);
next = next.getNextSibling();
}
context.popDocumentContext();
return seq;
}
}
else
{
//transform:stream-transform()
final ResponseModule myModule = (ResponseModule)context.getModule(ResponseModule.NAMESPACE_URI);
// response object is read from global variable $response
final Variable respVar = myModule.resolveVariable(ResponseModule.RESPONSE_VAR);
if(respVar == null)
{throw new XPathException(this, "No response object found in the current XQuery context.");}
if(respVar.getValue().getItemType() != Type.JAVA_OBJECT)
{throw new XPathException(this, "Variable $response is not bound to an Java object.");}
final JavaObjectValue respValue = (JavaObjectValue)
respVar.getValue().itemAt(0);
if (!"org.exist.http.servlets.HttpResponseWrapper".equals(respValue.getObject().getClass().getName()))
{throw new XPathException(this, signatures[1].toString() +
" can only be used within the EXistServlet or XQueryServlet");}
final ResponseWrapper response = (ResponseWrapper) respValue.getObject();
//setup the response correctly
final String mediaType = handler.getTransformer().getOutputProperty("media-type");
final String encoding = handler.getTransformer().getOutputProperty("encoding");
if(mediaType != null)
{
if(encoding == null)
{
response.setContentType(mediaType);
}
else
{
response.setContentType(mediaType + "; charset=" + encoding);
}
}
//do the transformation
try {
final OutputStream os = new BufferedOutputStream(response.getOutputStream());
final StreamResult result = new StreamResult(os);
handler.setResult(result);
final Serializer serializer = context.getBroker().getSerializer();
serializer.reset();
Receiver receiver = new ReceiverToSAX(handler);
try {
serializer.setProperties(serializationProps);
if (expandXIncludes) {
XIncludeFilter xinclude = new XIncludeFilter(serializer, receiver);
String xipath = serializationProps.getProperty(EXistOutputKeys.XINCLUDE_PATH);