Skip to content

Commit 337fe0b

Browse files
committed
Modify FOP configurations
1 parent 393616f commit 337fe0b

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed
Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.baeldung.java;
22

33
import java.io.BufferedOutputStream;
4+
import java.io.BufferedReader;
45
import java.io.File;
56
import java.io.FileInputStream;
67
import java.io.FileOutputStream;
8+
import java.io.FileReader;
9+
import java.io.FileWriter;
710
import java.io.IOException;
811
import java.io.InputStream;
912
import java.io.OutputStream;
@@ -13,9 +16,8 @@
1316
import javax.xml.transform.Source;
1417
import javax.xml.transform.Transformer;
1518
import javax.xml.transform.TransformerFactory;
16-
import javax.xml.transform.dom.DOMResult;
17-
import javax.xml.transform.dom.DOMSource;
1819
import javax.xml.transform.sax.SAXResult;
20+
import javax.xml.transform.stream.StreamResult;
1921
import javax.xml.transform.stream.StreamSource;
2022

2123
import org.apache.fop.apps.Fop;
@@ -25,69 +27,73 @@
2527
import org.dbdoclet.trafo.html.docbook.DocBookTransformer;
2628
import org.dbdoclet.trafo.script.Script;
2729
import org.junit.Test;
28-
import org.w3c.dom.Document;
2930

3031
public class ApacheFOPHeroldTest {
3132
private String[] inputUrls = {// @formatter:off
3233
// "http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/",
3334
// "http://www.baeldung.com/2011/10/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/",
34-
"http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/",
35-
// "http://www.baeldung.com/spring-security-basic-authentication",
35+
// "http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/",
36+
"http://www.baeldung.com/spring-security-basic-authentication",
3637
// "http://www.baeldung.com/spring-security-digest-authentication",
3738
//"http://www.baeldung.com/2011/11/20/basic-and-digest-authentication-for-a-restful-service-with-spring-security-3-1/",
3839
//"http://www.baeldung.com/spring-httpmessageconverter-rest",
3940
//"http://www.baeldung.com/2011/11/06/restful-web-service-discoverability-part-4/",
4041
//"http://www.baeldung.com/2011/11/13/rest-service-discoverability-with-spring-part-5/",
4142
//"http://www.baeldung.com/2013/01/11/etags-for-rest-with-spring/",
42-
//"http://www.baeldung.com/2012/01/18/rest-pagination-in-spring/",
43+
"http://www.baeldung.com/2012/01/18/rest-pagination-in-spring/",
44+
//"http://inprogress.baeldung.com/?p=1430",
4345
//"http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/",
4446
//"http://www.baeldung.com/rest-versioning",
4547
//"http://www.baeldung.com/2013/01/18/testing-rest-with-multiple-mime-types/"
4648
}; // @formatter:on
4749

48-
private String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl";
49-
private String output_prefix = "src/test/resources/";
50-
private String xmlFile = "src/test/resources/input.xml";
50+
private String style_file = "src/test/resources/docbook-xsl/fo/docbook.xsl";
51+
private String output_file = "src/test/resources/final_output.pdf";
52+
private String fo_file = "src/test/resources/fofile.fo";
53+
private String xmlInput = "src/test/resources/input.xml";
54+
private String xmlOutput = "src/test/resources/output.xml";
5155

5256
@Test
5357
public void whenTransformFromHeroldToPDF_thenCorrect() throws Exception {
5458
final int len = inputUrls.length;
55-
for (int i = 0; i < len; i++) {
56-
fromHTMLTOXMLUsingHerold(inputUrls[i]);
57-
final Document fo = fromXMLFileToFO();
58-
fromFODocumentToPDF(fo, output_prefix + i + ".pdf");
59+
fromHTMLTOXMLUsingHerold(inputUrls[0], false);
60+
for (int i = 1; i < len; i++) {
61+
fromHTMLTOXMLUsingHerold(inputUrls[i], true);
5962
}
63+
fixXML(xmlInput, xmlOutput);
64+
fromXMLFileToFO();
65+
fromFODocumentToPDF(output_file);
6066
}
6167

6268
// UTIL
6369

64-
private void fromHTMLTOXMLUsingHerold(final String input) throws Exception {
70+
private void fromHTMLTOXMLUsingHerold(final String input, boolean append) throws Exception {
6571
Script script;
6672
final TrafoScriptManager mgr = new TrafoScriptManager();
6773
final File profileFile = new File("src/test/resources/default.her");
6874
script = mgr.parseScript(profileFile);
6975
final DocBookTransformer transformer = new DocBookTransformer();
7076
transformer.setScript(script);
7177

72-
transformer.convert(getInputStream(input), new FileOutputStream(xmlFile));
78+
transformer.convert(getInputStream(input), new FileOutputStream(xmlInput ,append));
7379
}
7480

75-
private Document fromXMLFileToFO() throws Exception {
76-
final Source source = new StreamSource(new FileInputStream(xmlFile));
77-
final DOMResult result = new DOMResult();
78-
final Transformer transformer = createTransformer(style1);
81+
private void fromXMLFileToFO() throws Exception {
82+
final Source source = new StreamSource(new FileInputStream(xmlOutput));
83+
final StreamResult result = new StreamResult(new FileOutputStream(fo_file));
84+
final Transformer transformer = createTransformer(style_file);
7985
transformer.transform(source, result);
80-
return (Document) result.getNode();
86+
// return (Document) result.getNode();
8187
}
8288

83-
private void fromFODocumentToPDF(final Document fo, final String outputFile) throws Exception {
89+
private void fromFODocumentToPDF(final String outputFile) throws Exception {
8490
final FopFactory fopFactory = FopFactory.newInstance();
8591
final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(outputFile)));
8692

8793
final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream);
8894
final TransformerFactory factory = TransformerFactory.newInstance();
8995
final Transformer transformer = factory.newTransformer();
90-
final DOMSource src = new DOMSource(fo);
96+
final StreamSource src = new StreamSource(new File(fo_file));
9197
final Result res = new SAXResult(fop.getDefaultHandler());
9298
transformer.transform(src, res);
9399

@@ -105,5 +111,27 @@ private InputStream getInputStream(final String input) throws IOException {
105111
final URL url = new URL(input);
106112
return url.openStream();
107113
}
114+
115+
private void fixXML(String input, String output) throws IOException{
116+
BufferedReader reader = new BufferedReader(new FileReader(input));
117+
FileWriter writer = new FileWriter(output);
118+
String line = reader.readLine();
119+
int count = 0;
120+
while(line != null){
121+
if(line.contains("info>")){
122+
writer.write(line.replace("info>", "section>"));
123+
}
124+
else if(!((line.startsWith("<?xml") || line.startsWith("<article") || line.startsWith("</article")) && count > 4)){
125+
writer.write(line.replaceAll("xml:id=\"", "xml:id=\""+count));
126+
}
127+
writer.write("\n");
128+
129+
line = reader.readLine();
130+
count++;
131+
}
132+
writer.write("</article>");
133+
reader.close();
134+
writer.close();
135+
}
108136

109137
}

apache-fop/src/test/resources/default.her

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ transformation html2docbook;
22

33
section HTML {
44
encoding = "UTF-8";
5-
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']/.." ];
5+
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']/.."];
66
section-numbering-pattern = "(((\d\.)+)?\d?\.?\p{Z}*).*";
77
}
88

0 commit comments

Comments
 (0)