Skip to content

Commit

Permalink
Merge pull request eugenp#90 from Doha2012/master
Browse files Browse the repository at this point in the history
Add class ApacheFOPHeroldTest
  • Loading branch information
Eugen committed Dec 4, 2014
2 parents e0e6d3c + cf935dc commit 351f86c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.baeldung.java;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.xmlgraphics.util.MimeConstants;
import org.dbdoclet.trafo.TrafoScriptManager;
import org.dbdoclet.trafo.html.docbook.DocBookTransformer;
import org.dbdoclet.trafo.script.Script;
import org.junit.Test;
import org.w3c.dom.Document;

public class ApacheFOPHeroldTest {
private String[] inputUrls = { "http://inprogress.baeldung.com/?p=1430","https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html" };
private String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl";
private String output_prefix = "src/test/resources/";
private String xmlFile = "src/test/resources/input.xml";

@Test
public void whenTransformFromHeroldToPDF_thenCorrect() throws Exception{
int len = inputUrls.length;
for (int i = 0; i < len; i++) {
fromHTMLTOXMLUsingHerold(inputUrls[i]);
final Document fo = fromXMLFileToFO();
fromFODocumentToPDF(fo, output_prefix + i + ".pdf");
}
}

private void fromHTMLTOXMLUsingHerold(String input) throws Exception {
Script script;
TrafoScriptManager mgr = new TrafoScriptManager();
File profileFile = new File("src/test/resources/default.her");
script = mgr.parseScript(profileFile);
final DocBookTransformer transformer = new DocBookTransformer();
transformer.setScript(script);

transformer.convert(getInputStream(input), new FileOutputStream(xmlFile));
}

private Document fromXMLFileToFO() throws Exception {
final Source source = new StreamSource(new FileInputStream(xmlFile));
final DOMResult result = new DOMResult();
final Transformer transformer = createTransformer(style1);
transformer.transform(source, result);
return (Document) result.getNode();
}

private void fromFODocumentToPDF(final Document fo, final String outputFile) throws Exception {
final FopFactory fopFactory = FopFactory.newInstance();
final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(outputFile)));

final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream);
final TransformerFactory factory = TransformerFactory.newInstance();
final Transformer transformer = factory.newTransformer();
final DOMSource src = new DOMSource(fo);
final Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);

outStream.close();
}

private Transformer createTransformer(final String styleFile) throws Exception {
final TransformerFactory factory = TransformerFactory.newInstance();
final Transformer transformer = factory.newTransformer(new StreamSource(styleFile));

return transformer;
}

private InputStream getInputStream(String input) throws IOException {
URL url = new URL(input);
return url.openStream();
}

}

13 changes: 13 additions & 0 deletions apache-fop/src/test/resources/default.her
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
transformation html2docbook;

section HTML {
encoding = "UTF-8";
exclude = ["//*[@id='inner-wrapper']/*[position()<7]" , "//*[@class='post-meta']" , "//*[@class='entry-title']","//*[@id='respond']" ,"//*[@id='comments']","//*[@class='post-entries']","//*[@class='social']/../../h3[last()]" ,"//*[@class='social']/.." ];
section-numbering-pattern = "(((\d\.)+)?\d?\.?\p{Z}*).*";
}

section DocBook {
add-index = false;
decompose-tables = false;
encoding = "UTF-8";
}

0 comments on commit 351f86c

Please sign in to comment.