cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robby Pelssers <Robby.Pelss...@nxp.com>
Subject RE: [C2.2] Get Element from uploaded file
Date Thu, 17 Nov 2011 15:22:59 GMT
I actually had a similar need the other week and while I have used some XML-JAVA mappers like
Castor and XStream in the past I decided to try another approach where I generated JSON which
can be used from flowscript:


/** flowscript function to generate the pdf **/


function json2Object(json) {
                return eval("(" + json + ")");
}


function generatePDF() {
    var id = cocoon.parameters.id;  //e.g. PH3330L


    //var fileName = cocoon.getComponent('fileNameExtractor').getFileName(id);

    var output = new Packages.java.io.ByteArrayOutputStream();

    var uri = "extractData/" + id;
    //now we invoke a pipeline which returns a JSON string. E.g:
    /**
     *   {"id": "PH3330L","fileName": "PH3330L.pdf"},
     */
    cocoon.processPipelineTo(uri, null, output);

    var responseData = json2Object(output.toString());


    var response = cocoon.response;
    response.setHeader(
                "Content-Disposition",
                "attachment; filename=" + responseData.fileName;
    );
    cocoon.sendPage('source2pdf/' + id);

}


Sitemap:
----------
<!--
  {1} id: unique identifier for the source xml used to generate the PDF
-->
<map:match pattern="generatePdf/*">
  <map:call function="generatePDF">
    <map:parameter name="id" value="{1}"/>
  </map:call>
</map:match>

<map:match pattern="source2pdf/*">
  <map:generate src="source/{1}.xml"/> <!-- assume our sources are located in source
folder -->
  <map:transform src="xslt/source2poi.xslt"/>
  <map:serialize type="pdf"/>
</map:match>

<!--
  {1} id: unique identifier for the source xml used to generate the PDF
-->
<map:match pattern="extractData/*">
  <map:generate src="source/{1}.xml"/>
  <map:transform src="extractData.xslt"/>
  <map:serialize type="json"/>  <!-- you can quickly test with @type="text" -->
</map:match>



/**** ExtractData.xslt will generate a JSON representation of the DATA needed ****/

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Author: Robby Pelssers
-->

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xsl:output method="text" version="1.0" encoding="UTF-8"/>


  <xsl:template match="/">
    <xsl:text>{"id": "</xsl:text><xsl:value-of select="someXpathExpr"/><xsl:text>",
"fileName: "</xsl:text><xsl:value-of select="someXpathExpr"/>"}<xsl:text></xsl:text>
  </xsl:template>



</xsl:stylesheet>



Mime
View raw message