First attempt at extension using Antenna House Formatter.
authorTony Graham
Mon, 02 Dec 2013 13:47:11 +0000
changeset 16 2fe56172c8cf
parent 15 723bc9f1fc42
child 17 30e136c75630
First attempt at extension using Antenna House Formatter.
.hgignore
FOPRunXSLTExt/build-nonb.xml
FOPRunXSLTExt/examples/example3_saxon9_ahf.xsl
FOPRunXSLTExt/examples/formatting.xsl
FOPRunXSLTExt/examples/runsaxon9he_ahf61.sh.in
FOPRunXSLTExt/nbproject/private/private.properties
FOPRunXSLTExt/nbproject/project.properties
FOPRunXSLTExt/properties.xml
FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/ahf/saxon/RunAHFExtInitializer.java
FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/ahf/saxon/RunAHFSaxon.java
--- a/.hgignore	Mon Dec 02 13:34:18 2013 +0000
+++ b/.hgignore	Mon Dec 02 13:47:11 2013 +0000
@@ -1,3 +1,7 @@
 FOPRunXSLTExt/build
 FOPRunXSLTExt/dist
+syntax: glob
+FOPRunXSLTExt/examples/*.sh
+FOPRunXSLTExt/examples/out.*
+FOPRunXSLTExt/examples/*.fo
 FOPRunXSLTExt/examples/out
--- a/FOPRunXSLTExt/build-nonb.xml	Mon Dec 02 13:34:18 2013 +0000
+++ b/FOPRunXSLTExt/build-nonb.xml	Mon Dec 02 13:47:11 2013 +0000
@@ -27,8 +27,13 @@
       <filterset id="jars">
 	<filter token="file.reference.fop.jar" value="${file.reference.fop.jar}"/>
 	<filter token="file.reference.saxon9he.jar" value="${file.reference.saxon9he.jar}"/>
+	<filter token="file.reference.ahf.jar" value="${file.reference.XfoJavaCtl.jar}"/>
 	<filter token="FOPRunXSLTExt.jar" value="${FOPRunXSLTExt.jar}"/>
       </filterset>
+      <copy file="examples/runsaxon9he_ahf61.sh.in" tofile="examples/runsaxon9he_ahf61.sh">
+	<filterset refid="jars"/>
+      </copy>
+      <chmod file="examples/runsaxon9he_ahf61.sh" perm="ugo+rx"/>
       <copy file="examples/runsaxon9he_fop10.sh.in" tofile="examples/runsaxon9he_fop10.sh">
 	<filterset refid="jars"/>
       </copy>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FOPRunXSLTExt/examples/example3_saxon9_ahf.xsl	Mon Dec 02 13:47:11 2013 +0000
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- ============================================================= -->
+<!-- FOPRunXSLTExt Saxon example 3                                 -->
+<!--                                                               -->
+<!-- See http://www.w3.org/community/ppl/wiki/FOPRunXSLTExt        -->
+<!--                                                               -->
+<!-- Requires Saxon 9.4 or later and FOP 1.0                       -->
+<!--                                                               -->
+<!-- Produced by the Print and Page Layout Community Group @ W3C   -->
+<!-- ============================================================= -->
+<xsl:stylesheet
+    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+    version="2.0"
+    xmlns:fo="http://www.w3.org/1999/XSL/Format"
+    xmlns:xs="http://www.w3.org/2001/XMLSchema"
+    xmlns:ppl="http://org.w3c.ppl.xslt/saxon-extension"
+    exclude-result-prefixes="xs ppl">
+
+<!-- Common templates for formatting FOPRunXSLTExt examples -->
+<xsl:import href="formatting.xsl" />
+
+<xsl:key name="boxes" match="box" use="true()" />
+
+<xsl:key name="blocks" match="block[exists(@prod-id)]" use="@prod-id" />
+
+<!-- Where to write the output files. -->
+<xsl:param name="dest_dir" select="out" as="xs:string"/>
+<xsl:param name="area_tree_filename" />
+
+<!-- Initial template -->
+<xsl:template name="main">
+  <!-- Save the FO tree in a variable. -->
+  <xsl:variable name="fo_tree">
+    <xsl:apply-templates select="/" />
+  </xsl:variable>
+
+  <xsl:variable name="area_tree_file"
+		select="concat($dest_dir, '/', $area_tree_filename)" />
+
+  <xsl:message>Area tree filename = <xsl:value-of select="$area_tree_file" /></xsl:message>
+
+  <xsl:variable
+      name="area-tree"
+      select="ppl:area-tree($fo_tree)"
+      as="document-node()" />
+  <xsl:message select="$area-tree"/>
+
+  <xsl:variable name="overrides">
+    <overrides>
+      <xsl:for-each select="key('boxes', true())">
+	<xsl:variable name="id" select="@id" as="xs:string" />
+	<xsl:variable name="block"
+		      select="key('blocks', $id, $area-tree)[1]" />
+	<xsl:if test="$block/@ipd > $block/ancestor::flow/@ipd">
+	  <override id="{$id}" rotate="yes" />
+	</xsl:if>
+      </xsl:for-each>
+    </overrides>
+  </xsl:variable>
+
+  <xsl:apply-templates select="/">
+    <xsl:with-param name="overrides" select="$overrides" as="document-node()" tunnel="yes" />
+  </xsl:apply-templates>
+</xsl:template>
+
+</xsl:stylesheet>
\ No newline at end of file
--- a/FOPRunXSLTExt/examples/formatting.xsl	Mon Dec 02 13:34:18 2013 +0000
+++ b/FOPRunXSLTExt/examples/formatting.xsl	Mon Dec 02 13:47:11 2013 +0000
@@ -22,7 +22,7 @@
 </xsl:variable>
 
 <xsl:template match="/">
-  <fo:root>
+  <fo:root font-family="verdana, sans-serif" font-size="12pt">
     <fo:layout-master-set>
       <fo:simple-page-master master-name="test-page">
 	<fo:region-body margin="1in"/>
@@ -31,15 +31,14 @@
 
     <fo:page-sequence master-reference="test-page">
       <fo:flow flow-name="xsl-region-body">
-	<xsl:apply-templates select="example" />
+	<xsl:apply-templates/>
       </fo:flow>
     </fo:page-sequence>
   </fo:root>
 </xsl:template>
 
 <xsl:template match="header">
-  <fo:block font-size="14pt" font-family="verdana, sans-serif"
-	    color="red" space-before="5mm" space-after="5mm">
+  <fo:block font-size="14pt" color="red" space-before="5mm" space-after="5mm">
     <xsl:apply-templates/>
   </fo:block>
 </xsl:template>
@@ -52,13 +51,16 @@
     <xsl:if test="key('overrides', @id, $overrides)/@rotate = 'yes'">
       <xsl:attribute name="reference-orientation" select="'270'" />
     </xsl:if>
+    <xsl:if test="key('overrides', @id, $overrides)/@font-size">
+      <xsl:attribute name="font-size"
+                     select="concat(key('overrides', @id, $overrides)/@font-size, 'pt')" />
+    </xsl:if>
     <xsl:apply-templates/>
   </fo:block-container>
 </xsl:template>
 
 <xsl:template match="paragraph">
-  <fo:block id="{@id}" text-indent="5mm"
-	    font-family="verdana, sans-serif" font-size="12pt">
+  <fo:block id="{@id}" text-indent="5mm" space-before="3pt">
     <xsl:apply-templates/>
   </fo:block>
 </xsl:template>
@@ -69,4 +71,40 @@
   </fo:instream-foreign-object>
 </xsl:template>
 
-</xsl:stylesheet>
\ No newline at end of file
+<xsl:template match="list">
+  <xsl:param name="overrides" select="$overrides" as="document-node()" tunnel="yes"/>
+  <fo:list-block
+      provisional-distance-between-starts="{if (@label-width = 'narrow')
+                                              then '10mm'
+                                            else '35mm'}"
+      provisional-label-separation="4pt"
+      space-after="3pt"
+      id="{@id}">
+    <!-- If we know the exact label width, set the provisional
+         distance between starts to the width plus the label
+         separation. -->
+    <xsl:if test="exists(key('overrides', @id, $overrides))">
+      <xsl:attribute
+          name="provisional-distance-between-starts"
+          select="concat(key('overrides', @id, $overrides)/@label-width, ' + 4pt')" />
+    </xsl:if>
+    <xsl:apply-templates/>
+  </fo:list-block>
+</xsl:template>
+
+<xsl:template match="item">
+  <fo:list-item space-before="3pt">
+    <fo:list-item-label end-indent="label-end()">
+      <fo:block color="red">
+        <xsl:apply-templates select="@label"/>
+      </fo:block>
+    </fo:list-item-label>
+    <fo:list-item-body start-indent="body-start()">
+      <fo:block>
+        <xsl:apply-templates/>
+      </fo:block>
+    </fo:list-item-body>
+  </fo:list-item>
+</xsl:template>
+
+</xsl:stylesheet>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FOPRunXSLTExt/examples/runsaxon9he_ahf61.sh.in	Mon Dec 02 13:47:11 2013 +0000
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+AHF61_64_HOME="/usr/AHFormatterV61_64"
+export AHF61_64_HOME
+
+AHF61_64_LIB_FOLDER=${AHF61_64_HOME}/lib
+AHF61_64_BIN_FOLDER=${AHF61_64_HOME}/bin
+AHF61_64_ETC_FOLDER=${AHF61_64_HOME}/etc
+AHF61_64_SDATA_FOLDER=${AHF61_64_HOME}/sdata
+
+LD_LIBRARY_PATH=${AHF61_64_LIB_FOLDER}:${LD_LIBRARY_PATH}
+export LD_LIBRARY_PATH
+
+AHF61_64_LIC_PATH=${AHF61_64_ETC_FOLDER}
+export AHF61_64_LIC_PATH
+
+AHF61_64_HYPDIC_PATH=${AHF61_64_ETC_FOLDER}/hyphenation
+export AHF61_64_HYPDIC_PATH
+
+AHF61_64_DMC_TBLPATH=${AHF61_64_SDATA_FOLDER}/base2
+export AHF61_64_DMC_TBLPATH
+
+AHF61_64_DEFAULT_HTML_CSS=${AHF61_64_ETC_FOLDER}/html.css
+export AHF61_64_DEFAULT_HTML_CSS
+
+AHF61_64_FONT_CONFIGFILE=${AHF61_64_ETC_FOLDER}/font-config.xml
+export AHF61_64_FONT_CONFIGFILE
+
+SAXON=@file.reference.saxon9he.jar@
+AHF=@file.reference.ahf.jar@
+CP=$SAXON:@file.reference.ahf.jar@
+CP=$CP:@FOPRunXSLTExt.jar@
+
+java -cp $CP net.sf.saxon.Transform -init:org.w3c.ppl.xslt.ext.ahf.saxon.RunAHFExtInitializer -s:$1 -xsl:$2 -o:$3 -it:main dest_dir=$4 area_tree_filename=$5
\ No newline at end of file
--- a/FOPRunXSLTExt/nbproject/private/private.properties	Mon Dec 02 13:34:18 2013 +0000
+++ b/FOPRunXSLTExt/nbproject/private/private.properties	Mon Dec 02 13:47:11 2013 +0000
@@ -3,4 +3,4 @@
 do.jar=true
 javac.debug=true
 javadoc.preview=true
-user.properties.file=/home/arvedhs/.netbeans/7.2.1/build.properties
+user.properties.file=/home/tgraham/.netbeans/7.0/build.properties
--- a/FOPRunXSLTExt/nbproject/project.properties	Mon Dec 02 13:34:18 2013 +0000
+++ b/FOPRunXSLTExt/nbproject/project.properties	Mon Dec 02 13:47:11 2013 +0000
@@ -1,6 +1,5 @@
 annotation.processing.enabled=true
 annotation.processing.enabled.in.editor=false
-annotation.processing.processors.list=
 annotation.processing.run.all.processors=true
 annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
 application.title=FOPRunXSLTExt
@@ -28,36 +27,64 @@
 endorsed.classpath=
 excludes=
 file.reference.avalon-framework-4.2.0.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/avalon-framework-4.2.0.jar
+file.reference.avalon-framework.jar=/usr/share/java/avalon-framework.jar
 file.reference.batik-all-1.7.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/batik-all-1.7.jar
+file.reference.batik-all.jar=/usr/share/java/batik-all.jar
 file.reference.commons-io-1.3.1.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/commons-io-1.3.1.jar
+file.reference.commons-io.jar=/usr/share/java/commons-io.jar
 file.reference.commons-logging-1.0.4.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/commons-logging-1.0.4.jar
+file.reference.commons-logging.jar=/usr/share/java/commons-logging.jar
 file.reference.fop.jar=/home/arvedhs/Development/XSLFO/fop-trunk/build/fop.jar
+file.reference.fop.jar-1=/usr/share/java/fop.jar
 file.reference.saxon9he.jar=/home/arvedhs/Development/JavaLibs/saxon9he.jar
+file.reference.saxon9he.jar-1=/usr/local/share/saxon/saxon9he.jar
 file.reference.serializer-2.7.0.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/serializer-2.7.0.jar
+file.reference.serializer.jar=/usr/share/java/serializer.jar
 file.reference.servlet-2.2.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/servlet-2.2.jar
+file.reference.servlet-api-2.5.jar=/usr/share/java/servlet-api-2.5.jar
 file.reference.xalan-2.7.0.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/xalan-2.7.0.jar
+file.reference.xalan2.jar=/usr/share/java/xalan2.jar
 file.reference.xercesImpl-2.7.1.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/xercesImpl-2.7.1.jar
+file.reference.xercesImpl.jar=/usr/share/java/xercesImpl.jar
+file.reference.XfoJavaCtl.jar=/usr/AHFormatterV61_64/lib/XfoJavaCtl.jar
 file.reference.xml-apis-1.3.04.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/xml-apis-1.3.04.jar
 file.reference.xml-apis-ext-1.3.04.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/xml-apis-ext-1.3.04.jar
+file.reference.xml-apis-ext.jar=/usr/share/java/xml-apis-ext.jar
+file.reference.xml-apis.jar=/usr/share/java/xml-apis.jar
 file.reference.xmlgraphics-commons-svn-trunk.jar=/home/arvedhs/Development/XSLFO/fop-trunk/lib/xmlgraphics-commons-svn-trunk.jar
+file.reference.xmlgraphics-commons.jar=/usr/share/java/xmlgraphics-commons.jar
 includes=**
 jar.archive.disabled=${jnlp.enabled}
 jar.compress=false
 jar.index=${jnlp.enabled}
 javac.classpath=\
+    ${file.reference.avalon-framework.jar}:\
     ${file.reference.avalon-framework-4.2.0.jar}:\
+    ${file.reference.batik-all.jar}:\
     ${file.reference.batik-all-1.7.jar}:\
+    ${file.reference.commons-io.jar}:\
     ${file.reference.commons-io-1.3.1.jar}:\
+    ${file.reference.commons-logging.jar}:\
     ${file.reference.commons-logging-1.0.4.jar}:\
+    ${file.reference.serializer.jar}:\
     ${file.reference.serializer-2.7.0.jar}:\
+    ${file.reference.servlet-api-2.5.jar}:\
     ${file.reference.servlet-2.2.jar}:\
+    ${file.reference.xalan2.jar}:\
     ${file.reference.xalan-2.7.0.jar}:\
+    ${file.reference.xercesImpl.jar}:\
     ${file.reference.xercesImpl-2.7.1.jar}:\
+    ${file.reference.xml-apis.jar}:\
     ${file.reference.xml-apis-1.3.04.jar}:\
+    ${file.reference.xml-apis-ext.jar}:\
     ${file.reference.xml-apis-ext-1.3.04.jar}:\
     ${file.reference.xmlgraphics-commons-svn-trunk.jar}:\
+    ${file.reference.xmlgraphics-commons.jar}:\
+    ${file.reference.fop.jar-1}:\
     ${file.reference.fop.jar}:\
-    ${file.reference.saxon9he.jar}
+    ${file.reference.saxon9he.jar}:\
+    ${file.reference.saxon9he.jar-1}:\
+    ${file.reference.XfoJavaCtl.jar}
 # Space-separated list of extra javac options
 javac.compilerargs=
 javac.deprecation=false
--- a/FOPRunXSLTExt/properties.xml	Mon Dec 02 13:34:18 2013 +0000
+++ b/FOPRunXSLTExt/properties.xml	Mon Dec 02 13:47:11 2013 +0000
@@ -4,4 +4,5 @@
 
 <entry key="prebuilt.lib">/usr/share/java</entry>
 <entry key="saxon.lib">/usr/local/share/saxon</entry>
+<entry key="ahf.lib">/usr/AHFormatterV61_64</entry>
 </properties>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/ahf/saxon/RunAHFExtInitializer.java	Mon Dec 02 13:47:11 2013 +0000
@@ -0,0 +1,18 @@
+package org.w3c.ppl.xslt.ext.ahf.saxon;
+
+import javax.xml.transform.TransformerException;
+import net.sf.saxon.Configuration;
+import net.sf.saxon.lib.Initializer;
+
+/**
+ *
+ * @author tgraham
+ */
+public class RunAHFExtInitializer implements Initializer {
+
+    @Override
+    public void initialize(Configuration c) throws TransformerException {
+        c.registerExtensionFunction(new RunAHFSaxon());
+    }
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/ahf/saxon/RunAHFSaxon.java	Mon Dec 02 13:47:11 2013 +0000
@@ -0,0 +1,99 @@
+package org.w3c.ppl.xslt.ext.ahf.saxon;
+
+import jp.co.antenna.XfoJavaCtl.*;
+import net.sf.saxon.dom.DocumentOverNodeInfo;
+import net.sf.saxon.expr.XPathContext;
+import net.sf.saxon.lib.ExtensionFunctionCall;
+import net.sf.saxon.lib.ExtensionFunctionDefinition;
+import net.sf.saxon.om.Item;
+import net.sf.saxon.om.SequenceIterator;
+import net.sf.saxon.om.StructuredQName;
+import net.sf.saxon.trans.XPathException;
+import net.sf.saxon.tree.tiny.TinyDocumentImpl;
+import net.sf.saxon.value.SequenceType;
+import net.sf.saxon.value.StringValue;
+import net.sf.saxon.value.Value;
+import net.sf.saxon.value.ObjectValue;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import javax.xml.transform.TransformerFactory;
+import java.io.StringWriter;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+
+class ErrDump implements MessageListener {
+
+	public void onMessage(int errLevel, int errCode, String errMessage) {
+		System.out.println("ErrorLevel = " + errLevel + "\nErrorCode = " + errCode + "\n" + errMessage);
+	}
+}
+
+/**
+ * @author tgraham
+ */
+public class RunAHFSaxon extends ExtensionFunctionDefinition {
+
+    @Override
+    public StructuredQName getFunctionQName() {
+        return new StructuredQName("runahf", "http://org.w3c.ppl.xslt/saxon-extension", "area-tree");
+    }
+
+    @Override
+    public SequenceType[] getArgumentTypes() {
+        return new SequenceType[]{SequenceType.SINGLE_NODE};
+    }
+
+    @Override
+    public SequenceType getResultType(SequenceType[] sts) {
+        return SequenceType.SINGLE_NODE;
+    }
+
+    @Override
+    public ExtensionFunctionCall makeCallExpression() {
+        return new ExtensionFunctionCall() {
+            @Override
+            public SequenceIterator<? extends Item> call(SequenceIterator<? extends Item>[] sis,
+                    XPathContext xpc) throws XPathException {
+                
+                TinyDocumentImpl item = (TinyDocumentImpl) sis[0].next();
+                Document foTree = (Document) DocumentOverNodeInfo.wrap(item);
+                ByteArrayInputStream isFo = null;
+                ByteArrayOutputStream osAt = null;
+                try {
+                    isFo = new ByteArrayInputStream(nodeToString(foTree).getBytes("UTF-8"));
+                    osAt = new ByteArrayOutputStream();
+                    
+                    XfoObj axfo = new XfoObj();
+                    ErrDump eDump = new ErrDump();
+                    axfo.setMessageListener(eDump);
+                    axfo.setExitLevel(4);
+                    axfo.render(isFo, osAt, "@AreaTree");
+                } catch (Exception ex) {
+                    ex.printStackTrace();
+                    throw new XPathException(ex);
+                }
+                StreamSource sAt = new StreamSource(new ByteArrayInputStream(osAt.toByteArray()));
+                return Value.asIterator(new ObjectValue(sAt));
+            }
+        };
+    }
+
+    private String nodeToString(Node node) {
+        StringWriter sw = new StringWriter();
+        try {
+	    Transformer t = TransformerFactory.newInstance().newTransformer();
+            t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+            t.transform(new DOMSource(node), new StreamResult(sw));
+        } catch (TransformerException te) {
+            System.out.println("nodeToString Transformer Exception");
+        }
+        return sw.toString();
+    }
+}
\ No newline at end of file