package examples.rtf2fo;

import java.io.File;
import java.io.IOException;

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 de.vc.jfo.rtf.listeners.StylesheetDataSource;

/**
 * Example how to create a simple stylesheet from a RTF file.
 */

public class CreateStylesheet {

  private final static String RTF_FILE  = "docs-in/rtf2fo/stylesheet.rtf";
  private final static String XSL_FILE  = "docs-in/rtf2fo/stylesheet.xsl";

  public static void main(String[] args) throws Exception{

    // Create RTF importer
    RTFImporter importer = new RTFImporter();

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

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

    // Write XSLT file
    Renderer xslRenderer = RendererFactory.instance().getRenderer(OutputFormat.XSL_STYLESHEET);
    xslRenderer.render(doc, new File(XSL_FILE), OutputFormat.XSL_STYLESHEET );

  }


  /**
   * Creates a field listener that handles RTF fields of our document.
   * 
   * @return Field processor.
   * @throws IOException
   */
  private static StylesheetDataSource createFieldListener() {
  	StylesheetDataSource fp = new StylesheetDataSource();
    fp.setSelection("field1", "adress@phone");
    fp.setSelection("field2", "adress@fax");
    return fp;
  }

}
