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 Lakshmi Anantharaman <La...@camsystems.com> on 2002/01/18 21:26:18 UTC

FOP and DOM Success

I tried for long and with the help of  fop-dev archive got my FOP servlet to
work with a DOM .
Here comes the code . I hope it is useful to someone down the line !

public void renderXML(HttpServletResponse response) throws ServletException
{
try {
         //Instantiate a DocumentBuilderFactory.
        DocumentBuilderFactory dFactory =
DocumentBuilderFactory.newInstance();
        // And setNamespaceAware, which is required when parsing xsl files
        dFactory.setNamespaceAware(true);
        //Use the DocumentBuilderFactory to create a DocumentBuilder.
        DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
        
        /**************************************************************/
        FileInputStream  xmlfile = new
FileInputStream("c:\\bea\\wlserver6.0\\contact.xml" );
        Document xmlDoc = dBuilder.parse(xmlfile);
        javax.xml.transform.dom.DOMSource xmlDomSource = new
javax.xml.transform.dom.DOMSource(xmlDoc);
        /************************************************************/

        
        /************************************************************/
        FileInputStream xsltFile = new FileInputStream("contactFO.xsl");

        org.w3c.dom.Document xslDoc = dBuilder.parse(xsltFile);
        javax.xml.transform.dom.DOMSource xslDomSource = new
javax.xml.transform.dom.DOMSource(xslDoc);
        javax.xml.transform.TransformerFactory tFactory =
javax.xml.transform.TransformerFactory.newInstance();
        javax.xml.transform.Templates templates =
tFactory.newTemplates(xslDomSource);
        javax.xml.transform.Transformer transformer =
templates.newTransformer();
        /*************************************************************/
        
        javax.xml.transform.dom.DOMResult foDomResult = new
javax.xml.transform.dom.DOMResult();
        transformer.transform(xmlDomSource, foDomResult);
        
        // Avoiding this step was what gave the null pointer exception ! 
        org.w3c.dom.Document foDoc
=(org.w3c.dom.Document)foDomResult.getNode();

         
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.setContentType("application/pdf");
        org.apache.fop.apps.Driver driver = new
org.apache.fop.apps.Driver();
        driver.setErrorDump(true);
        driver.setRenderer(driver.RENDER_PDF);
        driver.setupDefaultMappings() ;
        driver.setOutputStream(out);
        driver.render(foDoc);

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