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.RTFImporter;

//import java.util.logging.*;

/**
 * A simple example for converting RTF into XSL-FO and rendering it finally
 * into PDF using Apache's FOP.
 */
public class RTF2FO {


  private final static String RTF_FILE  = "docs-in/rtf2fo/xsl_index.rtf";
  private final static String FO_FILE  = "docs-out/rtf2fo/xsl_index.fo";
  private final static String PDF_FILE  = "docs-out/rtf2fo/xsl_index.pdf";
  private final static String IMAGE_DIR = "docs-out/rtf2fo/images";


  public static void main(String[] args) throws Exception{
    // If Jakarta Commons Logging discovers JDK 1.4 logger, following
    // example helps you configuring log output
/*
    Logger logger = Logger.getLogger("de.vc.jfo");
    logger.setLevel(Level.INFO);
    logger.setUseParentHandlers(false);
    StreamHandler h = new java.util.logging.StreamHandler(
      System.err, new Formatter(){
        public String format(LogRecord record) {
          return
              record.getLoggerName()+
              " ["+record.getLevel().toString()+"] "+
              record.getMessage()+"\n";
        }
      }
    );
    h.setLevel(Level.ALL);
    logger.addHandler(h);
*/

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

    // Do the import
    // This converts the RTF source file into an XSL-FO object tree
    Document doc = importer.importDocument( RTF_FILE );

    // Now we can store the XSL-FO
    doc.writeTo( new File(FO_FILE) );

    // Or render to any other format
    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(PDF_FILE) );
  }
}
