|
| 1 | +package org.baeldung.java.io; |
| 2 | + |
| 3 | +import java.io.BufferedOutputStream; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.FileNotFoundException; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.io.OutputStream; |
| 9 | + |
| 10 | +import javax.xml.transform.Result; |
| 11 | +import javax.xml.transform.Source; |
| 12 | +import javax.xml.transform.Transformer; |
| 13 | +import javax.xml.transform.TransformerFactory; |
| 14 | +import javax.xml.transform.dom.DOMResult; |
| 15 | +import javax.xml.transform.dom.DOMSource; |
| 16 | +import javax.xml.transform.sax.SAXResult; |
| 17 | +import javax.xml.transform.stream.StreamSource; |
| 18 | + |
| 19 | +import org.apache.fop.apps.Fop; |
| 20 | +import org.apache.fop.apps.FopFactory; |
| 21 | +import org.apache.xmlgraphics.util.MimeConstants; |
| 22 | +import org.dbdoclet.trafo.html.docbook.DocBookTransformer; |
| 23 | +import org.dbdoclet.trafo.script.Script; |
| 24 | +import org.junit.Test; |
| 25 | +import org.w3c.dom.Document; |
| 26 | +import org.w3c.tidy.Tidy; |
| 27 | + |
| 28 | +public class ApacheFOPTest { |
| 29 | + private String inputFile = "src/test/resources/hello.html"; |
| 30 | + private String style = "src/test/resources/xhtml2fo.xsl"; |
| 31 | + private String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl"; |
| 32 | + private String output = "src/test/resources/hello.pdf"; |
| 33 | + private String output1 = "src/test/resources/hello1.pdf"; |
| 34 | + private String output2 = "src/test/resources/hello2.pdf"; |
| 35 | + private String foFile = "src/test/resources/hello.fo"; |
| 36 | + private String xmlFile = "src/test/resources/hello.xml"; |
| 37 | + |
| 38 | + @Test |
| 39 | + public void whenTransformHTMLToPDFUsingJTidy_thenCorrect() throws Exception { |
| 40 | + final Document xhtml = fromHTMLToXHTML(); |
| 41 | + final Document fo = fromXHTMLToFO(xhtml); |
| 42 | + fromFODocumentToPDF(fo, output); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void whenTransformFromHTML2FOToPDF_thenCorrect() throws Exception { |
| 47 | + fromFOFileToPDF(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void whenTransformFromHeroldToPDF_thenCorrect() throws Exception { |
| 52 | + fromHTMLTOXMLUsingHerold(); |
| 53 | + final Document fo = fromXMLFileToFO(); |
| 54 | + fromFODocumentToPDF(fo, output2); |
| 55 | + } |
| 56 | + |
| 57 | + private Document fromHTMLToXHTML() throws FileNotFoundException { |
| 58 | + final FileInputStream reader = new FileInputStream(inputFile); |
| 59 | + final Tidy tidy = new Tidy(); |
| 60 | + final Document xml = tidy.parseDOM(reader, null); |
| 61 | + return xml; |
| 62 | + } |
| 63 | + |
| 64 | + private Document fromXHTMLToFO(final Document xhtml) throws Exception { |
| 65 | + final DOMSource source = new DOMSource(xhtml); |
| 66 | + final DOMResult result = new DOMResult(); |
| 67 | + final Transformer transformer = createTransformer(style); |
| 68 | + transformer.transform(source, result); |
| 69 | + return (Document) result.getNode(); |
| 70 | + } |
| 71 | + |
| 72 | + private void fromFODocumentToPDF(final Document fo, final String outputFile) throws Exception { |
| 73 | + final FopFactory fopFactory = FopFactory.newInstance(); |
| 74 | + final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(outputFile))); |
| 75 | + |
| 76 | + final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream); |
| 77 | + final TransformerFactory factory = TransformerFactory.newInstance(); |
| 78 | + final Transformer transformer = factory.newTransformer(); |
| 79 | + final DOMSource src = new DOMSource(fo); |
| 80 | + final Result res = new SAXResult(fop.getDefaultHandler()); |
| 81 | + transformer.transform(src, res); |
| 82 | + |
| 83 | + outStream.close(); |
| 84 | + } |
| 85 | + |
| 86 | + private Transformer createTransformer(final String styleFile) throws Exception { |
| 87 | + final TransformerFactory factory = TransformerFactory.newInstance(); |
| 88 | + final Transformer transformer = factory.newTransformer(new StreamSource(styleFile)); |
| 89 | + |
| 90 | + return transformer; |
| 91 | + } |
| 92 | + |
| 93 | + private void fromFOFileToPDF() throws Exception { |
| 94 | + final FopFactory fopFactory = FopFactory.newInstance(); |
| 95 | + final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(output1))); |
| 96 | + final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream); |
| 97 | + |
| 98 | + final TransformerFactory factory = TransformerFactory.newInstance(); |
| 99 | + final Transformer transformer = factory.newTransformer(); |
| 100 | + final Source src = new StreamSource(new FileInputStream(foFile)); |
| 101 | + final Result res = new SAXResult(fop.getDefaultHandler()); |
| 102 | + transformer.transform(src, res); |
| 103 | + |
| 104 | + outStream.close(); |
| 105 | + } |
| 106 | + |
| 107 | + private Document fromXMLFileToFO() throws Exception { |
| 108 | + final Source source = new StreamSource(new FileInputStream(xmlFile)); |
| 109 | + final DOMResult result = new DOMResult(); |
| 110 | + final Transformer transformer = createTransformer(style1); |
| 111 | + transformer.transform(source, result); |
| 112 | + return (Document) result.getNode(); |
| 113 | + } |
| 114 | + |
| 115 | + private void fromHTMLTOXMLUsingHerold() throws Exception { |
| 116 | + final Script script = new Script(); |
| 117 | + final DocBookTransformer transformer = new DocBookTransformer(); |
| 118 | + transformer.setScript(script); |
| 119 | + transformer.convert(new FileInputStream(inputFile), new FileOutputStream(xmlFile)); |
| 120 | + } |
| 121 | +} |
0 commit comments