Re: A generator with sitemap parameter in C3

Francesco Chicchiriccò <[email protected]>
Newsgroups gmane.text.xml.cocoon.user
Message-ID <[email protected]>
On 11/02/2013 13:55, gelo1234 wrote:
> Thank you Francesco. That is what I was looking for!! :)
>
> So the setup(...) has access to sitemap parameters :) Why not 
> setConfiguration(..) ?
>
> Does call:
> this.setup((Map<String, Object>) configuration)
> strips off?/adds? some unwanted/additional data for sitemap-servlet 
> framework ?
>
> As i recall setConfiguration is for Java only (without 
> sitemap-servlet) Cocoon use and setup is for standard sitemap-servlet 
> ? Is that correct ?

This is correct: I've just reported my own default when I start 
developing new sitemap components, with purpose of staying as much 
compatible with sitemap and non-sitemap usage.

Regards.

> 2013/2/11 Francesco Chicchiriccò <[email protected] 
> <mailto:[email protected]>>
>
>     On 11/02/2013 13:27, gelo1234 wrote:
>>     Ohh...ok that clarifies a problem a bit. So if the generator
>>     declaration inside sitemap.xmap does NOT have src=".." attribute
>>     i won't get this piece of code [1] executed:
>>
>>     if (this.source != null) {
>>                  Object attributesObj = configuration.get("attributes");
>>                  if (attributesObj != null && attributesObj instanceof Map) {
>>                  ...
>>     }
>>
>>     In case of the custom example given before:
>>
>>     <map:generate type="some-custom">
>>       ...
>>     </map:generate>
>>
>>     it does NOT have src attribute (Not every generator got src
>>     attribute). So the question is still valid, how to get to those
>>     <map:parameter .../> values in such case ? Simply by adding
>>     useless src="" attribute to <map:generate ..> e.g.
>>
>>     <map:generate type="some-custom" src="http://www.w3c.org"> ?
>>
>>     or through the use of some Helper class? as it is in [2] (to
>>     Request Parameters instead of <map:parameter name="a"
>>     value="jexl:cocoon.request.a"/>) with HttpContextHelper - wonder
>>     if there is any equivalent Helper class for Cocoon Sitemap :)
>
>     Hi,
>     [2] injects HTTP request parameters as sitemap parameters: here's
>     why the usage of HttpContextHelper.
>
>     Let's suppose you want to implement a generator able to deal with:
>
>     <map:generate type="mycustom">
>       <map:parameter name="customParam" value="customValue"/>
>     </map:generate>
>
>     Then you need to
>
>     1. create class MyCustomGenerator extending AbstractSAXGenerator
>
>     2. implement setup() and setConfiguration() - an example
>     implementation could be
>
>         @Override
>         public void setConfiguration(final Map<String, ? extends
>     Object> configuration) {
>             this.setup((Map<String, Object>) configuration);
>         }
>
>         @Override
>         public void setup(final Map<String, Object> parameters) {
>             if (parameters == null) {
>                 return;
>             }
>
>     // do something with parameters.get("customParam")
>         }
>
>     3. add a file
>     src/main/resources/META-INF/cocoon/spring/my-sitemap-components.xml with
>     the following content:
>
>     <beans xmlns="http://www.springframework.org/schema/beans"
>     <http://www.springframework.org/schema/beans>
>            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>     <http://www.w3.org/2001/XMLSchema-instance>
>            xsi:schemaLocation="http://www.springframework.org/schema/beans
>     http://www.springframework.org/schema/beans/spring-beans.xsd">
>
>       <bean name="generator:mycustom" class="xx.yyy.MyCustomGenerator"
>     scope="prototype"/>
>
>     </beans>
>
>     HTH
>
>     Regards.
>
>>     [1]
>>     https://svn.apache.org/repos/asf/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java
>>     [2]
>>     https://svn.apache.org/repos/asf/cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/component/RequestParametersGenerator.java
>>
>>     2013/2/11 Francesco Chicchiriccò <[email protected]
>>     <mailto:[email protected]>>
>>
>>         On 11/02/2013 12:07, gelo1234 wrote:
>>
>>             Question 1:
>>             Can we have some example of this one ?
>>
>>             <map:generate type="some-custom">
>>                <map:parameter name="a" value="1"/>
>>                <map:parameter name="b" value="2"/>
>>             </map:generate>
>>
>>             I mean, how to get sitemap parameter value from Java
>>             Generator code ?
>>             btw. Request Parameters are obtained by the use of
>>             HttpContextHelper, is this correct ?
>>
>>
>>         No: you can access map:parameter elements from within Java
>>         code in the same way how this is done in the XSLTTransformer
>>         [1]: consider that "source" is the only pre-defined parameter
>>         you have in any pipeline component, mapped to the 'src' XML
>>         attribute.
>>
>>
>>             Question 2:
>>             What is the best (optimal) way to include XML content
>>             generated from
>>             servlet/controller?
>>
>>             <x:include .... src="servlet:/..."/>
>>
>>             If the servlet is calling a REST controller there are 2
>>             invocations involved (one to gather data, the other one
>>             (with e.g. string-template) to actually serialize the
>>             data). Is there any way that REST controller could
>>             serialize the data itself ? without invoking another
>>             servlet/Page ?
>>
>>
>>         Not that I know: anyway I am using similar includes in
>>         different production environments with high load and I've
>>         experienced no troubles so far.
>>
>>
>>             Question 3:
>>             If the generator is included in some transformer phase
>>             (via x:include)
>>             does it still have access to original Request Parameters
>>             when invoked with:
>>
>>             <x:include ... src="servlet:/generate?par1=a&par2=b/>
>>
>>             or they are lost (original request params) and
>>             substituted by new ones (par1, par2 from the line above)?
>>
>>
>>         They are lost: you need to explicitly pass them though.
>>
>>         Regards.
>>
>>         [1]
>>         https://svn.apache.org/repos/asf/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java
>>
-- 
Francesco Chicchiriccò

ASF Member, Apache Syncope PMC chair, Apache Cocoon PMC Member
http://people.apache.org/~ilgrosso/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.