简体   繁体   English

Saxon XSLT转换:转换期间失败时如何关闭输出流

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

I want to do a XSLT transformation with multiple output files. 我想对多个输出文件进行XSLT转换。 There for I used "xsl:result-document". 我在那里使用了“ 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. 但是,如果“ xsl:result-document”创建的文档生成失败,则我的程序将无法再删除该文档。 I think the reason is, that "xsl:result-document" create a different OutputStream. 我认为原因是“ xsl:result-document”创建了一个不同的OutputStream。 Does anyone know how to close all output streams? 有谁知道如何关闭所有输出流?

Edit: I use Saxon 9.5 to do the transformation. 编辑:我使用Saxon 9.5进行转换。

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 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. 您可以通过注册自己的OutputURIResolver(也许基于标准版本)来解决此问题,该OutputURIResolver跟踪所有打开的输出流,并能够由应用程序直接调用以最终关闭它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在java中运行撒克逊xslt转换 - How to run saxon xslt transformation in java 如何在使用Saxon进行xslt转换期间创建文件夹,然后在其中复制二进制数据 - How to create a folder during xslt transformation with Saxon in which to copy binary data afterwards 如何在带参数的Java中运行Saxon Xslt转换 - How do I run saxon xslt transformation in java with parameters Java / Android中以编程方式进行的Saxon XSLT转换 - Programmatically Saxon XSLT Transformation in Java/Android XSLT转换处理期间发生WrappedRuntimeException - WrappedRuntimeException during processing of an XSLT transformation 撒克逊人(Saxon)XSLT-Transformation:如何从中更改空标签的序列化 <x/> 至 <x></x> ? - Saxon XSLT-Transformation: How to change serialization of an empty tag from <x/> to <x></x>? 使用Saxon-HE在JavaFX中对xml文件进行XSLT转换 - XSLT transformation of xml file in JavaFX with Saxon-HE XML XSLT转换期间如何处理(R)符号 - How to handle (R) symbol during XML XSLT transformation 使用Apache FOP和Style Vision使用Saxon将XSLT 2.0转换为Java中的PDF - XSLT 2.0 Transformation To PDF In Java with Saxon Using Apache FOP and Style Vision XSLT在使用SAXON Java程序进行转换时无法找到SQL jar文件 - XSLT not able to find SQL jar files while Transformation using SAXON java Programme
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM