You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by "M.Weiss" <m....@gmx.net> on 2002/08/20 13:20:31 UTC

Problem with running FOP as Servlet

Hi everybody,

I got a huge problem, rendering xml/xsl-documents to a pdf using fop in a
servlet. Perhaps, anybody could help me, as I`m near to give up ;-| Find the
XML-Datafile, the XSL-Template and the Servlet-Code below.

My problem is, that running the fop-commandline-tool (using FOP 0.20.1) the
conversion of those two files to a pdf runs very fine... No problem with
that. Using the same in a Servlet (in struts-paradigma), I got the following
error-stack on System.out:

using SAX parser org.apache.xerces.parsers.SAXParser
building formatting object tree
setting up fonts
 [2]
java.lang.NoSuchMethodError
        at
org.apache.xpath.axes.ChildIterator.nextNode(ChildIterator.java:140)
        at
org.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemForEach.ja
va:428)
        at
org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.jav
a:193)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2154)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2097)
        at
org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java:
739)
        at
org.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemForEach.ja
va:495)
        at
org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.jav
a:193)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2154)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2097)
        at
org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java:
739)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2154)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2097)
        at
org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java:
739)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2154)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2097)
        at
org.apache.xalan.templates.ElemLiteralResult.execute(ElemLiteralResult.java:
739)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2154)
        at
org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(Transform
erImpl.java:2097)
        at
org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(Transformer
Impl.java:2029)
        at
org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.j
ava:1189)
        at
org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:2894)
        at java.lang.Thread.run(Thread.java:484)


When removing the <xsl:apply-templates/>-tag from the XSL-Template,
replacing it by e.g. "Test", the servlet runs quite fine and prints a PDF to
the browser.
The Action class uses a form class (instantiated as peForm) which got 2
properties (xml and xsl), so peForm.getXml() returns the file-name for the
XML-Datafile, peForm.getXsl() returns the file-name for the XSL-Template.

Can anyone help with that ?

Many thanks in advance and best regards,

Marcus



XML-DATA:
--------------
<?xml version="1.0"?>
<title>Chapter title</title>


XSL-TEMPLATE:
-------------------
<?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:template match="/">
  <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
    <fo:simple-page-master master-name="page"
      page-height="29.7cm"
      page-width="21cm"
      margin-top="1cm"
      margin-bottom="2cm"
      margin-left="2.5cm"
      margin-right="2.5cm">
     <fo:region-before extent="3cm"/>
     <fo:region-body margin-top="3cm"/>
     <fo:region-after extent="1.5cm"/>
    </fo:simple-page-master>
   </fo:layout-master-set>

   <fo:page-sequence master-name="page" initial-page-number="2">
    <fo:flow flow-name="xsl-region-body">
     <xsl:apply-templates/>
    </fo:flow>
   </fo:page-sequence>

  </fo:root>
 </xsl:template>

 <xsl:template match="title">
  <fo:block text-align="center" space-after="8pt" space-before="16pt"
space-after.precedence="3">
     <xsl:apply-templates/>
  </fo:block>
 </xsl:template>
</xsl:stylesheet>


JAVA-ACTIONCLASS:
--------------------------
package any.package.gui.action;

import org.apache.struts.action.ActionForward;
import java.io.*;
import java.util.Hashtable;
import java.util.Locale;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.MessageResources;

import any.package.gui.form.*;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;

import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Version;
import org.apache.fop.apps.XSLTInputHandler;

public class PdfErstellenAction2 extends PrePostAction {
    /** Default parser name. */
    private static final String
    DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";

     public PdfErstellenAction2() {
     System.setProperty("org.xml.sax.driver", DEFAULT_PARSER_NAME);
     }

     public ActionForward executePre(ActionMapping mapping, AbstractForm
form,
          HttpServletRequest request, HttpServletResponse response)
          throws javax.servlet.ServletException, java.io.IOException {
              return new ActionForward(mapping.getInput());
     }


     public ActionForward executePost(ActionMapping mapping, AbstractForm
form,
          HttpServletRequest request, HttpServletResponse response)
          throws javax.servlet.ServletException, java.io.IOException
    {
        PdfErstellenForm peForm = (PdfErstellenForm)form;

        response.setContentType("application/pdf");

        Driver driver = new Driver();
        driver.setRenderer(Driver.RENDER_PDF);
        XSLTInputHandler inputHandler = new XSLTInputHandler(new
File(peForm.getXml()), new File(peForm.getXsl()));
        try
        {
          XMLReader parser = inputHandler.getParser();
          ByteArrayOutputStream out = new ByteArrayOutputStream();

          driver.setOutputStream(out);
          driver.render(parser, inputHandler.getInputSource());

          byte[] content = out.toByteArray();
          response.setContentLength(content.length);
          response.getOutputStream().write(content);
          response.getOutputStream().flush();
        }
        catch (Exception e)
        {
          e.printStackTrace(System.out);
          throw new ServletException(e);
        }

        return null;
    }

    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);
        }
    }

}