Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » XML » XSLT
Language: Java
Expertise: Advanced
Feb 13, 2007

Process an XSL-FO Document

Sometimes you need to process an XSL-FO document that is the result of an XSLT transformation. Actually, the XSL-FO document doesn't exist, it is just a stream of SAX events. In a Java application, this can be done with the FOP processor classes:

//the XML document
C:\Data_Local\xml\docs\AirWings_xpath_xslt.xml
//the XSL stylesheet
C:\Data_Local\xml\docs\AirWings_xslt_fop.xsl
//the output - a PDF file
C:\Data_Local\xml\docs\AirWings_xslt_1.pdf

//FOP
import org.apache.fop.apps.Driver; 
import org.apache.fop.messaging.MessageHandler;

//Avalon
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.logger.ConsoleLogger;

//Java
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.SAXResult;

public class XSL_FO_and_XML_XSLT_1{
   public static void main(String[] args)
      {
      OutputStream OS_pdf=null;
      Transformer tXML=null;
         
      //create a org.apache.fop.apps.Driver object
      Driver fop=new Driver();
        
      //setting  a logger
      Logger logger=new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
      fop.setLogger(logger);
      MessageHandler.setOutputMethod(MessageHandler.SCREEN);
      MessageHandler.setScreenLogger(logger); 
      
      //specify the PDF format
      fop.setRenderer(Driver.RENDER_PDF);         
                          
      //the PDF file
      try{
         File F_pdf=new File  
              ("C://Data_Local//xml//docs//AirWings_xslt_1.pdf");
         OS_pdf=new FileOutputStream(F_pdf);   
         fop.setOutputStream(OS_pdf);
         }catch(java.io.FileNotFoundException e)
            {MessageHandler.error("[***   "+e.getMessage()+"  
                                  ***]");}
                                           
      //create a TransformerFactory object
      TransformerFactory tfXML=TransformerFactory.newInstance();
         
      //setting the XSL stylesheet, AirWings_xslt_fop.xsl
      Source sXSL=new StreamSource(new File 
             ("C://Data_Local//xml//docs//AirWings_xslt_fop.xsl"));
                
      //getting a Transformer object
      try{
         tXML=tfXML.newTransformer(sXSL);
         }catch 
            (javax.xml.transform.TransformerConfigurationException e)
            {MessageHandler.error("[*** XSLT Error ***]");}
         
      //setting the XML, AirWings_xpath_xslt.xml
      Source sXML=new StreamSource(new File
             ("C://Data_Local//xml//docs//AirWings_xpath_xslt.xml"));
         
      //define the Result
      Result rXML=new SAXResult(fop.getContentHandler());

      //pass sXML and rXML to the transformer
      try{
         tXML.transform(sXML,rXML);
         }catch(javax.xml.transform.TransformerException e)
            {MessageHandler.error("[*** XSLT error ***]");}
      }
}         
Notice that there isn't an .fo/.fop file.

Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap