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
    4
    5
    6
    if (!converter.registerFOP(path("/opt/fop/fop"))) {
        return 1;
    }
    
    File xslDir = new File(path("../../../xsl"));
    System.setProperty("EBOOKC_XSL_DIR", xslDir.getAbsolutePath());
    • There are several methods which allows 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.
    • A Converter can automatically determine where to find the directory containing all the ebookc XSLT 2.0 stylesheets. It assumes that ebookc.jar is in the class path of Java™. It also assumes that ebookc.jar is contained in a lib/ directory. The xsl/ directory is assumed to be a sibling of the lib/ directory.

      However, the situation may be very different in applications other than the ebookc command-line. That's why you may need to set the EBOOKC_XSL_DIR system property to the URL or absolute file path of the xsl/ directory containing the XSLT stylesheets.

  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.

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”. This means that 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.