简体   繁体   中英

What information does Result contain when using FOP and XSL-FO, and how can I use it?

I am using Apache FOP in conjunction with XSL-FO to convert XML into a formatted PDF. Right now I have the following method in Java (ignore the fact that the method is not returning a boolean for now):

public boolean Transform() throws TransformerException, FOPException
{
  Transformer lTransformer;
  java.io.ByteArrayOutputStream lOutStream = new java.io.ByteArrayOutputStream();

  FopFactory lFopFactory = FopFactory.newInstance();
  FOUserAgent lFOAgent = lFopFactory.newFOUserAgent();

  lTransformer = getTransformer(mXsltSource); // returns a new Transformer
  Fop fop = lFopFactory.newFop(mMimeOut, lFOAgent, lOutStream);
  Result res = new SAXResult(fop.getDefaultHandler());

  lTransformer.transform(mSource, res); // transforms xml source to formatted XSL-FO transform
}

However, I am unsure of how to use "res" to make sure that the "lTransformer.transform(StreamSource, Result)" method executed correctly (I am assuming that is the purpose of the Result object). I have looked at the javadocs for the transform method, Result class, and SAXResult class, but these have not yielded much help. Can anybody offer some insight into this?

TL;DR; What is the purpose of passing a Result in javax.xml.transform.Transformer.transform(StreamSource, Result) , and how can I use this to check that the operation completed successfully?

The result of the transform is written to that Result object. If you look at the chain of objects that go into creating the Result object, the output stream you've specified is ultimately where the result is written (you passed a ByteArrayOutputStream into FopFactory.newFop() , and then pass the Fop handler into the Result - the output stream is the ultimate destination of your xsl transformation.

Pass in a FileOutputStream pointing to a file on your disk somewhere instead of the ByteArrayOutputStream , and after the transformation (if everything worked), the file should be a PDF you can view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM