[英]Saxon XSLT Transformation: How to close outputstream when failing during transformation
我想对多个输出文件进行XSLT转换。 我在那里使用了“ xsl:result-document”。 转换失败时,应删除所有输出文件。 但是,如果“ xsl:result-document”创建的文档生成失败,则我的程序将无法再删除该文档。 我认为原因是“ xsl:result-document”创建了一个不同的OutputStream。 有谁知道如何关闭所有输出流?
编辑:我使用Saxon 9.5进行转换。
请参阅下面的源代码:
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();
}
}
}
我怀疑您发现了一个错误。 我已经在这里记录了:请对其进行跟踪以寻求解决方案。
https://saxonica.plan.io/issues/1857
您可以通过注册自己的OutputURIResolver(也许基于标准版本)来解决此问题,该OutputURIResolver跟踪所有打开的输出流,并能够由应用程序直接调用以最终关闭它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.