Home | | Service Oriented Architecture | Web Application Integration: Java Servlets, XSLT, and XSL-FO

Chapter: XML and Web Services : Building XML-Based Applications : Transforming XML with XSL

Web Application Integration: Java Servlets, XSLT, and XSL-FO

In this section, we will pull all the parts together and develop a Web application that inte-grates Java servlets, XSLT, and XSL-FO. We will develop a Java servlet to pass an XML document and XSL style sheet to the Apache-FOP formatting engine. The XML docu-ment is booklist.xml.

Web Application Integration: Java Servlets, XSLT, and XSL-FO

In this section, we will pull all the parts together and develop a Web application that inte-grates Java servlets, XSLT, and XSL-FO. We will develop a Java servlet to pass an XML document and XSL style sheet to the Apache-FOP formatting engine. The XML docu-ment is booklist.xml. The XSL style sheet, booklist_table.xsl, contains the XSL-FO template code to generate a table. The servlet will respond with the PDF document gen-erated by the Apache-FOP formatting engine. The application interaction is shown in Figure 9.23.


Developing the Java Servlet

 

The Java servlet handles an HTTP GET request. The servlet sets up a reference to the files booklist.xml and booklist_table.xsl. The Apache-FOP API provides access to the Apache-FOP formatting engine via the class org.apache.fop.apps.Driver. The follow-ing code creates an instance of the driver and sets the renderer to PDF:

 

// setup the driver for PDF Driver driver = new Driver();

 

driver.setRenderer(Driver.RENDER_PDF);

 

Next, the servlet creates a file reference for the XML document and XSL style sheet. Because the servlet is running in the context of a servlet engine, we need to retrieve the real path to the Web application’s root (c:\foo\ch9_xsl\xsl_fo\public_html). The XSLTInputHandler class transforms the XML document using the XSL style sheet, and the resulting document is input for the Apache-FOP processing engine. This is accom-plished in the following code fragment:

 

String  appRoot    =  getServletContext().getRealPath(“/”);

 

String  xmlFileName  =  “booklist.xml”;

 

String  xslFileName  =  “booklist_table.xsl”;

 

File  xmlFile  =  new  File(appRoot  +  xmlFileName);

File  xslFile  =  new  File(appRoot  +  xslFileName);

//  create  an  input  handler  for  the  XSLT  transformation

 

XSLTInputHandler inputHandler = new XSLTInputHandler(xmlFile, xslFile); XMLReader parser = inputHandler.getParser();

 

Now, we need to set up an output for the XSL-FO formatter process. We’ll use a ByteArrayOutputStream object to serve as a temporary buffer. The XSL-FO processor generates the PDF document using the following code:

 

setup  the  output  for  XSL-FO  formatter  process

 

temporarily place in a ByteArrayOutputStream ByteArrayOutputStream out = new ByteArrayOutputStream(); driver.setOutputStream(out);

 

Run the formatter based on the XSL-FO document driver.render(parser, inputHandler.getInputSource());

 

Finally, we need to send the PDF document back to the Web browser. Recall from the previous step that the document was placed in a temporary buffer. All we have to do is access the content of the temporary buffer and send the content using the response. This is accomplished in the following code:

 

The  out  object  has  the  result  of  the  XSL-FO  formatter  process

 

Retrieve  the  content  from  ByteArray

 

byte[]  content  =  out.toByteArray();

 

Setup the response for the web browser response.setContentType(“application/pdf”); response.setContentLength(content.length);

 

Finally,  send  the  result  to  the  browser!

 

OutputStream outputToBrowser = response.getOutputStream(); outputToBrowser.write(content);

 

outputToBrowser.flush();

 

Listing 9.19 contains the complete code for the EzFopServlet.

 

LISTING 9.19  <install_dir>\ch9_xsl\xsl_fo\servlet_source\EzFopServlet.java

 

import  java.io.File;

 

import  java.io.OutputStream;

 

import java.io.ByteArrayOutputStream; import java.io.IOException;

 

import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;

 

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import  org.xml.sax.XMLReader;

import  org.apache.fop.apps.Driver;

 

import  org.apache.fop.apps.XSLTInputHandler;

 

/**

 

* Example servlet to generate a PDF from an XSL-FO document */

 

public  class  EzFopServlet  extends  HttpServlet  {

 

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 

try  {

 

//  get  the  application  root  for  this  web  app

 

String  appRoot     =  getServletContext().getRealPath(“/”);

 

String xmlFileName = “booklist.xml”; String xslFileName = “booklist_table.xsl”;

 

File xmlFile = new File(appRoot + xmlFileName); File xslFile = new File(appRoot + xslFileName);

 

//  diagnostic  messages

 

System.out.println(“EzFopServlet: XSL-FO formatting:”); System.out.println(“xml = “ + xmlFile); System.out.println(“xsl = “ + xslFile + “\n\n”);

 

setup the driver for PDF Driver driver = new Driver();

 

driver.setRenderer(Driver.RENDER_PDF);

 

create an input handler for the XSLT transformation XSLTInputHandler inputHandler =

 

new XSLTInputHandler(xmlFile, xslFile); XMLReader parser = inputHandler.getParser();

 

         setup  the  output  for  XSL-FO  formatter  process

 

temporarily place in a ByteArrayOutputStream ByteArrayOutputStream out = new ByteArrayOutputStream(); driver.setOutputStream(out);

 

Run the formatter based on the XSL-FO document driver.render(parser, inputHandler.getInputSource());

 

         The  out  object  has  the  result  of  the  XSL-FO  formatter  process

 

            Retrieve  the  content  from  ByteArray

 

byte[]  content  =  out.toByteArray();

 

// Setup the response for the web browser

response.setContentType(“application/pdf”); response.setContentLength(content.length);

//  Finally,  send  the  result  to  the  browser!

 

OutputStream outputToBrowser = response.getOutputStream(); outputToBrowser.write(content);

 

outputToBrowser.flush();

 

}

 

catch (Exception exc) { log(exc.toString());

 

throw  new  ServletException(exc);

 

}

 

}

 

}

 

 

Testing the Example

 

This example is stored in a separate Web application archive (WAR) file, called ezfop.war. The WAR file contains the compiled class for EzFopServlet. The WAR file can be deployed on any JSP or servlet engine that supports the Servlet 2.3 API or JSP 1.2.

 

Follow these steps to deploy it on a Tomcat 4 server:

 

1.    Copy the file <install_dir>\ch9_xsl\xsl_fo\ezfop.war to

 

<tomcat_install_dir>\webapps.

 

     Restart the Tomcat server.

 

Access the ezfop Web application using http://localhost:8080/ezfop. Figure 9.23 shows what your screen should look like.

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
XML and Web Services : Building XML-Based Applications : Transforming XML with XSL : Web Application Integration: Java Servlets, XSLT, and XSL-FO |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.