Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > FOP and XSLT <xsl:import> issue

Reply
Thread Tools

FOP and XSLT <xsl:import> issue

 
 
couellette@gmail.com
Guest
Posts: n/a
 
      04-05-2005
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" xmlnsalan="http://xml.apache.org/xslt"
xmlnssl="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>

 
Reply With Quote
 
 
 
 
R.F. Pels
Guest
Posts: n/a
 
      04-05-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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.

--
Ruurd
..o.
...o
ooo
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems running Apache FOP: org.apache.fop.fo.FOTreeBuilder fatalError Pablo Java 0 03-28-2007 02:31 PM
How to convert XML to XSLT & XSL-FO to be used by FOP ? krislioe@gmail.com Java 1 03-21-2007 12:34 PM
FOP, XSLT: problems with SAX2DTM Parsing trying to convert to XSL-FO in Java Jens Mueller XML 0 04-12-2005 04:06 PM
newbie questions concernig XSL/XSLT and FO/FOP (sorry, long) DV XML 2 04-02-2004 02:31 AM
weird links in PDF from XML and xslt using FOP Jurrie XML 0 10-06-2003 08:26 AM



Advertisments