From 337fe0b67858dd11157aa1c609da5b507267a7a7 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sun, 7 Dec 2014 20:10:00 +0200 Subject: [PATCH] Modify FOP configurations --- .../baeldung/java/ApacheFOPHeroldTest.java | 72 +++++++++++++------ apache-fop/src/test/resources/default.her | 2 +- 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/apache-fop/src/test/java/org/baeldung/java/ApacheFOPHeroldTest.java b/apache-fop/src/test/java/org/baeldung/java/ApacheFOPHeroldTest.java index c0c9f00959..4252bedb3b 100644 --- a/apache-fop/src/test/java/org/baeldung/java/ApacheFOPHeroldTest.java +++ b/apache-fop/src/test/java/org/baeldung/java/ApacheFOPHeroldTest.java @@ -1,9 +1,12 @@ package org.baeldung.java; import java.io.BufferedOutputStream; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -13,9 +16,8 @@ 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.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.fop.apps.Fop; @@ -25,43 +27,47 @@ 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 = {// @formatter:off // "http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/", // "http://www.baeldung.com/2011/10/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/", - "http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/", - // "http://www.baeldung.com/spring-security-basic-authentication", + // "http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/", + "http://www.baeldung.com/spring-security-basic-authentication", // "http://www.baeldung.com/spring-security-digest-authentication", //"http://www.baeldung.com/2011/11/20/basic-and-digest-authentication-for-a-restful-service-with-spring-security-3-1/", //"http://www.baeldung.com/spring-httpmessageconverter-rest", //"http://www.baeldung.com/2011/11/06/restful-web-service-discoverability-part-4/", //"http://www.baeldung.com/2011/11/13/rest-service-discoverability-with-spring-part-5/", //"http://www.baeldung.com/2013/01/11/etags-for-rest-with-spring/", - //"http://www.baeldung.com/2012/01/18/rest-pagination-in-spring/", + "http://www.baeldung.com/2012/01/18/rest-pagination-in-spring/", + //"http://inprogress.baeldung.com/?p=1430", //"http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/", //"http://www.baeldung.com/rest-versioning", //"http://www.baeldung.com/2013/01/18/testing-rest-with-multiple-mime-types/" }; // @formatter:on - 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"; + private String style_file = "src/test/resources/docbook-xsl/fo/docbook.xsl"; + private String output_file = "src/test/resources/final_output.pdf"; + private String fo_file = "src/test/resources/fofile.fo"; + private String xmlInput = "src/test/resources/input.xml"; + private String xmlOutput = "src/test/resources/output.xml"; @Test public void whenTransformFromHeroldToPDF_thenCorrect() throws Exception { final int len = inputUrls.length; - for (int i = 0; i < len; i++) { - fromHTMLTOXMLUsingHerold(inputUrls[i]); - final Document fo = fromXMLFileToFO(); - fromFODocumentToPDF(fo, output_prefix + i + ".pdf"); + fromHTMLTOXMLUsingHerold(inputUrls[0], false); + for (int i = 1; i < len; i++) { + fromHTMLTOXMLUsingHerold(inputUrls[i], true); } + fixXML(xmlInput, xmlOutput); + fromXMLFileToFO(); + fromFODocumentToPDF(output_file); } // UTIL - private void fromHTMLTOXMLUsingHerold(final String input) throws Exception { + private void fromHTMLTOXMLUsingHerold(final String input, boolean append) throws Exception { Script script; final TrafoScriptManager mgr = new TrafoScriptManager(); final File profileFile = new File("src/test/resources/default.her"); @@ -69,25 +75,25 @@ public class ApacheFOPHeroldTest { final DocBookTransformer transformer = new DocBookTransformer(); transformer.setScript(script); - transformer.convert(getInputStream(input), new FileOutputStream(xmlFile)); + transformer.convert(getInputStream(input), new FileOutputStream(xmlInput ,append)); } - private Document fromXMLFileToFO() throws Exception { - final Source source = new StreamSource(new FileInputStream(xmlFile)); - final DOMResult result = new DOMResult(); - final Transformer transformer = createTransformer(style1); + private void fromXMLFileToFO() throws Exception { + final Source source = new StreamSource(new FileInputStream(xmlOutput)); + final StreamResult result = new StreamResult(new FileOutputStream(fo_file)); + final Transformer transformer = createTransformer(style_file); transformer.transform(source, result); - return (Document) result.getNode(); + // return (Document) result.getNode(); } - private void fromFODocumentToPDF(final Document fo, final String outputFile) throws Exception { + private void fromFODocumentToPDF(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 StreamSource src = new StreamSource(new File(fo_file)); final Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(src, res); @@ -105,5 +111,27 @@ public class ApacheFOPHeroldTest { final URL url = new URL(input); return url.openStream(); } + + private void fixXML(String input, String output) throws IOException{ + BufferedReader reader = new BufferedReader(new FileReader(input)); + FileWriter writer = new FileWriter(output); + String line = reader.readLine(); + int count = 0; + while(line != null){ + if(line.contains("info>")){ + writer.write(line.replace("info>", "section>")); + } + else if(!((line.startsWith(" 4)){ + writer.write(line.replaceAll("xml:id=\"", "xml:id=\""+count)); + } + writer.write("\n"); + + line = reader.readLine(); + count++; + } + writer.write(""); + reader.close(); + writer.close(); + } } diff --git a/apache-fop/src/test/resources/default.her b/apache-fop/src/test/resources/default.her index 7b0f8d929d..6c1a3e63c1 100644 --- a/apache-fop/src/test/resources/default.her +++ b/apache-fop/src/test/resources/default.her @@ -2,7 +2,7 @@ transformation html2docbook; section HTML { encoding = "UTF-8"; - exclude = ["//*[@class='custom-design-100']","//*[@class='custom-design-114']", "//form","//*[@src]","//*[@id='inner-wrapper']/*[position()<7]" , "//*[@class='post-meta']" , "//*[@class='entry-title']","//*[@id='respond']" ,"//*[@id='comments']","//*[@class='post-entries']","//*[@class='social']/../../h3[last()]" ,"//*[@class='social']/.." ]; + exclude = ["//h3[contains(.,'I usually')]", "//*[@id='disqus_thread']","//head","//*[@class='custom-design-100']","//*[@class='custom-design-114']", "//form","//*[@src]","//*[@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}*).*"; }