Saxon XSLT转换:如何在转换期间失败时关闭输出流 [英] Saxon XSLT Transformation: How to close outputstream when failing during transformation

查看:179
本文介绍了Saxon XSLT转换:如何在转换期间失败时关闭输出流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用多个输出文件进行XSLT转换。因为我使用了xsl:result-document。转换失败时,应删除所有输出文件。但是如果由xsl:result-document创建的文档生成失败,我的程序将无法再删除此文档。我认为原因是,xsl:result-document创建了一个不同的OutputStream。有谁知道如何关闭所有输出流?

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?

编辑:我使用Saxon 9.5进行转换。

I use Saxon 9.5 to do the transformation.

请参阅下面的源代码:

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

您可以通过注册自己的OutputURIResolver来解决问题(可能是基于标准的一个)跟踪所有打开的输出流,并且能够被应用程序直接调用以在最后关闭它们。

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.

这篇关于Saxon XSLT转换:如何在转换期间失败时关闭输出流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆