You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Christopher Oliver <re...@verizon.net> on 2004/01/04 08:03:01 UTC

Woody template transformer using jx macros

Attached is a first attempt to implement the behavior of the Woody 
template transformer with the macro facility of the JXTemplateGenerator. 
I'm not sure whether it's a hack or a design pattern, but since the 
Woody widgets know how to emit themselves onto a ContentHandler, the 
only real change I had to make to JXTemplateGenerator was to expose its 
xmlConsumer as a template variable (namely cocoon.consumer).

At first glance, these macros may seem a bit complex, but I think you'll 
find the attached is actually quite a bit simpler than 
WoodyTemplateTransformer. The additional benefit is that you also have 
the full power of  JXTemplate's in your Woody form templates (expression 
substitution, flow control, etc).

To use these macros, in your sitemap replace

  <generate src="blah.xml">
  <transform type="woody">

with
   <generate type="jx" src="blah.xml">

and modify your form template to import the jx template containing the 
macros, e.g:

<page xmlns:wt="http://apache.org/cocoon/woody/template/1.0"
        xmlns:wi="http://apache.org/cocoon/woody/instance/1.0"
        xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">


  <jx:import uri="forms/woody.jx"/>

  <title>Sample form</title>
  <content>
    <wt:form-template action="#{$continuation/id}.continue" method="POST">
     ...

Let me know what you think.

Regards,

Chris

Re: Woody template transformer using jx macros

Posted by Sylvain Wallez <sy...@apache.org>.
Vadim Gritsenko wrote:

> Sylvain Wallez wrote:

<snip/>

>> Christopher, can you confirm this? I would be good to have the 
>> "widget" or "currentWidget" variable available to crawl the widget 
>> tree from any point in the template.
>
>
> I don't know how to use those macros yet, but yes, "widget" is better 
> name than "ignored" :)


Obviously ;-)

To use the macros, just <jx:import uri="woody.jx"/> in the JX template 
as explained by Christopher in his first post.

<snip/>

> Gotcha! So the options are either to use JX Generator + Woody 
> Transformer, or JX Generator + Woody Macros library, where macros 
> library performs all the stuff transformer was doing before.


Exactly!

> Just need to see what is performance difference (and compare with bare 
> woody transformer).


Yep.

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
Orixo, the opensource XML business alliance  -  http://www.orixo.com



Re: Woody template transformer using jx macros

Posted by Vadim Gritsenko <va...@verizon.net>.
Sylvain Wallez wrote:

> Vadim Gritsenko wrote:
>
>> Sylvain Wallez wrote:
>> ....
>>
>>>    <jx:when test="$widget.getWidget("items").size > 0">
>>
>>
>>
>>
>> Thanks for JXTemplateGenerator example. I assume $widget here should 
>> be woody form, which is stored in the request attribute, and 
>> according to [1], method call syntax is a bit different from the 
>> above. So I ended up with:
>>
>>  <jx:when test="getWidget(getAttribute($request, "form"), 
>> "items")/size > 0">
>

Correction:
  <jx:when test="#{getWidget(getAttribute($request, "form"), 
"items")/size > 0}">


>> which actually worked for me, with JXTemplateGenerator. I'm now 
>> puzzled: how yours expression works?
>
>
>
> It doesn't work ;-)
> I wrote it by mimicing what's in Christopher's code without actually 
> testing it.
>
> Reading in more detail the JX implementation of <wt:form-template> and 
> <wt:repeater>, I found that the variable containing the current widget 
> is not "widget" but "ignored"!!
>
> Christopher, can you confirm this? I would be good to have the 
> "widget" or "currentWidget" variable available to crawl the widget 
> tree from any point in the template.


I don't know how to use those macros yet, but yes, "widget" is better 
name than "ignored" :)


>>> The important difference with the WoodyTransformer is that the 
>>> generator has access to the form model and therefore can produce a 
>>> page layout whose content depend on the actual values of this model.
>>
>>
>>
>>
>> I'm worried a bit about performance of such a beastie - macros on top 
>> of interpreted jxpath language executed in interpreted java machine...
>
>
>
> Yep. But this also avoids an additional transformer.
>
>>> The above example is however a bit too simplistic since the same 
>>> result could be achieved with the WoodyTransformer. The jx:when test 
>>> expression would then be something like 
>>> "$woody-form.getWidget("items").size > 0".
>>
>>
>>
>>
>> Now I'm lost. I have not found code in neither 
>> EffectWidgetReplacingPipe nor in WoodyTemplateTransformer which would 
>> implement any jx: functionality, except commented-out translateText 
>> method. What do I miss?
>
>
>
> Maybe I wasn't clear. A "regular" JXTemplate (without Woody macros) 
> can access the form model to dynamically build the form template 
> that's later expanded by WoodyTransformer.
>
> But if we want to implement the repeater example (different layout 
> depending on the repeater size) using this combination, we can do it 
> only for top-level repeaters and not for nested ones, as the iteration 
> on repeaters is handled by the transformer, and JXTemplate can 
> therefore not access the properties of these nested repeaters (unless 
> the JX template implements itself that iteration, but it is then 
> equivalent to the macros).
>
> Hope it's clearer.


Gotcha! So the options are either to use JX Generator + Woody 
Transformer, or JX Generator + Woody Macros library, where macros 
library performs all the stuff transformer was doing before. Just need 
to see what is performance difference (and compare with bare woody 
transformer).

Vadim



Re: Woody template transformer using jx macros

Posted by Tim Larson <ti...@yahoo.com>.
--- Christopher Oliver <re...@verizon.net> wrote:
> As regards performance, WoodyTemplateTransformer is faster. I did some 
> informal testing using cocoon.processPipelineTo() without the xslt 
> transformation steps using the following script:

I wonder how the performance of the WidgetReplacingPipe compares to the
EffectWidgetReplacingPipe, since they are designed differently.
Of course, you could not test the "form_model_gui_template.xml" with the
first one, but the comparison with the other file would be interesting.
Testing the WidgetReplacingPipe should just be a matter of changing
which class the WoodyTemplateTransformer extends and re-running the test.

--Tim Larson


__________________________________
Do you Yahoo!?
Find out what made the Top Yahoo! Searches of 2003
http://search.yahoo.com/top2003

Re: Woody template transformer using jx macros

Posted by Christopher Oliver <re...@verizon.net>.
Sylvain Wallez wrote:

> Vadim Gritsenko wrote:
>
>
<snip>

>> which actually worked for me, with JXTemplateGenerator. I'm now 
>> puzzled: how yours expression works?
>
>
>
> It doesn't work ;-)
> I wrote it by mimicing what's in Christopher's code without actually 
> testing it.
>
> Reading in more detail the JX implementation of <wt:form-template> and 
> <wt:repeater>, I found that the variable containing the current widget 
> is not "widget" but "ignored"!!
>
> Christopher, can you confirm this? I would be good to have the 
> "widget" or "currentWidget" variable available to crawl the widget 
> tree from any point in the template.
>
In that implementation I used an explicit stack to maintain the current 
widget. So you would have to use ${woodyStack.peek()} to access it.

This is a general issue with JXTemplate's: what should the rules about 
variable scoping be? Currently, any variables you set within a macro are 
visible within the body when it is evaluated (and hide variables with 
the same names defined by enclosing tag). I've attached an alternative 
implementation that uses the execution stack rather than an explicit 
stack. In this implementation the current widget is available as a 
variable ${woodyCurrent}. However this behavior is too permissive and 
can easily lead to accidental name clashes.

I need to think more about this but it should at least be possible to 
create variables that are private to the macro.

As regards performance, WoodyTemplateTransformer is faster. I did some 
informal testing using cocoon.processPipelineTo() without the xslt 
transformation steps using the following script:
   
    var count = 2000;
    var start = new Date();
    for (var i = 0; i < count; i++) {
      var strm = new java.io.ByteArrayOutputStream();
      cocoon.processPipelineTo(uri, bizData, strm);
    }
    var end = new Date();
    print("Elapsed = " + ((end - start)/1000));
    print("Per instance = " + (((end - start)/1000)/count));

For "form1_template.xml" I got the following results:

<generate type=jx src="..."/>  
<serialize/>

    Elapsed = 26.788
    Per instance = 0.013394

<generate src="...">
<transform type="woody">
<serialize/>
   
    Elapsed = 24.225
    Per instance = 0.0121125

For "form_model_gui_template.xml":

<generate type=jx src="..."/>  
<serialize/>
   
    Elapsed = 19.408
    Per instance = 0.09704
   
<generate src="...">
<transform type="woody">
<serialize/>
   
    Elapsed = 8.632
    Per instance = 0.04316
   

I'm not sure yet why the difference is more pronounced in the latter case.

Of course the performance of the whole pipeline is dominated by xslt 
processing. When I included the remainder of the pipeline I got the 
following results for "form1_template.xml":

<generate type=jx src="..."/>  
<...
    Elapsed = 174.19
    Per instance = 0.087095

<generate src="...">
<transform type="woody">
<...
    Elapsed = 163.234
    Per instance = 0.08161700000000001


Regards,

Chris

Re: Woody template transformer using jx macros

Posted by Sylvain Wallez <sy...@apache.org>.
Vadim Gritsenko wrote:

> Sylvain Wallez wrote:
> ....
>
>>    <jx:when test="$widget.getWidget("items").size > 0">
>
>
>
> Thanks for JXTemplateGenerator example. I assume $widget here should 
> be woody form, which is stored in the request attribute, and according 
> to [1], method call syntax is a bit different from the above. So I 
> ended up with:
>
>  <jx:when test="getWidget(getAttribute($request, "form"), 
> "items")/size > 0">
>
> which actually worked for me, with JXTemplateGenerator. I'm now 
> puzzled: how yours expression works?


It doesn't work ;-)
I wrote it by mimicing what's in Christopher's code without actually 
testing it.

Reading in more detail the JX implementation of <wt:form-template> and 
<wt:repeater>, I found that the variable containing the current widget 
is not "widget" but "ignored"!!

Christopher, can you confirm this? I would be good to have the "widget" 
or "currentWidget" variable available to crawl the widget tree from any 
point in the template.

>> The important difference with the WoodyTransformer is that the 
>> generator has access to the form model and therefore can produce a 
>> page layout whose content depend on the actual values of this model.
>
>
>
> I'm worried a bit about performance of such a beastie - macros on top 
> of interpreted jxpath language executed in interpreted java machine...


Yep. But this also avoids an additional transformer.

>> The above example is however a bit too simplistic since the same 
>> result could be achieved with the WoodyTransformer. The jx:when test 
>> expression would then be something like 
>> "$woody-form.getWidget("items").size > 0".
>
>
>
> Now I'm lost. I have not found code in neither 
> EffectWidgetReplacingPipe nor in WoodyTemplateTransformer which would 
> implement any jx: functionality, except commented-out translateText 
> method. What do I miss?


Maybe I wasn't clear. A "regular" JXTemplate (without Woody macros) can 
access the form model to dynamically build the form template that's 
later expanded by WoodyTransformer.

But if we want to implement the repeater example (different layout 
depending on the repeater size) using this combination, we can do it 
only for top-level repeaters and not for nested ones, as the iteration 
on repeaters is handled by the transformer, and JXTemplate can therefore 
not access the properties of these nested repeaters (unless the JX 
template implements itself that iteration, but it is then equivalent to 
the macros).

Hope it's clearer.

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
Orixo, the opensource XML business alliance  -  http://www.orixo.com



Re: Woody template transformer using jx macros

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Sylvain Wallez wrote:
...

>    <jx:when test="$widget.getWidget("items").size > 0">


Thanks for JXTemplateGenerator example. I assume $widget here should be 
woody form, which is stored in the request attribute, and according to 
[1], method call syntax is a bit different from the above. So I ended up 
with:

  <jx:when test="getWidget(getAttribute($request, "form"), "items")/size 
 > 0">

which actually worked for me, with JXTemplateGenerator. I'm now puzzled: 
how yours expression works?


> The important difference with the WoodyTransformer is that the 
> generator has access to the form model and therefore can produce a 
> page layout whose content depend on the actual values of this model.


I'm worried a bit about performance of such a beastie - macros on top of 
interpreted jxpath language executed in interpreted java machine...


> The above example is however a bit too simplistic since the same 
> result could be achieved with the WoodyTransformer. The jx:when test 
> expression would then be something like 
> "$woody-form.getWidget("items").size > 0".


Now I'm lost. I have not found code in neither EffectWidgetReplacingPipe 
nor in WoodyTemplateTransformer which would implement any jx: 
functionality, except commented-out translateText method. What do I miss?

Vadim

[1] http://jakarta.apache.org/commons/jxpath/users-guide.html



Re: Woody template transformer using jx macros

Posted by Sylvain Wallez <sy...@apache.org>.
Antonio Gallardo wrote:

>Hi Sylvain:
>
>It is great, can you wikify it? ;)
>  
>

Mmmh... a new woody sample may be better, don't you think?

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
Orixo, the opensource XML business alliance  -  http://www.orixo.com



Re: Woody template transformer using jx macros

Posted by Antonio Gallardo <ag...@agsoftware.dnsalias.com>.
Hi Sylvain:

It is great, can you wikify it? ;)

Best Regards,

Antonio Gallardo

Sylvain Wallez dijo:
> Vadim Gritsenko wrote:
>
>> Sylvain Wallez wrote:
>>
>>> Christopher Oliver wrote:
>>>
>>>> Continuing my conversation with myself....
>>>
>>>
>>> Well, continue, this conversation is very interesting ;-)
>>>
>>> Seriously, having woody templates in a generator is IMO a must have,
>>> as there are many cases where just "inserting" widgets is not enough,
>>> and the page structure depends on the properties of the widgets.
>>>
>>> An example is repeaters, where we may want a different display for
>>> empty repeaters and non-empty repeaters.
>>
>>
>>
>> Can you guys show a quick example of this? I'm not quite following all
>> the new development here...
>
>
> Consider the classical shopping cart. The form has an "items" repeater
> containing an "item" field and a "select" boolean field. Two actions
> manage the shopping cart: "addItem" and "removeItem".
>
> When the cart is empty, we don't want to show a table with only headers
> nor the "removeItem" button. The template may then be something like:
>
> <wt:form-template>
>   <jx:choose>
>     <jx:when test="$widget.getWidget("items").size > 0">
>       There are no items in your cart.
>       <wt:widget id="addItem"/>
>     </jx:when>
>     <jx:otherwise>
>       <table>
>         <tr>
>           <th>Item</th>
>           <th>Select</th>
>         </tr>
>         <wt:repeater-widget id="items">
>           <tr>
>             <td><wt:widget id="item"></td>
>             <td><wt:widget id="select"></td>
>           </tr>
>         </wt:repeater-widget>
>         <wt:widget id="addItem"/>
>         <wt:widget id="removeItem"/>
>       </table>
>     </jx:otherwise>
>   </jx:choose>
> </wt:form-template>
>
> The important difference with the WoodyTransformer is that the generator
> has access to the form model and therefore can produce a page layout
> whose content depend on the actual values of this model.
>
> The above example is however a bit too simplistic since the same result
> could be achieved with the WoodyTransformer. The jx:when test expression
> would then be something like "$woody-form.getWidget("items").size > 0".
>
> But in the case of nested repeaters, only the generator approach allows
> to have a form layout that depends on the properties of child repeaters,
> since the child repeater to be considered depends on the iteration index
> on the parent repeater.
>
> Hope I was clear and it explains the use case!
>
> Sylvain
>
> --
> Sylvain Wallez                                  Anyware Technologies
> http://www.apache.org/~sylvain           http://www.anyware-tech.com
> { XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
> Orixo, the opensource XML business alliance  -  http://www.orixo.com
>
>


Re: Woody template transformer using jx macros

Posted by Sylvain Wallez <sy...@apache.org>.
Vadim Gritsenko wrote:

> Sylvain Wallez wrote:
>
>> Christopher Oliver wrote:
>>
>>> Continuing my conversation with myself....
>>
>>
>> Well, continue, this conversation is very interesting ;-)
>>
>> Seriously, having woody templates in a generator is IMO a must have, 
>> as there are many cases where just "inserting" widgets is not enough, 
>> and the page structure depends on the properties of the widgets.
>>
>> An example is repeaters, where we may want a different display for 
>> empty repeaters and non-empty repeaters.
>
>
>
> Can you guys show a quick example of this? I'm not quite following all 
> the new development here...


Consider the classical shopping cart. The form has an "items" repeater 
containing an "item" field and a "select" boolean field. Two actions 
manage the shopping cart: "addItem" and "removeItem".

When the cart is empty, we don't want to show a table with only headers 
nor the "removeItem" button. The template may then be something like:

<wt:form-template>
  <jx:choose>
    <jx:when test="$widget.getWidget("items").size > 0">
      There are no items in your cart.
      <wt:widget id="addItem"/>
    </jx:when>
    <jx:otherwise>
      <table>
        <tr>
          <th>Item</th>
          <th>Select</th>
        </tr>
        <wt:repeater-widget id="items">
          <tr>
            <td><wt:widget id="item"></td>
            <td><wt:widget id="select"></td>
          </tr>
        </wt:repeater-widget>
        <wt:widget id="addItem"/>
        <wt:widget id="removeItem"/>
      </table>
    </jx:otherwise>
  </jx:choose>
</wt:form-template>

The important difference with the WoodyTransformer is that the generator 
has access to the form model and therefore can produce a page layout 
whose content depend on the actual values of this model.

The above example is however a bit too simplistic since the same result 
could be achieved with the WoodyTransformer. The jx:when test expression 
would then be something like "$woody-form.getWidget("items").size > 0".

But in the case of nested repeaters, only the generator approach allows 
to have a form layout that depends on the properties of child repeaters, 
since the child repeater to be considered depends on the iteration index 
on the parent repeater.

Hope I was clear and it explains the use case!

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
Orixo, the opensource XML business alliance  -  http://www.orixo.com



Re: Woody template transformer using jx macros

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Sylvain Wallez wrote:

> Christopher Oliver wrote:
>
>> Continuing my conversation with myself....
>
>
>
> Well, continue, this conversation is very interesting ;-)
>
> Seriously, having woody templates in a generator is IMO a must have, 
> as there are many cases where just "inserting" widgets is not enough, 
> and the page structure depends on the properties of the widgets.
>
> An example is repeaters, where we may want a different display for 
> empty repeaters and non-empty repeaters.


Can you guys show a quick example of this? I'm not quite following all 
the new development here...

Vadim



Re: Woody template transformer using jx macros

Posted by Sylvain Wallez <sy...@apache.org>.
Christopher Oliver wrote:

> Continuing my conversation with myself....


Well, continue, this conversation is very interesting ;-)

Seriously, having woody templates in a generator is IMO a must have, as 
there are many cases where just "inserting" widgets is not enough, and 
the page structure depends on the properties of the widgets.

An example is repeaters, where we may want a different display for empty 
repeaters and non-empty repeaters.

BTW, this work heavily relies on jx:evalBody which I never heard of 
before. This greatly enhances the power of macros.

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
Orixo, the opensource XML business alliance  -  http://www.orixo.com



RE: Woody template transformer using jx macros

Posted by Reinhard Poetz <re...@apache.org>.
From: Christopher Oliver [mailto:res1cf5x@verizon.net] 

> Continuing my conversation with myself....

;-)

I haven't had the time to try it within an example but from what I see
in your mails it is *really cool*.

--
Reinhard


Re: Woody template transformer using jx macros

Posted by Christopher Oliver <re...@verizon.net>.
Continuing my conversation with myself....

I updated the Woody block and noticed there are quite a few new things 
there. In order to implement the <wt:class> and <wt:new> tags I extended 
JXTemplateGenerator to support dynamic evaluation of custom tags. I just 
checked in this update. See the documentation. The attached file 
contains implementations of all of the <wt:xxx> tags including class, 
new, union, case and struct.

Regards,

Chris

Christopher Oliver wrote:

> The attached fixes a couple of bugs and adds the wt:continuation-id 
> tag. I've tested it with all the forms/{1}_template.xml examples in 
> the woody samples.
>
> Regards,
> Chris
>
> Christopher Oliver wrote:
>
>> Attached is a first attempt to implement the behavior of the Woody 
>> template transformer with the macro facility of the JXTemplateGenerator. 
>
>
> <snip>
>
>------------------------------------------------------------------------
>
>
><snip>
>


Re: Woody template transformer using jx macros

Posted by Christopher Oliver <re...@verizon.net>.
The attached fixes a couple of bugs and adds the wt:continuation-id tag. 
I've tested it with all the forms/{1}_template.xml examples in the woody 
samples.

Regards,
Chris

Christopher Oliver wrote:

> Attached is a first attempt to implement the behavior of the Woody 
> template transformer with the macro facility of the JXTemplateGenerator. 

<snip>