My problem seems to be in the transformation of the xml and xsl stylesheet
(which includes fo elements). My servlet takes an fo document fine, but my
problem is I need it to take xml (either in a stream or a file) and an xsl
stylesheet that can format the xml very specifically for print. I get a few
different errors. Here is my xml, xsl, and servlet code.
XML --
<?xml version="1.0"?>
<timesheet>
<employee>
<first_name>John</first_name>
<last_name>Doe</last_name>
<dept>5130</dept>
<location>B27</location>
<ext>2819</ext>
<type>XPT</type>
</employee>
</timesheet>
----------------------------------------------------------------------------
----
XSL --
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" media-type="text/xml"
indent="yes"/>
<xsl:template match="timesheet">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="only">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-name="only">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates
select="employee"/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="employee">
<fo:block font-size="8pt" font-family="arial"
line-height="10pt">
<xsl:apply-templates select="first_name"/>
<xsl:apply-templates select="last_name"/>
<xsl:apply-templates select="dept"/>
<xsl:apply-templates select="location"/>
<xsl:apply-templates select="ext"/>
<xsl:apply-templates select="type"/>
</fo:block>
</xsl:template>
<xsl:template match="first_name">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</xsl:template>
<xsl:template match="last_name">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</xsl:template>
<xsl:template match="dept">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</xsl:template>
<xsl:template match="location">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</xsl:template>
<xsl:template match="ext">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</xsl:template>
<xsl:template match="type">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</xsl:template>
</xsl:stylesheet>
----------------------------------------------------------------------------
Java Servlet --
package com.company.bis.timesheet.servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Version;
import org.apache.fop.apps.XSLTInputHandler;
import org.apache.log4j.*;
import com.company.bis.timesheet.config.*;
public class PdfServlet extends HttpServlet {
private static final String FO_REQUEST_PARAM = "fo";
private static final String XML_REQUEST_PARAM = "xml";
private static final String XSL_REQUEST_PARAM = "xsl";
private Category logs = null;
/**
* init() reads connection-related information from config.xml
via the ConfigReader class.
* It is responsible for setting the logging context for the
servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
/* set up a log4j category for this page. */
logs =
Category.getInstance(PdfServlet.class.getName());
/* get log4j configuration info. */
try {
PropertyConfigurator.configure(ConfigReader.getLogProperties());
} catch (Exception e) {
logs.error("Problem reading Log Properties
value; element missing or incorrect in config.xml:" + e);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try {
String foParam = ConfigReader.getXslFoDirectory() + "/" +
request.getParameter(FO_REQUEST_PARAM);
logs.debug("xsl fo directory: " +
ConfigReader.getXslFoDirectory());
logs.debug("fo file: " + foParam);
String xmlParam = ConfigReader.getXmlDirectory() + "/" +
request.getParameter(XML_REQUEST_PARAM);
logs.debug("xml directory: " + ConfigReader.getXmlDirectory());
logs.debug("xml file: " + xmlParam);
String xslParam = ConfigReader.getXslDirectory() + "/" +
request.getParameter(XSL_REQUEST_PARAM);
logs.debug("xsl directory: " + ConfigReader.getXslDirectory());
logs.debug("xsl file: " + xslParam);
if ((request.getParameter(FO_REQUEST_PARAM)) != null) {
FileInputStream file = new FileInputStream(foParam);
renderFO(new InputSource(file), request, response);
} else if(((request.getParameter(XML_REQUEST_PARAM)) != null) &&
((request.getParameter(XSL_REQUEST_PARAM)) != null)) {
XSLTInputHandler input = new XSLTInputHandler(new
File(xmlParam), new File(xslParam));
renderXML(input, request, response);
} else {
logs.error("Problem rendering document to Pdf: parameters
supplied were incorrect. ");
String message = "There has been an error
rendering your timesheet to Pdf.";
RequestDispatcher rd =
request.getRequestDispatcher( "Error.jsp?message=" +
message);
rd.forward(request, response);
}
} catch (Exception e) {
logs.error("Problem rendering document to Pdf: " + e);
String message = "There has been an error
rendering your timesheet to Pdf.";
RequestDispatcher rd =
request.getRequestDispatcher( "Error.jsp?message=" +
message);
rd.forward(request, response);
}
}
/**
* renders an FO inputsource into a PDF file which is rendered
* directly to the response object's OutputStream
*/
public void renderFO(InputSource foFile, HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.setContentType("application/pdf");
Driver driver = new Driver(foFile, out);
logs.debug("Just created driver");
//driver.setLogger(logs);
driver.setRenderer(Driver.RENDER_PDF);
logs.debug("Just set renderer for driver");
driver.run();
logs.debug("Just called run on driver");
byte[] content = out.toByteArray();
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
} catch (Exception e) {
logs.error("Problem rendering document to Pdf: error in renderFO
" + e);
String message = "There has been an error
rendering your timesheet to Pdf.";
RequestDispatcher rd =
request.getRequestDispatcher( "Error.jsp?message=" +
message);
rd.forward(request, response);
}
}
public void renderXML(XSLTInputHandler input, HttpServletRequest
request,
HttpServletResponse response) throws
ServletException, IOException {
try {
logs.debug("In renderXML");
InputSource foFile = input.getInputSource();
renderFO(foFile, request, response);
} catch (Exception e) {
logs.error("Problem rendering document to Pdf: error in
renderXML " + e);
String message = "There has been an error
rendering your timesheet to Pdf.";
RequestDispatcher rd =
request.getRequestDispatcher( "Error.jsp?message=" +
message);
rd.forward(request, response);
}
}
/**
* creates a SAX parser, using the value of org.xml.sax.parser
* defaulting to org.apache.xerces.parsers.SAXParser
*
* @return the created SAX parser
*/
static XMLReader createParser() throws ServletException {
String parserClassName = System.getProperty("org.xml.sax.parser");
if (parserClassName == null) {
parserClassName = "org.apache.xerces.parsers.SAXParser";
}
try {
return (XMLReader) Class.forName(
parserClassName).newInstance();
} catch (Exception e) {
throw new ServletException(e);
}
}
}
-----Original Message-----
From: Scott Moore [mailto:[EMAIL PROTECTED]
Sent: Friday, February 01, 2002 9:10 AM
To: [EMAIL PROTECTED]
Subject: Re: XML, XSL a real problem
Yes, at least the XML part. The XSLT is on the server so the client doesn't
send it, but I could get it exactly the same way I get the XML.
What exactly are you having problems with? Writing the servlet code to
retrieve an XML document from a POST?
Scott
----- Original Message -----
From: "Zahigian, Mike" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, February 01, 2002 12:03 PM
Subject: XML, XSL a real problem
> Has anybody managed to get a Java Servlet to accept xml, xsl stylesheet
> including fo and render to pdf? I've been struggling for days and am
> running out of time before I have to abandon FOP for this project. If
> possible, include your xml, xsl, and servlet code so I can work from
there.
> Thanks in advance for any help.
>
> Mike Zahigian
> Business Information Systems
> x72819
>