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:[EMAIL PROTECTED]
> Sent: Tuesday, April 29, 2003 3:53 PM
> To: [EMAIL PROTECTED]
> 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:[EMAIL PROTECTED]
> Sent: April 29, 2003 3:41 PM
> To: [EMAIL PROTECTED]
> 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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

<<attachment: HSSF_example.zip>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to