捕获xsl结果文档的输出流 [英] Catch output stream of xsl result-document

查看:132
本文介绍了捕获xsl结果文档的输出流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种干扰写入xsl结果文档的方法,以避免将它们写入文件系统。现在我的模板正在写入临时目录,然后我压缩该目录。我想做那个whitout写到文件系统。我正在使用撒克逊处理器。提供仅使用标准Java库的解决方案。任何建议都表示赞赏。

I need a way to interfere in writting xsl result documents to avoid writting them to file system. Right now my template is writting to a temporary directory, and then i zip that directory. I want to do that whitout writting to file system. I am using saxon procesor. A solution that is using just standard java libraries is perfered. Any suggestion is appreciated.

编辑:我发现这个类为.net saxon api http://www.saxonica.com/documentation/dotnetdoc/Saxon/Api/IResultDocumentHandler.html
我需要一些与java相同的东西。

I found this class for .net saxon api http://www.saxonica.com/documentation/dotnetdoc/Saxon/Api/IResultDocumentHandler.html I need something equivalent for java.

推荐答案

你需要实现接口 net.sf.saxon.OutputURIResolver

http://www.saxonica.com/documentation/javadoc/net/sf/saxon/lib/OutputURIResolver.html

您可以根据需要在resolve方法中重定向输出。在我的例子中,这是实现类的样子。

You need to implement interface net.sf.saxon.OutputURIResolver
http://www.saxonica.com/documentation/javadoc/net/sf/saxon/lib/OutputURIResolver.html
You can redirect output in resolve method however you like. In my case this is how implemented class looks like.

public class ZipOutputURIReslover implements OutputURIResolver{

    private ZipOutputStream zipOut;

    public ZipOutputURIReslover(ZipOutputStream zipOut) {
        super();
        this.zipOut = zipOut;
    }

    public void close(Result arg0) throws TransformerException {
        try {
            zipOut.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Result resolve(String href, String base) throws TransformerException {
        try {
            zipOut.putNextEntry(new ZipEntry(href));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new StreamResult(zipOut);
    }
}

此后您需要注册 net.sf.saxon.OutputURIResolver 到trasnformer factory。

After this you need to register net.sf.saxon.OutputURIResolver to trasnformer factory.

ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream("file.zip"));
factory.setAttribute("http://saxon.sf.net/feature/outputURIResolver", new ZipOutputURIReslover(zipOut));

当您加载模板并运行转换时,所有xsl:result-documents将直接写入zipOutputStream。


答案在这里找到 http://sourceforge.net/p/saxon/discussion/94027/thread/9ee79dea/#70a9/6fef

When you load your template and run transformation all xsl:result-documents will be written directly to zipOutputStream.

Answer was found here http://sourceforge.net/p/saxon/discussion/94027/thread/9ee79dea/#70a9/6fef

这篇关于捕获xsl结果文档的输出流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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