I want to do a XSLT transformation with multiple output files. There for I used "xsl:result-document". When the transformation fails, all output files should be deleted. But if the generation of a document created by "xsl:result-document" fails, my program not able to delete this document anymore. I think the reason is, that "xsl:result-document" create a different OutputStream. Does anyone know how to close all output streams?
Edit: I use Saxon 9.5 to do the transformation.
Please see below for my source code:
public void simpleTransform(String sourcePath, String xsltPath, String outputPath)
{
String resultDir=outputPath+"/filename.html";
TransformerFactory tFactory = TransformerFactory.newInstance();
StreamSource ss = new StreamSource(new File(xsltPath));
StreamResult sr = new StreamResult(new File(resultDir));
Transformer transformer = tFactory.newTransformer(ss);
try
{
transformer.transform(new StreamSource(new File(sourcePath)), sr);
System.out.println("Transformation finished!");
}
catch (TransformerException te)
{
try
{
System.out.println("Transformation failed! Trying to close Outputstreams...");
sr.getOutputStream().flush();
sr.getOutputStream().close();
transformer.reset();
System.out.println("Outputstream closed!");
try
{
FileUtils.deleteDirectory(new File(tempDirPath));
System.out.println("Files succesfully deleted!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}