title: Transforming Streaming XSLT Without a Custom Content Handler
body: <p>Take a look at this website: </p>
  
  <p><a href="http://xmpp.wordpress.com:8008/firehose.xml?type=text/plain" rel="nofollow">http://xmpp.wordpress.com:8008/firehose.xml?type=text/plain</a></p>
  
  <p>It constantly streams data.  You can transform this content using the newest version of XSLT (v3), with a command like this:</p>
  
  <pre><code>&lt;xsl:stream href="http://xmpp.wordpress.com:8008/firehose.xml?type=text/plain"&gt;
  </code></pre>
  
  <p>If I want to write some Java code to initiate the transformation (using Saxon, which has implemented xsl:stream), I can do this:</p>
  
  <pre><code>// XSL
  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer(new StreamSource(new FileInputStream(xslFile)));
  // XML
  StreamSource xmlSource = new StreamSource(new FileInputStream(xmlFile));
  // Output
  MyCustomContentHandler handler = new MyCustomContentHandler();  
  PrintStream outputPrintStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outputFile)), true);
  handler.setPrintStream(outputPrintStream);
  Result result = new SAXResult(handler);
  // Transform
  transformer.transform(xmlSource, result);
  </code></pre>
  
  <p>This works.  If you let it run for a bit, then open the output file, you’ll see data in it.  If you re-open it a bit later, you’ll see even more data.  The key to this is the custom content handler that processes the various SAX events.</p>
  
  <p>But suppose that I don’t really want a custom content handler.  Suppose I just want to keep the output of the XSLT as is.  I can modify my code like this:</p>
  
  <pre><code>// XSL
  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer(new StreamSource(new FileInputStream(xslFile)));
  // XML
  StreamSource xmlSource = new StreamSource(new FileInputStream(xmlFile));
  // Output
  TransformerHandler transformerHandler = ((SAXTransformerFactory) SAXTransformerFactory.newInstance()).newTransformerHandler();
  transformerHandler.setResult(new StreamResult(new PrintWriter(new FileOutputStream(outputFile, true), true)));
  // or this…
  //transformerHandler.setResult(new StreamResult(new FileOutputStream(outputFile)));
  // or this…
  //transformerHandler.setResult(new StreamResult(new FileWriter(outputFile)));
  ContentHandler contentHandler = (ContentHandler) transformerHandler;
  SAXResult result = new SAXResult(transformerHandler);
  // Transform
  transformer.transform(xmlSource, result);
  </code></pre>
  
  <p>The good news is that I no longer need a custom content handler, and my output now matches the output of the XSLT exactly.  The bad news is that although this code works with non-streaming XSLT, it does not work with streaming XSLT.  Despite my various attempts at setting the result (see the “or this…” statements above), nothing is written to the file.  I suspect there’s a buffering problem of some sort.</p>
  
  <p>Question:  How can I combine the best of these two together?  How can I transform a streaming XSLT without having to use a custom content handler?</p>