Xalan working with AHF.
authorTony Graham
Sun, 09 Feb 2014 17:55:14 +0000
changeset 27 c34d37f2d7a1
parent 26 606b0bf30b17
child 28 5509d620d54f
Xalan working with AHF.
FOPRunXSLTExt/build-nonb.xml
FOPRunXSLTExt/examples/example1_xalan_ahf61.xsl
FOPRunXSLTExt/examples/example1_xalan_fop10.xsl
FOPRunXSLTExt/examples/example3_saxon9_fop10.xsl
FOPRunXSLTExt/examples/example5_saxon9_ahf61.xsl
FOPRunXSLTExt/examples/example5_saxon9_fop10.xsl
FOPRunXSLTExt/examples/formatting.xsl
FOPRunXSLTExt/examples/runsaxon9he_ahf61.sh.in
FOPRunXSLTExt/examples/runxalan_ahf61.sh.in
FOPRunXSLTExt/examples/runxalan_fop10.sh.in
FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/ahf/saxon/RunAHFSaxon.java
FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/ahf/xalan/RunAHFXalan.java
FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/fop/RunFOP.java
FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/fop/xalan/RunFOPXalan.java
--- a/FOPRunXSLTExt/build-nonb.xml	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/build-nonb.xml	Sun Feb 09 17:55:14 2014 +0000
@@ -42,6 +42,10 @@
 	<filterset refid="jars"/>
       </copy>
       <chmod file="examples/runxalan_fop10.sh" perm="ugo+rx"/>
+      <copy file="examples/runxalan_ahf61.sh.in" tofile="examples/runxalan_ahf61.sh">
+	<filterset refid="jars"/>
+      </copy>
+      <chmod file="examples/runxalan_ahf61.sh" perm="ugo+rx"/>
     </target>
 
     <target name="zip.windows">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FOPRunXSLTExt/examples/example1_xalan_ahf61.xsl	Sun Feb 09 17:55:14 2014 +0000
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- ============================================================= -->
+<!-- FOPRunXSLTExt Xalan example 1                                 -->
+<!--                                                               -->
+<!-- See http://www.w3.org/community/ppl/wiki/FOPRunXSLTExt        -->
+<!--                                                               -->
+<!-- Requires Xalan 2.7 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="1.0"
+    xmlns:fo="http://www.w3.org/1999/XSL/Format"
+    xmlns:xalan="http://xml.apache.org/xalan"
+    xmlns:runahf="runahf">
+
+<xalan:component prefix="runahf" functions="areaTree" elements="">
+  <xalan:script lang="javaclass"
+		src="xalan://org.w3c.ppl.xslt.ext.ahf.xalan" />
+</xalan:component>
+
+<xsl:param name="dest_dir" />
+<xsl:param name="area_tree_filename" />
+
+<xsl:variable name="area_tree_file"
+	      select="concat($dest_dir, '/', $area_tree_filename)" />
+
+<xsl:template match="/">
+  <!-- Make FO from source document. -->
+  <xsl:variable name="foTree">
+    <fo:root>
+      <fo:layout-master-set>
+	<fo:simple-page-master master-name="test-page">
+	  <fo:region-body margin="1in"/>
+	</fo:simple-page-master>
+      </fo:layout-master-set>
+
+      <fo:page-sequence master-reference="test-page">
+	<fo:flow flow-name="xsl-region-body">
+	  <xsl:apply-templates select="example" />
+	</fo:flow>
+      </fo:page-sequence>
+    </fo:root>
+  </xsl:variable>
+
+  <xsl:copy-of select="runahf:RunAHFXalan.areaTree($foTree)"/>
+</xsl:template>
+
+<xsl:template match="header">
+  <fo:block font-size="14pt" font-family="verdana" color="red"
+	    space-before="5mm" space-after="5mm">
+    <xsl:apply-templates/>
+  </fo:block>
+</xsl:template>
+
+<xsl:template match="paragraph">
+  <fo:block text-indent="5mm" font-family="verdana" font-size="12pt">
+    <xsl:apply-templates/>
+  </fo:block>
+</xsl:template>
+
+</xsl:stylesheet>
\ No newline at end of file
--- a/FOPRunXSLTExt/examples/example1_xalan_fop10.xsl	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/examples/example1_xalan_fop10.xsl	Sun Feb 09 17:55:14 2014 +0000
@@ -1,47 +1,55 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<xsl:stylesheet version="2.0"
-xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-xmlns:fo="http://www.w3.org/1999/XSL/Format"
-xmlns:xs="http://www.w3.org/2001/XMLSchema"
-xmlns:xalan="http://xml.apache.org/xalan"
-xmlns:runfop="runfop">
+<!-- ============================================================= -->
+<!-- FOPRunXSLTExt Xalan example 1                                 -->
+<!--                                                               -->
+<!-- See http://www.w3.org/community/ppl/wiki/FOPRunXSLTExt        -->
+<!--                                                               -->
+<!-- Requires Xalan 2.7 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="1.0"
+    xmlns:fo="http://www.w3.org/1999/XSL/Format"
+    xmlns:xalan="http://xml.apache.org/xalan"
+    xmlns:runfop="runfop">
 
-<xalan:component prefix="runfop" functions="areaTreeUrl" elements="">
-    <xalan:script lang="javaclass" src="xalan://org.w3c.ppl.xslt.ext.fop.xalan" />
+<xalan:component prefix="runfop" functions="areaTree" elements="">
+  <xalan:script lang="javaclass"
+		src="xalan://org.w3c.ppl.xslt.ext.fop.xalan" />
 </xalan:component>
 
 <xsl:param name="dest_dir" />
 <xsl:param name="area_tree_filename" />
 
+<xsl:variable name="area_tree_file"
+	      select="concat($dest_dir, '/', $area_tree_filename)" />
+
 <xsl:template match="/">
-<xsl:variable name="foTree">
-<fo:root>
+  <!-- Make FO from source document. -->
+  <xsl:variable name="foTree">
+    <fo:root>
       <fo:layout-master-set>
-        <fo:simple-page-master master-name="test-page">
-          <fo:region-body margin="1in"/>
-        </fo:simple-page-master>
+	<fo:simple-page-master master-name="test-page">
+	  <fo:region-body margin="1in"/>
+	</fo:simple-page-master>
       </fo:layout-master-set>
 
       <fo:page-sequence master-reference="test-page">
-        <fo:flow flow-name="xsl-region-body">
-          <xsl:apply-templates select="example" />
-       </fo:flow>
+	<fo:flow flow-name="xsl-region-body">
+	  <xsl:apply-templates select="example" />
+	</fo:flow>
       </fo:page-sequence>
     </fo:root>
-</xsl:variable>
-
-<xsl:variable name="area_tree_file" select="concat($dest_dir, '/', $area_tree_filename)" />
+  </xsl:variable>
 
-<xsl:message>Area tree filename = <xsl:value-of select="$area_tree_file" /></xsl:message>
-
-<url>
-  <xsl:value-of select="runfop:RunFOPXalan.areaTreeUrl($foTree, $area_tree_file)"/>
-  </url>
+  <xsl:copy-of select="runfop:RunFOPXalan.areaTree($foTree)"/>
 </xsl:template>
 
 <xsl:template match="header">
   <fo:block font-size="14pt" font-family="verdana" color="red"
-  space-before="5mm" space-after="5mm">
+	    space-before="5mm" space-after="5mm">
     <xsl:apply-templates/>
   </fo:block>
 </xsl:template>
--- a/FOPRunXSLTExt/examples/example3_saxon9_fop10.xsl	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/examples/example3_saxon9_fop10.xsl	Sun Feb 09 17:55:14 2014 +0000
@@ -4,7 +4,7 @@
 <!--                                                               -->
 <!-- See http://www.w3.org/community/ppl/wiki/FOPRunXSLTExt        -->
 <!--                                                               -->
-<!-- Requires Saxon 9.4 or later and FOP 1.0                       -->
+<!-- Requires Saxon 9.5 or later and FOP 1.0                       -->
 <!--                                                               -->
 <!-- Produced by the Print and Page Layout Community Group @ W3C   -->
 <!-- ============================================================= -->
--- a/FOPRunXSLTExt/examples/example5_saxon9_ahf61.xsl	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/examples/example5_saxon9_ahf61.xsl	Sun Feb 09 17:55:14 2014 +0000
@@ -4,7 +4,7 @@
 <!--                                                               -->
 <!-- See http://www.w3.org/community/ppl/wiki/FOPRunXSLTExt        -->
 <!--                                                               -->
-<!-- Requires Saxon 9.4 or later and FOP 1.0                       -->
+<!-- Requires Saxon 9.5 or later and FOP 1.0                       -->
 <!--                                                               -->
 <!-- Produced by the Print and Page Layout Community Group @ W3C   -->
 <!-- ============================================================= -->
--- a/FOPRunXSLTExt/examples/example5_saxon9_fop10.xsl	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/examples/example5_saxon9_fop10.xsl	Sun Feb 09 17:55:14 2014 +0000
@@ -4,7 +4,7 @@
 <!--                                                               -->
 <!-- See http://www.w3.org/community/ppl/wiki/FOPRunXSLTExt        -->
 <!--                                                               -->
-<!-- Requires Saxon 9.4 or later and FOP 1.0                       -->
+<!-- Requires Saxon 9.5 or later and FOP 1.0                       -->
 <!--                                                               -->
 <!-- Produced by the Print and Page Layout Community Group @ W3C   -->
 <!-- ============================================================= -->
--- a/FOPRunXSLTExt/examples/formatting.xsl	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/examples/formatting.xsl	Sun Feb 09 17:55:14 2014 +0000
@@ -4,7 +4,7 @@
 <!--                                                               -->
 <!-- See http://www.w3.org/community/ppl/wiki/FOPRunXSLTExt        -->
 <!--                                                               -->
-<!-- Requires Saxon 9.4 or later and FOP 1.0                       -->
+<!-- Requires Saxon 9.4 or later and FOP 1.0 or AHF 6.1            -->
 <!--                                                               -->
 <!-- Produced by the Print and Page Layout Community Group @ W3C   -->
 <!-- ============================================================= -->
@@ -38,7 +38,8 @@
 </xsl:template>
 
 <xsl:template match="header">
-  <fo:block font-size="14pt" 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>
--- a/FOPRunXSLTExt/examples/runsaxon9he_ahf61.sh.in	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/examples/runsaxon9he_ahf61.sh.in	Sun Feb 09 17:55:14 2014 +0000
@@ -31,4 +31,4 @@
 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
+java -cp $CP net.sf.saxon.Transform -init:org.w3c.ppl.xslt.ext.ahf.saxon.RunAHFExtInitializer -s:$1 -xsl:$2 -o:$3 -it:main
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FOPRunXSLTExt/examples/runxalan_ahf61.sh.in	Sun Feb 09 17:55:14 2014 +0000
@@ -0,0 +1,43 @@
+#!/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
+
+CP=@file.reference.fop.jar@
+AHF=@file.reference.ahf.jar@
+CP=$CP:$FOP/lib/xalan-2.7.0.jar
+CP=$CP:$FOP/lib/xercesImpl-2.7.1.jar
+CP=$CP:$FOP/lib/xml-apis-1.3.04.jar
+CP=$CP:$FOP/lib/serializer-2.7.0.jar
+CP=$CP:$FOP/lib/xmlgraphics-commons-1.4.jar
+CP=$CP:$FOP/lib/commons-logging-1.0.4.jar
+CP=$CP:$FOP/lib/commons-io-1.3.1.jar
+CP=$CP:$FOP/lib/avalon-framework-4.2.0.jar
+CP=$CP:$FOP/lib/batik-all-1.7.jar
+CP=$CP:@file.reference.ahf.jar@
+CP=$CP:@FOPRunXSLTExt.jar@
+
+java -cp $CP org.apache.xalan.xslt.Process -IN $1 -XSL $2 -OUT $3
--- a/FOPRunXSLTExt/examples/runxalan_fop10.sh.in	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/examples/runxalan_fop10.sh.in	Sun Feb 09 17:55:14 2014 +0000
@@ -13,4 +13,4 @@
 CP=$CP:$FOP/lib/batik-all-1.7.jar
 CP=$CP:@FOPRunXSLTExt.jar@
 
-java -cp $CP org.apache.xalan.xslt.Process -IN $1 -XSL $2 -OUT $3 -PARAM dest_dir $4 -PARAM area_tree_filename $5
+java -cp $CP org.apache.xalan.xslt.Process -IN $1 -XSL $2 -OUT $3
--- a/FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/ahf/saxon/RunAHFSaxon.java	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/ahf/saxon/RunAHFSaxon.java	Sun Feb 09 17:55:14 2014 +0000
@@ -99,16 +99,4 @@
             }
         };
     }
-
-    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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/ahf/xalan/RunAHFXalan.java	Sun Feb 09 17:55:14 2014 +0000
@@ -0,0 +1,74 @@
+package org.w3c.ppl.xslt.ext.ahf.xalan;
+
+import jp.co.antenna.XfoJavaCtl.*;
+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.parsers.DocumentBuilderFactory;
+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 RunAHFXalan {
+
+    public static Node areaTree(Node foTree) throws Exception {
+	ByteArrayInputStream isFo = null;
+	ByteArrayOutputStream osAt = null;
+	XfoObj axfo = null;
+	try {
+	    isFo = new ByteArrayInputStream(nodeToString(foTree).getBytes());
+	    osAt = new ByteArrayOutputStream();
+                    
+	    axfo = new XfoObj();
+	    ErrDump eDump = new ErrDump();
+	    axfo.setMessageListener(eDump);
+	    axfo.setExitLevel(4);
+	    // Just setting the printer name in render() fails with
+	    // 'Unknown printer name:' message in AHF 6.1 MR3.
+	    axfo.setPrinterName("@AreaTree");
+	    axfo.render(isFo, osAt, "@AreaTree");
+	    return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(osAt.toByteArray()));
+	} catch (Exception ex) {
+	    ex.printStackTrace();
+	    throw new Exception(ex);
+	}
+	finally {
+	    try {
+		if (axfo != null)
+		    axfo.releaseObjectEx();
+	    } catch (XfoException e) {
+		System.out.println("ErrorLevel = " + e.getErrorLevel() + "\nErrorCode = " + e.getErrorCode() + "\n" + e.getErrorMessage());
+		return null;
+	    }
+	}
+    }
+
+    private static 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();
+    }
+}
--- a/FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/fop/RunFOP.java	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/fop/RunFOP.java	Sun Feb 09 17:55:14 2014 +0000
@@ -1,17 +1,22 @@
 package org.w3c.ppl.xslt.ext.fop;
 
 import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;
+import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 import org.apache.fop.apps.Fop;
 import org.apache.fop.apps.FopFactory;
@@ -41,6 +46,23 @@
 	transformer.transform(src, res);
     }
 
+    public Node executeFop(Node foTree)
+            throws Exception {
+
+        FopFactory fopFactory = FopFactoryFactory.createFopFactory();
+        if (fopFactory == null) {
+            throw new Exception("Cannot create FopFactory");
+        }
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AREA_TREE, out);
+        TransformerFactory tf = new TransformerFactoryImpl();
+        Transformer transformer = tf.newTransformer();
+        Source src = new DOMSource(foTree);
+        Result res = new SAXResult(fop.getDefaultHandler());
+        transformer.transform(src, res);
+        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(out.toByteArray()));
+    }
+
     public String executeFop(String ifName, Node foTree)
             throws Exception {
 
--- a/FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/fop/xalan/RunFOPXalan.java	Sat Feb 08 22:33:52 2014 +0000
+++ b/FOPRunXSLTExt/src/org/w3c/ppl/xslt/ext/fop/xalan/RunFOPXalan.java	Sun Feb 09 17:55:14 2014 +0000
@@ -9,6 +9,10 @@
  */
 public class RunFOPXalan {
     
+    public static Node areaTree(Node foTree) throws Exception {
+        return new RunFOP().executeFop(foTree);
+    }
+    
     public static String areaTreeUrl(Node foTree, String ifName) throws Exception {
         return new RunFOP().executeFop(ifName, foTree);
     }