Appendix A. Embedding
com.xmlmind.ebook.convert.Converter
Quick and easy embedding: embed
com.xmlmind.ebookc.convert.Converter,
the Java™ class which is
used to implement the ebookc
command-line utility.
Converter
is the
object which is at the core of the ebookc
command-line utility.
Its run
method accepts the
same string arguments as the ebookc
command-line
utility.
The full source code of the Embed1
sample is
found in Embed1.java
.
- Create the
Converter
.1 2 3 4 5 6 7 8 9
StyleSheetCache cache = new StyleSheetCache(); Console console = new Console() { public void showMessage(String message, MessageType messageType) { System.err.println(message); } }; Converter converter = new Converter(cache, console);
StyleSheetCache
is a simple cache for the ebookc XSLT 2.0 stylesheets. It is a thread-safe object which is intended to be shared by severalConverter
s.Unlike
StyleSheetCache
,Converter
is not thread-safe. Each thread must own itsConverter
. However, therun
method of aConverter
may be invoked several times.Console
is a very simple interface. Implementing this interface allows to do whatever you want with the messages reported by aConverter
.
- Configure the
Converter
.1 2 3
if (!converter.registerFOP(path("/opt/fop/fop"))) { return 1; }
- There are several methods which may be used to register an
XSL-FO processor with a
Converter
. From high-level ones to low-level ones, these methods are:registerFOP
,registerXEP
,registerAHF
,registerXFC
,registerExternalFOConverter
,registerFOConverter
.
- There are several methods which may be used to register an
XSL-FO processor with a
- Invoke the
run
method.1 2 3 4 5 6 7 8
String[] args = { "-v", "-p", "pdf-outline", "yes", inFile.getPath(), outFile.getPath() }; return converter.run(args);
The
run
method returns 0 if the conversion is successful and an integer greater than 0 otherwise. When the conversion fails, errors messages are displayed on theConsole
.
§ Environment required for running this kind of embedding
Aside ".jar
" files like
ebookc.jar
, xmlresolver.jar
, saxon12.jar
,
etc, which are all listed in
ebookc_install_dir/doc/manual/embed/build.xml
(see
below), this kind of embedding also needs to access:
- The W3C XML schemas, schematrons and XML catalogs normally found in
ebookc_install_dir/schema/
. - The XSL stylesheets normally found in
ebookc_install_dir/xsl/
.
Therefore the requirements for running this kind of embedding are:
- Use system property
xml.catalog.files
to point toebookc_install_dir/schema/catalog.xml
or to an equivalent of this XML catalog. - Stock
ebookc_install_dir/schema/catalog.xml
contains the following entry:<rewriteURI uriStartString="ebookc-home:" rewritePrefix="../" />
This
rewriteURI
entry is needed to find theebook.xsd
schema and the location of the directory containing the XSL stylesheets. Make sure that this entry exists in your XML catalogs and that it points to the actual location of the directory containing both theschema/
andxsl/
subdirectories.
Compiling and executing the Embed1
sample
Compile the Embed1
sample by running ant
in
ebookc_install_dir/doc/manual/html/embed/
.
Execute
the Embed1
sample by running "ant embed1
" in
ebookc_install_dir/doc/manual/html/embed/
. This will
convert ebookc_install_dir/docsrc/manual/manual.ebook
to
ebookc_install_dir/doc/manual/html/embed/manual.pdf
,
using Apache
FOP.
Note that Embed1.java
contains “hardwired
filenames”, like "/opt/fop/fop
". This means that, without
modifications, this sample cannot be run from elsewhere than
ebookc_install_dir/doc/manual/html/embed/
and that
you'll almost certainly need to modify the source code in order to specify
the actual location of the fop
(fop.bat
)
script.