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 static ["DefaultFOProcessorFactory"] newInstance(File cfgFile);
public static ["DefaultFOProcessorFactory"] newInstance(InputStream cfg); public void setLogger(Logger) public Logger getLogger();
public void setSourceResolver(SourceResolver) public SourceResolver getSourceResolver() }}}
}
public class AvalonFOProcessorFactory extends AbstractLogEnabled {{{ implements Serviceable, Configurable, Initializable {
public void service(ServiceManager); public void configure(Configuration); public void initialize();
- }}}
}
public interface FOProcessor {
{{{ public void addEventListener(FOPEventListener);
- public void removeEventListener(FOPEventListener); public void process(Source, FOPResult);
- }}}
}
public interface FOPEvent extends EventObject { //todo: figure out details, maybe further refine by sublassing }
public interface FOPEventListener extends EventListener {
{{{ public void error(FOPException);
- public void warning(FOPException); public void notification(FOPEvent); public void layoutProblem(FOPEvent); //...todo: figure out details }}}
}
public interface FOProcessorHandler {{{ extends ContentHandler... {
- public void setResult(FOPResult); public FOProcessor getFOProcesor(); }}}
}
//Sample bean: For use with FOPResult.setOutputProperty() public class DocumentMetadata {
{{{ public void setAuthor(String);
- public String getAuthor(); public void setTitle(String); public String getTitle(); public void setKeywords(); public String getKeywords(); //... }}}
}
public abstract class FOPResult {
{{{ public void setOutputProperty(String, Object);
- public Object getOutputProperty(String); public void setOutputProperties(Properties); public Properties getOutputProperties(); }}}
}
/** For PDF, PS, PCL, RTF, TIFF etc. */ public class StreamFOPResult implements FOPResult {
{{{ public void setMIMEType(String);
- public String getMIMEType();
public void setOutputStream(OutputStream); public OutputStream getOutputStream(); public void setSystemID(File); public void setSystemID(String); public String getSystemID(); }}}
}
/** For SVG and AT */ public class SAXFOPResult implements FOPResult {
{{{ public void setContentHandler(ContentHandler);
public ContentHandler getContentHandler();
- }}}
}
/** For SVG and AT */ public class DOMFOPResult implements FOPResult {
{{{ public void setNode(Node);
- public Node getNode(); }}}
}
/** For embedding and AWT preview */ public class Java2DFOPResult implements FOPResult {
{{{ public Java2DHook getJava2DHook();
- }}}
}
public interface Java2DHook {
{{{ public int getPageCount();
- public Rectangle getPageSize(int pagenum); public void renderPage(int pagenum, Graphics2D);
- }}}
}
public class PrintFOPResult implements FOPResult {
{{{ public Printable getPrintable();
- public Pageable getPageable(); }}}
}
}}}</code>
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");
}
//.... }
}}}</code>