Chapter 12. High-level method: embedding com.xmlmind.ditac.convert.Converter

Quick and easy embedding: embed com.xmlmind.ditac.convert.Converter, the Java™ class which is used to implement the ditac command-line utility.

Converter Opens in new window is the object which is at the core of the ditac command-line utility. Its run Opens in new window method accepts the same string arguments as the ditac command-line utility.
The full source code of the Embed1 sample is found in Opens in new window.
  1. Create the Converter.
    StyleSheetCache cache = new StyleSheetCache();
    Console console = new Console() {
        public void showMessage(String message, MessageType messageType) {
    Converter converter = new Converter(cache, console);
    • StyleSheetCache Opens in new window is a simple cache for the ditac 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 Opens in new window 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.
    if (!converter.registerFOP(path("/opt/fop/fop"))) {
        return 1;
    File xslDir = new File(path("../../../xsl"));
    System.setProperty("DITAC_XSL_DIR", xslDir.getAbsolutePath());
    • A Converter can automatically determine where to find the directory containing all the ditac XSLT 2.0 stylesheets. It assumes that ditac.jar is in the class path of Java™. It also assumes that ditac.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 ditac command-line. That's why you may need to set the DITAC_XSL_DIR system property to the absolute path of the xsl/ directory containing the XSLT stylesheets.
  3. Invoke the run method.
    String[] args = {
        "-p", "number", "all",
    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 ditac_install_dir/doc/manual/embed/.
Execute the Embed1 sample by running ant embed1 in ditac_install_dir/doc/manual/embed/. This will convert ditac_install_dir/docsrc/manual/manual.ditamap to ditac_install_dir/doc/manual/embed/manual.pdf, using Apache FOP.
Note that contains ``hardwired filenames''. This means that this sample cannot be run from elsewhere than ditac_install_dir/doc/manual/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.