Skip to content

Commit 4c73128

Browse files
committed
Add class ApacheFOPTest
1 parent 8ce12f9 commit 4c73128

1,853 files changed

Lines changed: 510663 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The DocBook XSL stylesheets are maintained by Norman Walsh,
2+
<ndw@nwalsh.com>, and members of the DocBook Project,
3+
<docbook-developers@sf.net>
4+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
To view a list of all open DocBook Project XSL stylesheet bugs:
2+
3+
http://docbook.sf.net/tracker/xsl/bugs
4+
5+
To submit a bug report against the stylesheets:
6+
7+
http://docbook.sf.net/tracker/submit/bug
8+
9+
To do a full-text search of all DocBook Project issues:
10+
11+
http://docbook.sf.net/tracker/search
12+
13+
Discussion about the DocBook Project XSL stylesheets takes place
14+
on the docbook-apps mailing list:
15+
16+
http://wiki.docbook.org/topic/DocBookAppsMailingList
17+
18+
Real-time discussion takes place on IRC:
19+
20+
http://wiki.docbook.org/topic/DocBookIrcChannel
21+
irc://irc.freenode.net/docbook
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Copyright
2+
---------
3+
Copyright (C) 1999-2007 Norman Walsh
4+
Copyright (C) 2003 Jiří Kosek
5+
Copyright (C) 2004-2007 Steve Ball
6+
Copyright (C) 2005-2008 The DocBook Project
7+
Copyright (C) 2011-2012 O'Reilly Media
8+
9+
Permission is hereby granted, free of charge, to any person
10+
obtaining a copy of this software and associated documentation
11+
files (the ``Software''), to deal in the Software without
12+
restriction, including without limitation the rights to use,
13+
copy, modify, merge, publish, distribute, sublicense, and/or
14+
sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following
16+
conditions:
17+
18+
The above copyright notice and this permission notice shall be
19+
included in all copies or substantial portions of the Software.
20+
21+
Except as contained in this notice, the names of individuals
22+
credited with contribution to this software shall not be used in
23+
advertising or otherwise to promote the sale, use or other
24+
dealings in this Software without prior written authorization
25+
from the individuals in question.
26+
27+
Any stylesheet derived from this Software that is publically
28+
distributed will be identified with a different name and the
29+
version strings in any derived Software will be changed so that
30+
no possibility of confusion between the derived package and this
31+
Software will exist.
32+
33+
Warranty
34+
--------
35+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
36+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
37+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
38+
NONINFRINGEMENT. IN NO EVENT SHALL NORMAN WALSH OR ANY OTHER
39+
CONTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
40+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
41+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42+
OTHER DEALINGS IN THE SOFTWARE.
43+
44+
Contacting the Author
45+
---------------------
46+
The DocBook XSL stylesheets are maintained by Norman Walsh,
47+
<ndw@nwalsh.com>, and members of the DocBook Project,
48+
<docbook-developers@sf.net>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
$Id: INSTALL 6145 2006-08-06 13:13:03Z xmldoc $
2+
3+
INSTALL file for the DocBook XSL stylesheets distribution
4+
5+
----------------------------------------------------------------------
6+
Case #1: Installation using a package management system
7+
----------------------------------------------------------------------
8+
If you have installed the DocBook XSL distribution using "apt-get",
9+
"yum", "urpmi", or some similar package-management front-end,
10+
then, as part of the package installation, the stylesheets have
11+
already been automatically installed in the appropriate location
12+
for your system, and your XML catalog environment has probably
13+
been updated to use that location.
14+
15+
----------------------------------------------------------------------
16+
Case #2: Installing manually
17+
----------------------------------------------------------------------
18+
If you have downloaded a docbook-xsl zip, tar.gz, or tar.bz2
19+
file, use the following steps to install it.
20+
21+
1. Move the zip, tar.gz, or tar.bz2 file to the directory where
22+
you'd like to install it (not to a temporary directory).
23+
24+
2. unzip or untar/uncompress the file
25+
26+
That will create a docbook-xsl-$VERSION directory (where
27+
$VERSION is the version number for the release).
28+
29+
The remaining steps are all OPTIONAL. They are intended to
30+
automatically update your user environment with XML Catalog
31+
information about the DocBook XSL distribution. You are NOT
32+
REQUIRED to complete these remaining steps. However, if you do
33+
not, and you want to use XML catalogs with the DocBook XSL
34+
stylesheets, you will need to manually update your XML catalog
35+
environment
36+
37+
3. Change to the docbook-xsl-$VERSION directory and execute the
38+
install.sh script:
39+
40+
./install.sh
41+
42+
That will launch an interactive installer, which will emit a
43+
series of prompts for you to respond to.
44+
45+
To instead run it non-interactively without being prompted
46+
for confirmation of the changes it makes, invoke it with the
47+
"--batch" switch, like this:
48+
49+
./install.sh --batch
50+
51+
After the process is complete, the installer will emit a
52+
message with a command you need to run in order to source
53+
your environment for use with the stylesheets.
54+
55+
4. To test that he installation has updated your environment
56+
correctly, execute the test.sh script:
57+
58+
./test.sh
59+
60+
That will test your XML catalog environment, using both the
61+
xmlcatalog application and the Apache XML Commons Resolver.
62+
63+
NOTE: The test.sh file is not created until the install.sh
64+
file is run for the first time.
65+
66+
5. (UNINSTALLING) If/when you want to uninstall the release,
67+
execute the uninstall.sh script.
68+
69+
./uninstall.sh
70+
71+
To instead run it non-interactively without being prompted
72+
for confirmation of the changes it makes, invoke it with the
73+
"--batch" switch, like this:
74+
75+
./uninstall.sh --batch
76+
77+
NOTE: The uninstall.sh file is not created until the install.sh
78+
file is run for the first time.
79+
80+
81+
----------------------------------------------------------------------
82+
Note to packagers
83+
----------------------------------------------------------------------
84+
The install.sh, .CatalogManager.properties.example, and .urilist
85+
files should not be packaged. They are useful only to users who
86+
are installing the stylesheets manually.
87+
88+
The catalog.xml file should be packaged.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# $Id: Makefile.tests 8481 2009-07-13 20:18:41Z abdelazer $
2+
#
3+
# This makefile does a "smoketest" of stylesheets for various
4+
# output formats in the DocBook XSL Stylesheets release package.
5+
# It doesn't actually check the output -- it's just useful for
6+
# confirming whether each XSLT transformation actually executes
7+
# successfully without any errors.
8+
#
9+
# To use it, run "make check" or just "make"
10+
11+
XSLTPROC=xsltproc
12+
XSLTPROC_FLAGS=
13+
14+
TESTFILE=tests/refentry.007.xml
15+
TESTFILE_NS=tests/refentry.007.ns.xml
16+
17+
NORMAL_STYLES=fo/docbook.xsl html/docbook.xsl xhtml/docbook.xsl
18+
NORMAL_PROFILE_STYLES=fo/profile-docbook.xsl html/profile-docbook.xsl xhtml/profile-docbook.xsl
19+
CHUNK_STYLES=html/chunk.xsl html/onechunk.xsl xhtml/chunk.xsl xhtml/onechunk.xsl
20+
HELP_STYLES=htmlhelp/htmlhelp.xsl javahelp/javahelp.xsl eclipse/eclipse.xsl
21+
MULTIFILE_STYLES=$(CHUNK_STYLES) $(HELP_STYLES)
22+
CHUNK_PROFILE_STYLES=html/profile-chunk.xsl html/profile-onechunk.xsl xhtml/profile-chunk.xsl xhtml/profile-onechunk.xsl
23+
HELP_PROFILE_STYLES=htmlhelp/profile-htmlhelp.xsl eclipse/profile-eclipse.xsl javahelp/profile-javahelp.xsl
24+
MULTIFILE_PROFILE_STYLES=$(CHUNK_PROFILE_STYLES) $(HELP_PROFILE_STYLES)
25+
26+
MAN_STYLE=manpages/docbook.xsl
27+
MAN_PROFILE_STYLE=manpages/profile-docbook.xsl
28+
29+
TWO_PROFILE_STYLE=profiling/profile.xsl
30+
31+
ROUNDTRIP_STYLES=roundtrip/dbk2ooo.xsl roundtrip/dbk2pages.xsl roundtrip/dbk2wordml.xsl
32+
SLIDES_STYLES=slides/html/default.xsl slides/xhtml/default.xsl slides/fo/plain.xsl
33+
WEBSITE_STYLES=website/website.xsl
34+
WEBSITE_CHUNK_STYLES=website/chunk-website.xsl
35+
36+
# chunked output gets written to TMP_OUTPUT_DIR
37+
TMP_OUTPUT_DIR=/tmp/smoketest-output/
38+
# if you don't want TMP_OUTPUT_DIR and its contents deleted, unset
39+
# SMOKETEST_CLEAN_TARGET; e.g. "make check SMOKETEST_CLEAN_TARGET=''"
40+
SMOKETEST_CLEAN_TARGET=smoketest-clean
41+
42+
check: smoketest-make-tmp-dir smoketest-normal smoketest-normal-profile smoketest-chunk smoketest-chunk-profile smoketest-man smoketest-man-profile smoketest-two-profile $(SMOKETEST_CLEAN_TARGET)
43+
44+
smoketest-make-tmp-dir:
45+
$(RM) -r $(TMP_OUTPUT_DIR)
46+
mkdir '$(TMP_OUTPUT_DIR)'
47+
48+
smoketest-normal:
49+
for stylesheet in $(NORMAL_STYLES); do \
50+
echo "$(XSLT) $(TESTFILE) $$stylesheet > /dev/null"; \
51+
$(XSLT) $(TESTFILE) $$stylesheet > /dev/null; \
52+
echo "$(XSLT) $(TESTFILE_NS) $$stylesheet > /dev/null"; \
53+
$(XSLT) $(TESTFILE_NS) $$stylesheet > /dev/null; \
54+
done
55+
56+
smoketest-normal-profile:
57+
for stylesheet in $(NORMAL_PROFILE_STYLES); do \
58+
echo "$(XSLT) $(TESTFILE) $$stylesheet > /dev/null"; \
59+
$(XSLT) $(TESTFILE) $$stylesheet > /dev/null; \
60+
echo "$(XSLT) $(TESTFILE_NS) $$stylesheet > /dev/null"; \
61+
$(XSLT) $(TESTFILE_NS) $$stylesheet > /dev/null; \
62+
done
63+
64+
smoketest-chunk:
65+
for stylesheet in $(MULTIFILE_STYLES) ; do \
66+
$(XSLT) $(TESTFILE) $$stylesheet manifest.in.base.dir=1 base.dir=$(TMP_OUTPUT_DIR) ; \
67+
$(XSLT) $(TESTFILE_NS) $$stylesheet manifest.in.base.dir=1 base.dir=$(TMP_OUTPUT_DIR) ; \
68+
done;
69+
70+
smoketest-chunk-profile:
71+
for stylesheet in $(MULTIFILE_PROFILE_STYLES) ; do \
72+
$(XSLT) $(TESTFILE) $$stylesheet manifest.in.base.dir=1 base.dir=$(TMP_OUTPUT_DIR) ; \
73+
$(XSLT) $(TESTFILE_NS) $$stylesheet manifest.in.base.dir=1 base.dir=$(TMP_OUTPUT_DIR) ; \
74+
done;
75+
76+
smoketest-man:
77+
$(XSLT) $(TESTFILE) $(MAN_STYLE) man.output.in.separate.dir=1 man.output.base.dir=$(TMP_OUTPUT_DIR) ; \
78+
$(XSLT) $(TESTFILE_NS) $(MAN_STYLE) man.output.in.separate.dir=1 man.output.base.dir=$(TMP_OUTPUT_DIR) ;
79+
80+
smoketest-man-profile:
81+
$(XSLT) $(TESTFILE) $(MAN_PROFILE_STYLE) man.output.in.separate.dir=1 man.output.base.dir=$(TMP_OUTPUT_DIR) ; \
82+
$(XSLT) $(TESTFILE_NS) $(MAN_PROFILE_STYLE) man.output.in.separate.dir=1 man.output.base.dir=$(TMP_OUTPUT_DIR) ;
83+
84+
smoketest-two-profile:
85+
$(XSLT) $(TESTFILE_NS) $(TWO_PROFILE_STYLE) > /dev/null ;
86+
87+
smoketest-clean:
88+
$(RM) -r $(TMP_OUTPUT_DIR)
89+

0 commit comments

Comments
 (0)