I think that's right -- basically, whoever opens the stream should also be responsible for closing it.  I can see how the intervening StreamResult could get in the way, making it trickier than it should be...

- Chip Whitmer

Michael Kay wrote:
This is actually quite tricky but I'll have another go at getting it right. I think that the rule should be that if the user supplies a StreamResult containing a writer or an outputStream, Saxon shouldn't close it; but if Saxon creates the outputStream (that is, if the supplied Result contained only a systemID), then it should also close it. I'm going to try adding some logic to that effect into the transformDocument() method of Controller.
Michael Kay
-----Original Message-----
From: saxon-help-admin@lists.sourceforge.net [mailto:saxon-help-admin@lists.sourceforge.net] On Behalf Of Chip Whitmer
Sent: 16 January 2004 23:05
To: saxon-help@lists.sourceforge.net
Subject: [saxon] Result file left open in Saxon 7.8 - Bug?

Running the following code with Saxon 7.8, the output stream under the StreamResult is not properly closed.  And result.getOutputStream() returns null, so I have no way to close the stream from this side.
For now, the work-around is to construct the FileOutputStream myself, pass it to the ResultStream constructor, and then directly close the stream.
The stream is closed properly with Saxon 7.4.
(I'm running WinXP, Java 1.4.1_02)
- Chip Whitmer
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class SaxonBug
 public static void main( String[] args )
     throws TransformerException
  if( args.length != 3 )
   System.out.println( "Usage: com.mpi.SaxonBug <sourceFile> <resultFile> <xslFile>" );
  File sourceFile = new File( args[0] );
  File resultFile = new File( args[1] );
  File xslFile    = new File( args[2] );
  StreamSource source = new StreamSource( sourceFile );
  StreamResult result = new StreamResult( resultFile );
  StreamSource xsl    = new StreamSource( xslFile );
  Transformer transformer = new net.sf.saxon.TransformerFactoryImpl().newTransformer( xsl );
  transformer.transform( source, result );
  if( result.getOutputStream() == null )
   System.out.println( "result.getOutputStream() is null" );
  if( resultFile.exists() && !resultFile.delete() )
   System.out.println( "ERROR: File is still open: " + resultFile );