package examples.rtf2fo;

import java.io.File;

import de.vc.jfo.Document;
import de.vc.jfo.renderer.OutputFormat;
import de.vc.jfo.renderer.Renderer;
import de.vc.jfo.renderer.RendererFactory;
import de.vc.jfo.rtf.FieldListener;
import de.vc.jfo.rtf.RTFImporter;

/**
 * Abstract super class for all examples that fill in RTF files. Subclasses
 * must implement
 * <ul>
 *  <li><code>getRTFFile()</code> - Source RTF file</li>
 *  <li><code>getFOFile()</code> - Target XSL-FO file</li>
 *  <li><code>getPDFFile()</code> - Target PDF file, is only created if FOP is present</li>
 *  <li><code>getFieldListener()</code> - Creates the field listener that
 *      handels specific fields.</li>
 * </ul>
 */

public abstract class AbstractFillinExample {

  //---- Attributes ------------------------------------------------------------

  private final static String IMAGE_DIR = "docs-in/rtf2fo/images";

  //---- statics ---------------------------------------------------------------

  //---- Constructors ----------------------------------------------------------

  //---- public  ---------------------------------------------------------------

  public void run() throws Exception{

    // Load your jfo-config.xml if it isn't in classpath
    // JFOConfig.loadFrom(new File("jfo-config.xml").toURL());

    // Create RTF importer
    RTFImporter importer = new RTFImporter();
    importer.getParameters().setImagePath(IMAGE_DIR);

    // Register the field listener that replaces our FILL-IN fields
    importer.addFieldListener( getFieldListener() );

    // Do import
    Document doc = importer.importDocument( getRTFFile() );

    // Write XSL-FO document to a file
    doc.writeTo( getFOFile() );

    // Or render to any other format
    // Note: You don't have to write the XSL-FO to render the document
    OutputFormat format = OutputFormat.PDF;
    Renderer r = RendererFactory.instance().getRenderer( format );
    if (r==null)
      // If FOP is not available we cannot render... :-(
      System.err.println("No renderer found for "+format+". Is FOP in classpath?");
    else
      // Render the FO-document into a PDF-file
      doc.render( r, format, new File(getPDFFile()) );
  }


  //---- protected -------------------------------------------------------------

  /**
   * Gets the field listener that handels RTF fields. Note that jfo has
   * also an internal field processor for processing fields like
   * page, includepicture, date, toc, if etc.
   * @return Field listener.
   */
  protected abstract FieldListener getFieldListener();

  /**
   * Gets the file name of source RTF file.
   * @return RTF file name.
   */
  protected abstract String getRTFFile();

  /**
   * Gets the file name of target XSL-FO file.
   * @return XSL-FO file name.
   */
  protected abstract String getFOFile();

  /**
   * Gets the file name of target PDF file.
   * @return PDF file name.
   */
  protected abstract String getPDFFile();

  //---- package ---------------------------------------------------------------

  //---- privat ----------------------------------------------------------------

}
