Back to FOPAvalonization and FOPProjectPages


Alternative new-API proposal

Author: JeremiasMaerki (jm)

<code>

public interface ["FOProcessorFactory"] {

{{{    public FOProcessor newFOProcessor(); 
    public ["FOProcessorHandler"] newFOProcessorHandler(); 

}

public class DefaultFOProcessorFactory implements FOProcessorFactory {

{{{ public static DefaultFOProcessorFactory newInstance();

}

public class AvalonFOProcessorFactory extends AbstractLogEnabled {{{ implements Serviceable, Configurable, Initializable {

}

public interface FOProcessor {

{{{ public void addEventListener(FOPEventListener);

}

public interface FOPEvent extends EventObject { //todo: figure out details, maybe further refine by sublassing }

public interface FOPEventListener extends EventListener {

{{{ public void error(FOPException);

}

public interface FOProcessorHandler {{{ extends ContentHandler... {

}

//Sample bean: For use with FOPResult.setOutputProperty() public class DocumentMetadata {

{{{ public void setAuthor(String);

}

public abstract class FOPResult {

{{{ public void setOutputProperty(String, Object);

}

/** For PDF, PS, PCL, RTF, TIFF etc. */ public class StreamFOPResult implements FOPResult {

{{{ public void setMIMEType(String);

}

/** For SVG and AT */ public class SAXFOPResult implements FOPResult {

{{{ public void setContentHandler(ContentHandler);

}

/** For SVG and AT */ public class DOMFOPResult implements FOPResult {

{{{ public void setNode(Node);

}

/** For embedding and AWT preview */ public class Java2DFOPResult implements FOPResult {

{{{ public Java2DHook getJava2DHook();

}

public interface Java2DHook {

{{{ public int getPageCount();

}

public class PrintFOPResult implements FOPResult {

{{{ public Printable getPrintable();

}

Example usage

XSL-FO to PDF with encryption

<code>

FOProcessorFactory fopFactory = ["DefaultFOProcessorFactory"].newInstance(new File("C:\userconfig.xml"));
fopFactory.setLogger(new ConsoleLogger());

FOProcessor fop = fopFactory.newFOProcessor();
Source src = new StreamSource(new File("C:\myfile.fo"));
FOPResult res = new ["StreamFOPResult"]("application/pdf", new File("C:\myfile.pdf"));

PDFEncryptionParams pdfcrypto = new ["PDFEncryptionParams"]("userpwd", "ownerpwd", false, false, false, true);
//org.apache.fop.pdf.PDFEncryptionParams: This class already exists today. Can be reused.
res.setOutputProperty("encryption", pdfcrypto);

DocumentMetadata metadata = new DocumentMetadata();
metadata.setTitle("my document");
metadata.setAuthor("Superman");
res.setOutputProperty("metadata", metadata);

fop.process(src, res);

</code>

XML to Printer

<code>

FOProcessorFactory fopFactory = ["DefaultFOProcessorFactory"].newInstance(new File("C:\userconfig.xml"));
fopFactory.setLogger(new ConsoleLogger());

TransformerFactory traxFactory = TransformerFactory.newInstance();
Transformer transformer = traxFactory.newTransformer(new StreamSource(new File("C:\cool.xsl")));

Source src = new StreamSource(new File("C:\myfile.xml"));

FOProcessorHandler handler = fopFactory.newFOProcessorHandler();
FOPResult res = new ["PrinterFOPResult"]();
handler.setResult(res);

transformer.transform(src, new SAXResult(handler));

PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPageable(res.getPageable());
pj.print();

</code>

Additional comments

- StreamFOPResult could be subclasses to PDFFOPResult providing strong typing for the parameters.

<code>

public class PDFFOPResult extends ["StreamFOPResult"] {

{{{    public void setEncryptionParams(PDFEncryptionParams params) { 
        setOutputProperty("encryption", params); 
    } 

    public ["PDFEncryptionParams"] getEncryptionsParams() { 
        return (PDFEncryptionParams)getOutputProperty("encryption"); 
    } 

    //.... 

}

FOPAvalonization/AltAPIProposalJM (last edited 2009-09-20 23:32:53 by localhost)