FOP and XSLT <xsl:import> issue

Discussion in 'Java' started by couellette@gmail.com, Apr 5, 2005.

  1. Guest

    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>
     
    , Apr 5, 2005
    #1
    1. Advertising

  2. R.F. Pels Guest

    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
     
    R.F. Pels, Apr 5, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johannes Koch
    Replies:
    0
    Views:
    1,685
    Johannes Koch
    Jul 29, 2003
  2. DV
    Replies:
    2
    Views:
    521
    Patrick TJ McPhee
    Apr 2, 2004
  3. Jens Mueller
    Replies:
    0
    Views:
    736
    Jens Mueller
    Apr 12, 2005
  4. Replies:
    1
    Views:
    465
  5. Pablo
    Replies:
    0
    Views:
    1,014
    Pablo
    Mar 28, 2007
Loading...

Share This Page