[xsl] How to write a stream-oriented XSLT filter?

Subject: [xsl] How to write a stream-oriented XSLT filter?
From: John English <john.foreign@xxxxxxxxx>
Date: Tue, 26 Mar 2013 16:41:30 +0200
Apologies in advance: this is not an XSLT-specific question, but I thought someone here might be able to help me anyway...

I have an XSLT filter which transforms XML output from a servlet to HTML. The filter uses a CharResponseWrapper to extract the response output as a string and then run it through the transformer:

      CharResponseWrapper wrapper =
          new CharResponseWrapper((HttpServletResponse)response);
      chain.doFilter(request,wrapper);
      s = wrapper.toString();
      if (s.length() > 0) {
        Transformer transformer = xslt.newTransformer();
        Source transformSource = new StreamSource(new StringReader(s));
        StreamResult result = new StreamResult(response.getWriter());
        transformer.transform(transformSource, result);
      }

This works fine unless the output is larger than a certain size, at which point I get an OutOfMemoryError.

The sensible way to deal with this would presumably be to connect up the response's output stream to the transformer's input source, so that I don't have to materialise all the output as a string before I start transforming it. I can't find any examples which show how I can do this; all the examples I've seen also work by materialising the output as a string before processing it.

It seems like I will need to move the transformation into the response wrapper, and maybe a separate thread to run the transformation while the response is being generated, but it all seems incredibly complicated. I'm hoping it's not as complicated as I imagine, and that someone here has already gone through all this and will be able to tell me how to proceed.

Please can anyone point me in the right direction here?

TIA,
--
John English

Current Thread