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.

  1. 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 several Converters.

      Unlike StyleSheetCache, Converter is not thread-safe. Each thread must own its Converter. However, the run method of a Converter 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 a Converter.
  2. Configure the Converter.
    1
    2
    3
    if (!converter.registerFOP(path("/opt/fop/fop"))) {
        return 1;
    }
  3. 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 the Console.

§ 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:

Therefore the requirements for running this kind of embedding are:

  1. Use system property xml.catalog.files to point to ebookc_install_dir/schema/catalog.xml or to an equivalent of this XML catalog.
  2. Stock ebookc_install_dir/schema/catalog.xml contains the following entry:
    <rewriteURI uriStartString="ebookc-home:" rewritePrefix="../" />

    This rewriteURI entry is needed to find the ebook.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 the schema/ and xsl/ 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.