Skip to content

Commit

Permalink
[feature] Added fo processor adapter for AntennaHouse Formatter. To b…
Browse files Browse the repository at this point in the history
…e extended, but works for now.

svn path=/trunk/eXist/; revision=16993
  • Loading branch information
wolfgangmm committed Aug 22, 2012
1 parent b4de5e8 commit ffda3b7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
5 changes: 2 additions & 3 deletions conf.xml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -829,17 +829,16 @@
<module uri="http://xmlcalabash.com" class="org.exist.xquery.modules.xmlcalabash.XMLCalabashModule" />
-->

<!--
<!--
XSLFO:
valid processor adapters are -
- org.exist.xquery.modules.xslfo.ApacheFopProcessorAdapter for Apache's FOP
- org.exist.xquery.modules.xslfo.RenderXXepProcessorAdapter for RenderX's XEP
- org.exist.xquery.modules.xslfo.AntennaHouseProcessorAdapter for AntennaHouse Formatter
-->
<!--
<module uri="http://exist-db.org/xquery/xslfo" class="org.exist.xquery.modules.xslfo.XSLFOModule">
<parameter name="processorAdapter" value="org.exist.xquery.modules.xslfo.ApacheFopProcessorAdapter"/>
</module>
-->

<!--
exiftool:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.exist.xquery.modules.xslfo;

import org.apache.log4j.Logger;
import org.exist.storage.DBBroker;
import org.exist.storage.serializers.Serializer;
import org.exist.util.serializer.SAXSerializer;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.NodeValue;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

import javax.xml.transform.OutputKeys;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
* Adapter for Antenna House XSL Formatter 5.0
*/
public class AntennaHouseProcessorAdapter implements ProcessorAdapter {

private final static Logger LOG = Logger.getLogger(AntennaHouseProcessorAdapter.class);

protected Class formatter;
protected Method renderMethod;
protected OutputStream out;
protected File xmlFile;
protected FileWriter xmlWriter;

@Override
public ContentHandler getContentHandler(DBBroker broker, NodeValue configFile, Properties parameters, String mimeType, OutputStream os) throws XPathException, SAXException {
this.out = os;
try {
LOG.info("Initializing AntennaHouse Formatter");
formatter = Class.forName("jp.co.antenna.XfoJavaCtl.XfoObj");
renderMethod = formatter.getMethod("render", InputStream.class, OutputStream.class, String.class);

xmlFile = File.createTempFile("xslfo", ".xml");
xmlWriter = new FileWriter(xmlFile);
Properties properties = new Properties();
properties.setProperty(OutputKeys.INDENT, "no");
FOContentHandler sax = new FOContentHandler(xmlWriter, properties);
return sax;
} catch (ClassNotFoundException e) {
throw new SAXException("Antenna Java API not found", e);
} catch (NoSuchMethodException e) {
throw new SAXException("Antenna Java API method not found", e);
} catch (IOException e) {
throw new SAXException("IO error caught while rendering FO");
}
}

@Override
public void cleanup() {
//To change body of implemented methods use File | Settings | File Templates.
}

private class FOContentHandler extends SAXSerializer {

public FOContentHandler(Writer writer, Properties outputProperties) {
super(writer, outputProperties);
}

@Override
public void startDocument() throws SAXException {
super.startDocument();
LOG.info("Started document");
}

@Override
public void endDocument() throws SAXException {
super.endDocument();
LOG.info("Transforming fo input: " + xmlFile.getAbsolutePath());

try {
xmlWriter.close();
FileInputStream is = new FileInputStream(xmlFile);

Object formatterObj = formatter.newInstance();

renderMethod.invoke(formatterObj, is, out, "@PDF");
} catch (Exception e) {
throw new SAXException(e.getMessage(), e);
}
}
}
}

0 comments on commit ffda3b7

Please sign in to comment.