Skip to content

Commit 0c02e6b

Browse files
committed
Modify FOP
1 parent 337fe0b commit 0c02e6b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

apache-fop/src/test/java/org/baeldung/java/ApacheFOPHeroldTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
import javax.xml.transform.Source;
1717
import javax.xml.transform.Transformer;
1818
import javax.xml.transform.TransformerFactory;
19+
import javax.xml.transform.dom.DOMResult;
20+
import javax.xml.transform.dom.DOMSource;
1921
import javax.xml.transform.sax.SAXResult;
20-
import javax.xml.transform.stream.StreamResult;
2122
import javax.xml.transform.stream.StreamSource;
2223

2324
import org.apache.fop.apps.Fop;
@@ -27,6 +28,7 @@
2728
import org.dbdoclet.trafo.html.docbook.DocBookTransformer;
2829
import org.dbdoclet.trafo.script.Script;
2930
import org.junit.Test;
31+
import org.w3c.dom.Document;
3032

3133
public class ApacheFOPHeroldTest {
3234
private String[] inputUrls = {// @formatter:off
@@ -49,7 +51,6 @@ public class ApacheFOPHeroldTest {
4951

5052
private String style_file = "src/test/resources/docbook-xsl/fo/docbook.xsl";
5153
private String output_file = "src/test/resources/final_output.pdf";
52-
private String fo_file = "src/test/resources/fofile.fo";
5354
private String xmlInput = "src/test/resources/input.xml";
5455
private String xmlOutput = "src/test/resources/output.xml";
5556

@@ -61,8 +62,8 @@ public void whenTransformFromHeroldToPDF_thenCorrect() throws Exception {
6162
fromHTMLTOXMLUsingHerold(inputUrls[i], true);
6263
}
6364
fixXML(xmlInput, xmlOutput);
64-
fromXMLFileToFO();
65-
fromFODocumentToPDF(output_file);
65+
final Document fo = fromXMLFileToFO();
66+
fromFODocumentToPDF(fo, output_file);
6667
}
6768

6869
// UTIL
@@ -78,22 +79,22 @@ private void fromHTMLTOXMLUsingHerold(final String input, boolean append) throws
7879
transformer.convert(getInputStream(input), new FileOutputStream(xmlInput ,append));
7980
}
8081

81-
private void fromXMLFileToFO() throws Exception {
82+
private Document fromXMLFileToFO() throws Exception {
8283
final Source source = new StreamSource(new FileInputStream(xmlOutput));
83-
final StreamResult result = new StreamResult(new FileOutputStream(fo_file));
84+
final DOMResult result = new DOMResult();
8485
final Transformer transformer = createTransformer(style_file);
8586
transformer.transform(source, result);
86-
// return (Document) result.getNode();
87+
return (Document) result.getNode();
8788
}
8889

89-
private void fromFODocumentToPDF(final String outputFile) throws Exception {
90+
private void fromFODocumentToPDF(Document fo, final String outputFile) throws Exception {
9091
final FopFactory fopFactory = FopFactory.newInstance();
9192
final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(outputFile)));
9293

9394
final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream);
9495
final TransformerFactory factory = TransformerFactory.newInstance();
9596
final Transformer transformer = factory.newTransformer();
96-
final StreamSource src = new StreamSource(new File(fo_file));
97+
final DOMSource src = new DOMSource(fo);
9798
final Result res = new SAXResult(fop.getDefaultHandler());
9899
transformer.transform(src, res);
99100

0 commit comments

Comments
 (0)