11package org .baeldung .java ;
22
33import java .io .BufferedOutputStream ;
4+ import java .io .BufferedReader ;
45import java .io .File ;
56import java .io .FileInputStream ;
67import java .io .FileOutputStream ;
8+ import java .io .FileReader ;
9+ import java .io .FileWriter ;
710import java .io .IOException ;
811import java .io .InputStream ;
912import java .io .OutputStream ;
@@ -31,56 +34,60 @@ public class ApacheFOPHeroldTest {
3134 private String [] inputUrls = {// @formatter:off
3235 // "http://www.baeldung.com/2011/10/20/bootstraping-a-web-application-with-spring-3-1-and-java-based-configuration-part-1/",
3336 // "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",
37+ // "http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/",
38+ "http://www.baeldung.com/spring-security-basic-authentication" ,
3639 // "http://www.baeldung.com/spring-security-digest-authentication",
3740 //"http://www.baeldung.com/2011/11/20/basic-and-digest-authentication-for-a-restful-service-with-spring-security-3-1/",
3841 //"http://www.baeldung.com/spring-httpmessageconverter-rest",
3942 //"http://www.baeldung.com/2011/11/06/restful-web-service-discoverability-part-4/",
4043 //"http://www.baeldung.com/2011/11/13/rest-service-discoverability-with-spring-part-5/",
4144 //"http://www.baeldung.com/2013/01/11/etags-for-rest-with-spring/",
42- //"http://www.baeldung.com/2012/01/18/rest-pagination-in-spring/",
45+ "http://www.baeldung.com/2012/01/18/rest-pagination-in-spring/" ,
46+ //"http://inprogress.baeldung.com/?p=1430",
4347 //"http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/",
4448 //"http://www.baeldung.com/rest-versioning",
4549 //"http://www.baeldung.com/2013/01/18/testing-rest-with-multiple-mime-types/"
4650 }; // @formatter:on
4751
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" ;
52+ private String style_file = "src/test/resources/docbook-xsl/fo/docbook.xsl" ;
53+ private String output_file = "src/test/resources/final_output.pdf" ;
54+ private String xmlInput = "src/test/resources/input.xml" ;
55+ private String xmlOutput = "src/test/resources/output.xml" ;
5156
5257 @ Test
5358 public void whenTransformFromHeroldToPDF_thenCorrect () throws Exception {
5459 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" );
60+ fromHTMLTOXMLUsingHerold (inputUrls [0 ], false );
61+ for (int i = 1 ; i < len ; i ++) {
62+ fromHTMLTOXMLUsingHerold (inputUrls [i ], true );
5963 }
64+ fixXML (xmlInput , xmlOutput );
65+ final Document fo = fromXMLFileToFO ();
66+ fromFODocumentToPDF (fo , output_file );
6067 }
6168
6269 // UTIL
6370
64- private void fromHTMLTOXMLUsingHerold (final String input ) throws Exception {
71+ private void fromHTMLTOXMLUsingHerold (final String input , boolean append ) throws Exception {
6572 Script script ;
6673 final TrafoScriptManager mgr = new TrafoScriptManager ();
6774 final File profileFile = new File ("src/test/resources/default.her" );
6875 script = mgr .parseScript (profileFile );
6976 final DocBookTransformer transformer = new DocBookTransformer ();
7077 transformer .setScript (script );
7178
72- transformer .convert (getInputStream (input ), new FileOutputStream (xmlFile ));
79+ transformer .convert (getInputStream (input ), new FileOutputStream (xmlInput , append ));
7380 }
7481
7582 private Document fromXMLFileToFO () throws Exception {
76- final Source source = new StreamSource (new FileInputStream (xmlFile ));
83+ final Source source = new StreamSource (new FileInputStream (xmlOutput ));
7784 final DOMResult result = new DOMResult ();
78- final Transformer transformer = createTransformer (style1 );
85+ final Transformer transformer = createTransformer (style_file );
7986 transformer .transform (source , result );
8087 return (Document ) result .getNode ();
8188 }
8289
83- private void fromFODocumentToPDF (final Document fo , final String outputFile ) throws Exception {
90+ private void fromFODocumentToPDF (Document fo , final String outputFile ) throws Exception {
8491 final FopFactory fopFactory = FopFactory .newInstance ();
8592 final OutputStream outStream = new BufferedOutputStream (new FileOutputStream (new File (outputFile )));
8693
@@ -105,5 +112,27 @@ private InputStream getInputStream(final String input) throws IOException {
105112 final URL url = new URL (input );
106113 return url .openStream ();
107114 }
115+
116+ private void fixXML (String input , String output ) throws IOException {
117+ BufferedReader reader = new BufferedReader (new FileReader (input ));
118+ FileWriter writer = new FileWriter (output );
119+ String line = reader .readLine ();
120+ int count = 0 ;
121+ while (line != null ){
122+ if (line .contains ("info>" )){
123+ writer .write (line .replace ("info>" , "section>" ));
124+ }
125+ else if (!((line .startsWith ("<?xml" ) || line .startsWith ("<article" ) || line .startsWith ("</article" )) && count > 4 )){
126+ writer .write (line .replaceAll ("xml:id=\" " , "xml:id=\" " +count ));
127+ }
128+ writer .write ("\n " );
129+
130+ line = reader .readLine ();
131+ count ++;
132+ }
133+ writer .write ("</article>" );
134+ reader .close ();
135+ writer .close ();
136+ }
108137
109138}
0 commit comments