Saxon XSLT Transformation: How to close outputstream when failing during transformation

☆樱花仙子☆ 提交于 2020-01-22 02:33:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!