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: [] On Behalf Of Chip Whitmer
Sent: 16 January 2004 23:05
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 javax.xml.transform.*;
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 );