« Return to Thread: [C2.2] Get Element from uploaded file

RE: [C2.2] Get Element from uploaded file

by Robby Pelssers-4 :: Rate this Message:

| View in Thread

Some parts of this message have been removed. Learn more about Nabble's security policy.

I just published a little write-up about a similar use case as you described on http://robbypelssers.blogspot.com/2011/11/apache-cocoon-from-xml-to-json-as.html

 

Kind regards,

Robby

 

 

From: Matthias Müller [mailto:pymote@...]
Sent: Friday, November 18, 2011 8:34 AM
To: users@...
Subject: Re: [C2.2] Get Element from uploaded file

 

I extended your first approach:

 

in the flow script I use the inputStream from the uploaded file to feed a XpathEvaluator class in combination with the an xpath expression.

 

var inputStream = widget.getValue().getInputStream();
var xpath = "/my/path/to/element";
var result = cocoon.getComponent('XPathEvaluator').evaluate(inputStream, xpath);

 

Thanks again, Matthias

 

 


Von: Robby Pelssers <Robby.Pelssers@...>
An: Matthias Müller <pymote@...>; "users@..." <users@...>
Gesendet: 16:22 Donnerstag, 17.November 2011
Betreff: RE: [C2.2] Get Element from uploaded file

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> 

 

 

 

 « Return to Thread: [C2.2] Get Element from uploaded file