This page lists some example code that may get you started. This page covers just a Java code that tames this beast; you may want to look at GENE runner for a command-line based GENE invoker.
What to do first? Of course, load an XML (we are using samples/sample-docbook.xml here)
final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); builderFactory.setExpandEntityReferences(false); final DocumentBuilder builder = builderFactory.newDocumentBuilder(); final Document doc = builder.parse("sample-docbook.xml"); final Element e = doc.getDocumentElement(); final SplittedDocHolder splitted = new SplittedDocHolder(e, SplittedDocHolder.PI_GENEREF_TARGET, null);
Before we try to use GENE, we'll need it to initialize properly. The document contains two namespaces: DocBook and MathML and we want it to produce a PDF file. We need to register three plugins:
InstanceProvider.getInstance().registerCoordinatorFactory( new sk.baka.xml.gene.fop.CoordinatorFactoryImpl()); InstanceProvider.getInstance().registerExporterFactory( new sk.baka.xml.gene.fop.ExporterFactoryImpl()); InstanceProvider.getInstance().registerExporterFactory( new sk.baka.xml.gene.mathml.ExporterFactoryImpl()); InstanceProvider.getInstance().registerExporterFactory( new sk.baka.xml.gene.docbook.ExporterFactoryImpl());
Now we need to get an instance of coordinator that is able to produce PDF. We may list available coordinators, select one and instantiate it (use InstanceProvider for this; you may get list of available coordinators using the GENE runner), however we'll demonstrate a different approach here:
final Set<CoordinatorInfo> infos = InstanceProvider.getInstance() .findCoordinator(null, org.apache.fop.apps.MimeConstants.MIME_PDF); final CoordinatorInfo coordinatorInfo = infos.iterator().next(); final ICoordinator coordinator = coordinatorInfo.newCoordinator();
Now it would be nice if GENE would compute the exporter order and the export graph itself, wouldn't it?
final ExportGraphBuilder graphBuilder = coordinator .getGraphBuilder(); final ExportGraph graph = graphBuilder.newGraph(ExportGraphBuilder .getDocumentNamespaces(splitted), coordinatorInfo, true);
Now you may play with the graph a little if you want, insert and remove nodes etc. When you're done, let's proceed further.
final TransformGraph transformGraph = graphBuilder .toTransformGraph(graph, coordinatorInfo, null); ExportGraphBuilder.printGraph(transformGraph, System.out);
This will construct and print the transform graph, an unambiguous representation of the exporter graph. For our example it should print this:
Graph | |--- Node hash: 1bbd23f Exporter: MathMLToImageExporter | MathML to Image Exporter | Priority: 0 Source namespace: {http://www.w3.org/1998/Math/MathML} [DOM] | exports {http://www.uniba.sk/euromath/holder/image} in these kinds: [OBJECT] | | | \--- Node hash: 3c0007 Exporter: ImageToPngExporter | ImageRef to PNG exporter | Priority: 0 Source namespace: {http://www.uniba.sk/euromath/holder/image} [OBJECT] | exports {http://www.uniba.sk/euromath/holder/png} in these kinds: [STREAM] | | | \--- sent to coordinator | \--- Node hash: 125fefa Exporter: DocbookToFOExporter DocBook to XSL-FO exporter Priority: 0 Source namespace: {http://docbook.org/ns/docbook} [SAX] exports {http://www.w3.org/1999/XSL/Format} in these kinds: [DOM, SAX] | \--- sent to coordinator
It means that for MathML the MathML->Image exporter will be used, followed by the Image->PNG exporter and its output directed to the coordinator (normally a Image->SVG exporter would have been chosen, unfortunately FOP0.92beta has some bugs that prevents it to cooperate with Batik1.6, hence SVG support was temporarily disabled). For DocBook, the DocBook->FO exporter is used and the result is directed to the coordinator. Finally, let's execute the transformation:
final ExportController controller = new ExportController(new File( "sample-docbook.xml"), splitted, transformGraph, coordinator); controller.export(new File("sample-docbook.pdf"));
Oh yeah, a fresh PDF straight from the oven :) You may note that