FOP uses FO.  It has nothing to do with XML.  It could clear up some
confusion to just put XML processing in a separate preprocessor jar.
The XML -> FO code is all Oracle, only requiring a third party jar to be
in the classpath at runtime.  When it hits the transform statement it
looks for xalan, possibly xerces I forget exactly which jar does what.

Here's some sample code as I embed it as a 2-step to clarify that the
XML is not related to FOP.
The second step uses the same Transformer class with a few key
differences.
1) The SAX input is generated the same but using the FO file instead of
the XML.
2) The Transformer is created without any XSL reference.
3) The output object is this. myTransformResult = new
SAXResult(myFop.getDefaultHandler());
How you set up that Fop object and what you do after that second
transform depends on the output format.


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.InputSource;

public class FoProcessor {
     public static final TransformerFactory myTransformerFactory =
TransformerFactory.newInstance();

     public void createFoFile(String myXmlFileName, String
myXslFileName, String myFoFileName) {
          File myXmlFile;
          FileInputStream myXmlFIS;
          byte[] myXmlByteData;
          File myXslFile;
          FileInputStream myXslFIS;
          byte[] myXslByteData;

          try {
               myXmlFile = new File(myXmlFileName);
               // Potential FileNotFound Exception
               myXmlFIS = new FileInputStream(myXmlFile);
               // Potential IOException
               myXmlByteData = new byte[myXmlFIS.available()];
               // Potential IOException
               myXmlFIS.read(myXmlByteData);
               // Potential IOException
               myXmlFIS.close();
     
               myXslFile = new File(myXslFileName);
               // Potential FileNotFound Exception
               myXslFIS = new FileInputStream(myXslFile);
               // Potential IOException
               myXslByteData = new byte[myXslFIS.available()];
               // Potential IOException
               myXslFIS.read(myXslByteData);
               // Potential IOException
               myXslFIS.close();
               createFoFile(myXmlByteData, myXslByteData, myFoFileName);
          } catch (Exception e) {
               // Crash here, exit with error and/or log
          }
     }
     public void createFoFile(byte[] myXmlByteData, byte[]
myXslByteData, String myFoFileName) {
          byte[] myFoByteData = createFo(myXmlByteData, myXslByteData);
          File myFoFile;
          FileOutputStream myFoFileOutputStream;

          myFoFile = new File(myFoFileName);
          if (myFoFile.exists()) {
               myFoFile.delete();
          }
          try {
               // Potential Exception FileNotFoundException
               myFoFileOutputStream = new FileOutputStream(myFoFile);
               // Potential Exception IOException
               myFoFileOutputStream.write(myFoByteData);
               // Potential Exception IOException
               myFoFileOutputStream.close();
          } catch (Exception e) {
               // Crash here, exit with error and/or log
          }
     }
     private byte[] createFo(byte[] myXmlByteData, byte[] myXslByteData)
{
          ByteArrayOutputStream myFoByteArrayOutputStream;
          StreamResult myStreamResult;
          SAXSource myXmlSAXSource;
          SAXSource myXslSAXSource;
          byte[] myFoByteData = null;

          myFoByteArrayOutputStream = new ByteArrayOutputStream();
          myStreamResult = new StreamResult(myFoByteArrayOutputStream);
          try {
               myXmlSAXSource = createSAXSource(myXmlByteData);
               myXslSAXSource = createSAXSource(myXslByteData);
               Transformer myTransformer = null;
               // Creates a null Transformer if XSL code is invalid or
if a concurrent thread is creating a Transformer
               while (myTransformer == null) {
                    // Potential Exception
TransformerConfigurationException
                    myTransformer =
myTransformerFactory.newTransformer(myXslSAXSource);
                    // Invalid syntax in style sheet crashes program on
setParameter method
                    myTransformer.setParameter("versionParam", "2.0");
               }
               // Potential Exception TransformerException
               myTransformer.transform(myXmlSAXSource,myStreamResult);
               myXmlByteData = null;
               myFoByteData = myFoByteArrayOutputStream.toByteArray();
               // Potential Exception IOException
               myFoByteArrayOutputStream.close();
          } catch (Exception e) {
               // Crash here, exit with error and/or log
          }
          return myFoByteData;
     }
     private SAXSource createSAXSource(byte[] myByteData) {
          ByteArrayInputStream myByteArrayInputStream;
          InputSource myInputSource;
          SAXSource mySAXSource;

          myByteArrayInputStream = new ByteArrayInputStream(myByteData);
          myInputSource = new InputSource(myByteArrayInputStream);
          mySAXSource = new SAXSource(myInputSource);
          return mySAXSource;
     }

}

-----Original Message-----
From: Andreas L. Delmelle [mailto:[email protected]] 
Sent: Tuesday, June 14, 2011 6:10 PM
To: [email protected]
Subject: Re: FOP Extension to handle Wiki Syntax

On 14 Jun 2011, at 16:46, Glenn Adams wrote:

> You know, given the time spent answering questions about XSL and the
XML+XSL -> XSL-FO front-end ("convenience mechanism") in FOP, I
sometimes wonder if it would be better to rip out that function. Perhaps
then folks would understand better that FOP is fundamentally an XSL-FO
-> output format processor.

FOP did start that way, if I recall correctly. There was a time when
including the possibility to feed XML+XSLT as input was considered a big
win. I actually think going back makes little sense, especially given
that the usage pattern for embedding is basic JAXP. Even if you feed FOP
plain FO, an identity transformation is used nowadays, also when
invoking via the CLI.
Way back when, using the CLI with plain FO actually did not use the XSLT
processor at all, just the XML parser.

Another point would be that XSL-FO itself defines nearly all its
examples in terms of XSLT that transforms semantic XML to FO... *shrug*
It is easy to see how newcomers get confused. Ultimately, we could just
respond with a link to the website, where it is darn' well explained
what the recommended practice is in case of problems (i.e. submit FO,
not XSLT; if the problem is XSLT, go to Mulberry etc.). 
Nonetheless, I'll probably never get tired of pointing it out to people
--even if they do seem to lack the most basic web browsing skills
sometimes... ;-)

...

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to