Catch output stream of xsl result-document

前端 未结 3 802
北恋
北恋 2021-01-13 20:23

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

3条回答
  •  情书的邮戳
    2021-01-13 21:11

    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);
        }
    }
    

    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));
    

    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

提交回复
热议问题