FOP and XSLT <xsl:import> issue

C

couellette

Hello,

I am trying to build a PDF page from an XML and an XSL:FO document from
inside a servlet. My XSL:FO is using <xsl:import> to reference
additional stylesheets for style as well as other support templates.
These support stylesheets are in the same directory as the master
XSL:FO doc.

My parent XSL:FO doc runs fine when I run it through a renderer like
XMLSpy, but when I run the transform from a servlet, my PDF document is
empty. I have traced this problem to my <xsl:import> statement which I
originally had using a relative path. If I change the relative path to
an absolute path, the thing runs perfect. However, using absolute paths
is not an option for me since I need the same code to work on both
Windows and UNIX.

Any suggestions on this? Here is some sample code:

JSP which calls FOP:
<%@ page import = "java.io.*, javax.xml.transform.*,
javax.xml.transform.stream.*, org.xml.sax.InputSource,
org.apache.fop.apps.*, org.apache.fop.messaging.*,
javax.xml.transform.stream.StreamSource" %>

<%
try{

String xmlBase = "/WEB-INF/XML/";
String detailXML = (String)request.getAttribute("DETAIL_XML");
ServletContext context = getServletContext();

String origin = (String)request.getParameter("origin");
String navigation = origin.toUpperCase();

String fileName = "ViewMessages.pdf";
response.setContentType( "application/pdf" );
response.setHeader("Content-disposition", "attachment; filename="
+fileName );

String styleSheet = "ViewPDFDetails";

java.net.URL xslURL = context.getResource(xmlBase + "xsl/" +
styleSheet + ".xslt");

java.io.ByteArrayOutputStream pdfOutput = new
java.io.ByteArrayOutputStream();

java.io.ByteArrayOutputStream interimOut = new
java.io.ByteArrayOutputStream() ;

InputStream xslInputStream = ( InputStream )xslURL.getContent();

StringBufferInputStream xmlInputStream = new StringBufferInputStream(
detailXML );

StreamSource xmlSource = new StreamSource(xmlInputStream);
StreamSource xslSource = new StreamSource(xslInputStream);

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslSource);

transformer.transform(xmlSource, new StreamResult( interimOut ));

String foOutput = interimOut.toString();

Driver driver = new Driver(new InputSource(new StringReader(
foOutput)), pdfOutput);
driver.setRenderer(Driver.RENDER_PDF);
driver.run();

ByteArrayInputStream bais = new ByteArrayInputStream(
pdfOutput.toByteArray() );
java.io.BufferedInputStream bis = new java.io.BufferedInputStream(
bais );

int bit = 1024;
try {
while ((bit) >= 0) {
bit = bis.read();
out.write(bit);
}
}catch(Exception e){
}

out.flush();
out.close();
bis.close();

}catch(Exception e){}

XSL:FO
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xalan="http://xml.apache.org/xslt"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lookupCache="xalan://bbh.xml.helper.ui.LookupCache"
xmlns:dateHelper="xalan://bbh.mbter.helpers.DateFormat"
xmlns:textHelper="xalan://bbh.mbter.helpers.text.TextConverter"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:import href="./styles.xsl" />

<xsl:template match="/">
<fo:root>
..
..
..
</fo:root>
</xsl:template>
</xsl:transform>
 
R

R.F. Pels

an absolute path, the thing runs perfect. However, using absolute paths
is not an option for me since I need the same code to work on both
Windows and UNIX.

Any suggestions on this?

Simple solution: make sure that your working directory is set to the origin
of the template. Other solution: require that there is a configuration file
located at a known good location, load the necessary properties from there.
For Windows machines, that would be the system directory, for UNICES that
would be a subdirectory of the /etc hierarchy. Query the system properties
to find out on what architecture you are running.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top