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 "Savino, Matt C" <Ma...@questdiagnostics.com> on 2003/05/01 18:47:43 UTC

RE: Seeking advice on HSSF

Ok here it is. I read all of the POIFS and Coccoon stuff an found there is some demand to use the HSSF serializer as a stand-alone piece, ala FOP. (Thus avoiding all of the Coccoon overhead and 20MB .war file.) This actually used to exist separately that but the POI guys donated it to be folded into Coccoon. Supposedly there is a project on the Jakarta sandbox called Morphos to run the HSSF serializer separately, but I couldn't find it. No problem, since it seems simple enough anyway (so far). 

It is a little surprising to me that there hasn't been more effort to implement HSSF as in FOP. It's very convenient to be able to generate PDF, HTML, XLS, just by switching the stylesheet and serializer. Isn't this idea supposed to be the whole promise of XML anyway? I realize Cocoon is designed to do this, but the whole full-blown Cocoon site-map architecture isn't always a good solution for eveyrone.

I have broken down the code below, separating the common code from the pieces that are unique to FOP and HSSF. I have also attached the source for the servlet, html, xml and xsl (GMR:) that I am using. It's a little messy right now but hopefully you can get the idea. There is also a piece outside of this snippet (in our actual production report generator servlet) that caches the byte-array and returns it for the ever-inconsistent IE second and third request hit. If someone wants to see that let me know. 

-Matt

     
/*-----------------------------------------------------------------------------------------*/
/*--------- This code is common to FOP and HSSF -------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/      	javax.xml.parsers.DocumentBuilderFactory dFactory 
        = javax.xml.parsers.DocumentBuilderFactory.newInstance();
      dFactory.setNamespaceAware(true);
      javax.xml.parsers.DocumentBuilder dBuilder = dFactory.newDocumentBuilder();

      org.w3c.dom.Document xmlDoc = dBuilder.parse(xmlFile);
      javax.xml.transform.dom.DOMSource xmlDomSource = new javax.xml.transform.dom.DOMSource(xmlDoc);

      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();
  
      ByteArrayOutputStream out = new ByteArrayOutputStream();
/*-----------------------------------------------------------------------------------------*/
/*--------- End Common Code ---------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/      


/*-----------------------------------------------------------------------------------------*/
/*--------- FOP-only code -----------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/            
      org.w3c.dom.Document foDoc = (org.w3c.dom.Document)domResult.getNode();

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

      response.setContentType("application/pdf");
/*-----------------------------------------------------------------------------------------*/
/*--------- End FOP-only Code -------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/            


/*-----------------------------------------------------------------------------------------*/
/*--------- HSSF-only  Code ---------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/            
     // Set up the HSSF Serializer to serialize the Result to an output stream.
      org.apache.cocoon.serialization.HSSFSerializer ser 
        = new org.apache.cocoon.serialization.HSSFSerializer();
      ser.initialize();  // don't forget this line or you get a null pointer exception
      ser.setOutputStream(out);
    
      // The Serializer functions as a SAX ContentHandler.
      javax.xml.transform.Result result 
        = new javax.xml.transform.sax.SAXResult((org.xml.sax.ContentHandler)ser);
      transformer.transform(xml, result);

      response.setContentType("application/vnd.ms-excel");
/*-----------------------------------------------------------------------------------------*/
/*--------- End HSSF-only Code ------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/            


/*-----------------------------------------------------------------------------------------*/
/*--------- Begin common code again -------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/      
      byte[] content = out.toByteArray();
      response.setContentLength(content.length);
      response.getOutputStream().write(content);
      response.getOutputStream().flush();
/*-----------------------------------------------------------------------------------------*/
/*--------- End ---------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/      



> -----Original Message-----
> From: Adam Shelley [mailto:ashelley@inlandkwbc.com]
> Sent: Tuesday, April 29, 2003 3:53 PM
> To: fop-user@xml.apache.org
> Subject: RE: Seeking advice on HSSF
> 
> 
> I am currently seeking the same information.  I'll post some 
> info if I come
> up with something.
> 
> -Adam
> 
> -----Original Message-----
> From: Savino, Matt C [mailto:Matt.C.Savino@questdiagnostics.com]
> Sent: April 29, 2003 3:41 PM
> To: fop-user@xml.apache.org
> Subject: Seeking advice on HSSF
> 
> 
> Hi, I figured I'd ask this here just in case anyone had any 
> opinions. We
> have a web-app which currently uses FOP in a servlet to 
> generate PDFs. We
> are looking into using HSSF to generate .xls files in a 
> similar fashion. Is
> anyone out there swapping FOP and HSSF like this? I've read all of the
> HSSF/POI and Cocoon-HSSF-Serializer stuff from the Apache 
> site and I am
> still unclear on how I would go from XML to GMR (via XSLT) to 
> .xls (via
> serializer) as I would with FOP. Do we have to go through 
> cocoon? It seems
> like a lot of overhead for this one function.
> 
> I know this is a little off topic but if any FOP people out here have
> experience on this I would really appreciate hearing it.
> 
> 
> thanks a lot,
> Matt Savino
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: fop-user-help@xml.apache.org
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: fop-user-help@xml.apache.org
> 
> 
> 

Re: Seeking advice on HSSF

Posted by Jeremias Maerki <de...@greenmail.ch>.
I see. Please do have a look at the Example*.java files. They don't use
XSLTInputHandler anymore. Just plain JAXP as you are used to. Going via
DOM just means you generate quite a lot of little objects which slows
the JVM and uses up memory.

On 01.05.2003 19:50:59 Savino, Matt C wrote:
> Thanks Jeremias. I think the main reason we are parsing the fodoc into a DOM is so we can serialize the FO out to a file for debugging (it's either that or do the transformation twice). I'm actually in the process of altering the HSSF stuff now so I can debug the GMR output. On your adive I will change both of them back to a stream source for production. Although I seem to remember experimenting extensively when we first started with SAX vs. DOM input sources using the FOP XSLTInputHandler, SAXHandler, etc. As I recall, we didn't see much difference in performance. But I will try it again for this round and let you know how it goes. Actually here is the old commented out code from these tests:
> 
>       File xmlFile  = new File(xmlFilename);
>       File xsltFile = new File(xsltFilename);
>       org.apache.fop.apps.InputHandler inputHandler2 = new org.apache.fop.apps.XSLTInputHandler(xmlFile, xsltFile);
>       org.xml.sax.XMLReader parser2 = inputHandler2.getParser();
>       driver.render(parser2, inputHandler2.getInputSource());
> 
> Is there a more efficient method I should try?


Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org


Re: Seeking advice on HSSF

Posted by Jeremias Maerki <de...@greenmail.ch>.
A few comments inline...

On 01.05.2003 18:47:43 Savino, Matt C wrote:
> Ok here it is. I read all of the POIFS and Coccoon stuff an found there
> is some demand to use the HSSF serializer as a stand-alone piece, ala
> FOP. (Thus avoiding all of the Coccoon overhead and 20MB .war file.)
> This actually used to exist separately that but the POI guys donated it
> to be folded into Coccoon. Supposedly there is a project on the Jakarta
> sandbox called Morphos to run the HSSF serializer separately, but I
> couldn't find it.

Morphos got deleted. I'm still trying to push Nicola Ken Barozzi into a
revival. But he's too deep into work at the moment. The new thing would
be developed in the Krysalis Community Project, probably as "Piper".
Morphos/Piper would also be a nice API for doing XSL:FO transformation.

> No problem, since it seems simple enough anyway (so far). 

<snip/>

> /*-----------------------------------------------------------------------------------------*/
> /*--------- This code is common to FOP and HSSF -------------------------------------------*/
> /*-----------------------------------------------------------------------------------------*/      	javax.xml.parsers.DocumentBuilderFactory dFactory 
>         = javax.xml.parsers.DocumentBuilderFactory.newInstance();
>       dFactory.setNamespaceAware(true);
>       javax.xml.parsers.DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
> 
>       org.w3c.dom.Document xmlDoc = dBuilder.parse(xmlFile);
>       javax.xml.transform.dom.DOMSource xmlDomSource = new javax.xml.transform.dom.DOMSource(xmlDoc);
> 
>       org.w3c.dom.Document xslDoc = dBuilder.parse(xsltFile);
>       javax.xml.transform.dom.DOMSource xslDomSource = new javax.xml.transform.dom.DOMSource(xslDoc);

Why do you parse the whole thing into a DOM? That's very inefficient
when you subsequently give it to JAXP anyway. A StreamSource would be a
lot better!

Have a look at the Example*.java in the latest FOP distribution under
examples/embedding.

> 
>       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();
>   
>       ByteArrayOutputStream out = new ByteArrayOutputStream();
> /*-----------------------------------------------------------------------------------------*/
> /*--------- End Common Code ---------------------------------------------------------------*/
> /*-----------------------------------------------------------------------------------------*/      
> 
> 
> /*-----------------------------------------------------------------------------------------*/
> /*--------- FOP-only code -----------------------------------------------------------------*/
> /*-----------------------------------------------------------------------------------------*/            
>       org.w3c.dom.Document foDoc = (org.w3c.dom.Document)domResult.getNode();

Again, don't do that. Use SAX as with the HSSF serializer:

        Result result = new SAXResult(driver.getContentHandler());
        transformer.transform(xml, result);
        
See how similar the code is to the one for HSSF?

>       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);
> 
>       response.setContentType("application/pdf");
> /*-----------------------------------------------------------------------------------------*/
> /*--------- End FOP-only Code -------------------------------------------------------------*/
> /*-----------------------------------------------------------------------------------------*/            
> 
> 
> /*-----------------------------------------------------------------------------------------*/
> /*--------- HSSF-only  Code ---------------------------------------------------------------*/
> /*-----------------------------------------------------------------------------------------*/            
>      // Set up the HSSF Serializer to serialize the Result to an output stream.
>       org.apache.cocoon.serialization.HSSFSerializer ser 
>         = new org.apache.cocoon.serialization.HSSFSerializer();
>       ser.initialize();  // don't forget this line or you get a null pointer exception
>       ser.setOutputStream(out);
>     
>       // The Serializer functions as a SAX ContentHandler.
>       javax.xml.transform.Result result 
>         = new javax.xml.transform.sax.SAXResult((org.xml.sax.ContentHandler)ser);
>       transformer.transform(xml, result);
> 
>       response.setContentType("application/vnd.ms-excel");
> /*-----------------------------------------------------------------------------------------*/
> /*--------- End HSSF-only Code ------------------------------------------------------------*/
> /*-----------------------------------------------------------------------------------------*/            
> 
> 
> /*-----------------------------------------------------------------------------------------*/
> /*--------- Begin common code again -------------------------------------------------------*/
> /*-----------------------------------------------------------------------------------------*/      
>       byte[] content = out.toByteArray();
>       response.setContentLength(content.length);
>       response.getOutputStream().write(content);
>       response.getOutputStream().flush();
> /*-----------------------------------------------------------------------------------------*/
> /*--------- End ---------------------------------------------------------------------------*/
> /*-----------------------------------------------------------------------------------------*/      


Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org