View Javadoc

1   package org.rundeck.api.util;
2   
3   import org.apache.http.entity.ContentProducer;
4   import org.dom4j.Document;
5   import org.dom4j.io.OutputFormat;
6   import org.dom4j.io.XMLWriter;
7   
8   import java.io.IOException;
9   import java.io.OutputStream;
10  
11  /**
12   * DocumentContentProducer writes XML document to a stream
13   *
14   * @author greg
15   * @since 2014-02-27
16   */
17  public class DocumentContentProducer implements ContentProducer {
18      Document document;
19      private OutputFormat format;
20  
21      public DocumentContentProducer(final Document document, final OutputFormat format) {
22          this.document = document;
23          this.format = format;
24      }
25  
26      public DocumentContentProducer(final Document document) {
27          this.document = document;
28          format = new OutputFormat("", false, "UTF-8");
29      }
30  
31      @Override
32      public void writeTo(final OutputStream outstream) throws IOException {
33  
34          final XMLWriter xmlWriter = new XMLWriter(outstream, format);
35          xmlWriter.write(document);
36          xmlWriter.flush();
37      }
38  }