问题
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();
}
}
}
回答1:
I suspect you have uncovered a bug. I've logged it here: please track it for a resolution.
https://saxonica.plan.io/issues/1857
You could work around the problem by registering your own OutputURIResolver (perhaps based on the standard one) which keeps track of all open output streams and has the ability to be called directly by the application to close them at the end.
来源:https://stackoverflow.com/questions/17998881/saxon-xslt-transformation-how-to-close-outputstream-when-failing-during-transfo