Catch output stream of xsl result-document

前端 未结 3 799
北恋
北恋 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:02

    Note that recent versions of Saxon require that the href (URI) is unique. Thus the system ID of the stream in the output resolver must also be unique.

    For example:

    1. Specify the result document href values in the stylesheet

      
      
    2. Create the output resolver

      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();
              }
              Result result = new StreamResult(zipOut);
              // Must ensure the stream is given a unique ID
              result.setSystemId(UUID.randomUUID().toString());
              return result;
          }
      }
      
    3. Attach the output resolver to the transformer

      ZipOutputURIResolver outputResolver = new ZipOutputURIResolver(outputStream);
      // Controller is the Saxon implementation of the JAXP Transformer
      ((Controller) transformer).setOutputURIResolver(outputResolver);
      

提交回复
热议问题