我想对多个输出文件进行 XSLT 转换。我在那里使用了“xsl:结果文档”。当转换失败时,应删除所有输出文件。但是,如果由“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 https://saxonica.plan.io/issues/1857
您可以通过注册自己的 OutputURIResolver(可能基于标准的)来解决该问题,该解析器跟踪所有打开的输出流,并能够由应用程序直接调用以在最后关闭它们。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)