You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Andre Juffer <aj...@cc.oulu.fi> on 2011/09/20 10:14:39 UTC

Cocoon3: string-template; collections

Hi,

I am dealing with the following. I created a simple REST resource, like 
(without setters):

@Path("/test")
public class TestResource {

     @GET
     @Path("/{id}")
     public Response getTest(@PathParam("id") String id)
     {
         TestDTO testDTO = this.testFacade.find(id);
         Map<String, Object> map = new HashMap<String, Object>();
         map.put("test", testDTO);
         return 
URLResponseBuilder.newInstance("servlet:tribc-tct:/test.json", map).build();
     }
}

The TestFacade returns a DTO (data transfer object) and this DTO exposes 
a number of getters. The application must be able to return both XML and 
JSON. So, I place the DTO in a map and 'call' the sitemap for generated 
the response (here as JSON):

<map:match pattern="*.json">
   <map:generate type="string-template" src="xml/{map:1}.xml" />
   <map:transform src="xsl/xml2json.xsl" />
   <map:transform type="logasxml"/>
   <map:transform type="log"/>
   <map:serialize type="json" />  <!-- This serializer is my own -->
</map:match>

The test.xml looks like:

<test>
     <testId>$test.testId$</testId>
     <name>$test.name$</name>
     <!-- and more ...->
</test>

You can see that the DTO that was stored in the map is used in the 
test.xml to create an XML representation of the DTO. The above works 
fine (see StringTemplate [1]). The XML is subsequently transformed to JSON.

My problem is now how to use string-template when dealing with a 
collection. That is, the TestFacade returns a collection of DTOs 
(Collecton<TestDTO>), for which I need to make an XML (or JSON) 
representation, like:

<tests>
   <test>...</test>
   <test>...</test>
</tests>

My question: How to iterate through a collection of objects with 
string-template to create the above, if the collection is stored in a 
map. The StringTemplate pages are not very easy to read and I did not 
really find a clue how to handle this particular problem. In cocoon 2.2 
one could use <jx:forEach ...>, which was extremely convenient. I was 
hoping that there is a simular way with string-template.

Thanks for any help,

-- 
Andre H. Juffer              | Phone: +358-8-553 1161
Biocenter Oulu and           | Fax: +358-8-553-1141
Department of Biochemistry   | Email: andre.juffer@oulu.fi
University of Oulu, Finland  | WWW: www.biochem.oulu.fi/Biocomputing/
StruBioCat                   | WWW: www.strubiocat.oulu.fi
NordProt                     | WWW: www.nordprot.org
Triacle Biocomputing         | WWW: www.triacle-bc.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


RE: Cocoon3: string-template; collections

Posted by Robby Pelssers <Ro...@nxp.com>.
Hi Andre,

I found some example online but I haven't tested it personally. The example the syntax how to iterate


<ul>
    $orders: {order|
        <li>Order $order.OrderId$</li>
    }$
</ul>

Let me know if that works out for you.

Robby

-----Original Message-----
From: Andre Juffer [mailto:ajuffer@cc.oulu.fi] 
Sent: Tuesday, September 20, 2011 10:15 AM
To: Cocoon Users
Subject: Cocoon3: string-template; collections

Hi,

I am dealing with the following. I created a simple REST resource, like 
(without setters):

@Path("/test")
public class TestResource {

     @GET
     @Path("/{id}")
     public Response getTest(@PathParam("id") String id)
     {
         TestDTO testDTO = this.testFacade.find(id);
         Map<String, Object> map = new HashMap<String, Object>();
         map.put("test", testDTO);
         return 
URLResponseBuilder.newInstance("servlet:tribc-tct:/test.json", map).build();
     }
}

The TestFacade returns a DTO (data transfer object) and this DTO exposes 
a number of getters. The application must be able to return both XML and 
JSON. So, I place the DTO in a map and 'call' the sitemap for generated 
the response (here as JSON):

<map:match pattern="*.json">
   <map:generate type="string-template" src="xml/{map:1}.xml" />
   <map:transform src="xsl/xml2json.xsl" />
   <map:transform type="logasxml"/>
   <map:transform type="log"/>
   <map:serialize type="json" />  <!-- This serializer is my own -->
</map:match>

The test.xml looks like:

<test>
     <testId>$test.testId$</testId>
     <name>$test.name$</name>
     <!-- and more ...->
</test>

You can see that the DTO that was stored in the map is used in the 
test.xml to create an XML representation of the DTO. The above works 
fine (see StringTemplate [1]). The XML is subsequently transformed to JSON.

My problem is now how to use string-template when dealing with a 
collection. That is, the TestFacade returns a collection of DTOs 
(Collecton<TestDTO>), for which I need to make an XML (or JSON) 
representation, like:

<tests>
   <test>...</test>
   <test>...</test>
</tests>

My question: How to iterate through a collection of objects with 
string-template to create the above, if the collection is stored in a 
map. The StringTemplate pages are not very easy to read and I did not 
really find a clue how to handle this particular problem. In cocoon 2.2 
one could use <jx:forEach ...>, which was extremely convenient. I was 
hoping that there is a simular way with string-template.

Thanks for any help,

-- 
Andre H. Juffer              | Phone: +358-8-553 1161
Biocenter Oulu and           | Fax: +358-8-553-1141
Department of Biochemistry   | Email: andre.juffer@oulu.fi
University of Oulu, Finland  | WWW: www.biochem.oulu.fi/Biocomputing/
StruBioCat                   | WWW: www.strubiocat.oulu.fi
NordProt                     | WWW: www.nordprot.org
Triacle Biocomputing         | WWW: www.triacle-bc.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org