简体   繁体   中英

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

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();
    }
}
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM