[英]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.