You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Dario Liberman <ve...@ineba.net> on 2003/02/26 18:19:53 UTC
VM body node execution
Hello,
I am new to velocity, I am from the near webmacro world. :-)
I was thinking about migrating to Velocity.
And when I came to the VMs I found it interesting, but would like to
increment its expressive power now that I got tempted to use it.
The improvement goes subject to this question:
I would like to have access inside a VM to the body node of the macro's
callee, is it possible?
I am new to this, and I would like to accomplish it if it is possible. Is
there any guru that can help me out?
Any comments are welcome.
Here I describe what uses I would be giving to it, with examples, so that I
can explain myself.
for example:
code->
#macro( tostring $strvar )
#set ( $strvar = "$macro_body") ## this would do a kind of
Velocity.execute($current_context, new
stringwriter(req.get("$strvar")),macro_body_node)
#end
<-code
so when I use it:
code->
#tostring ( $mystrvar)
VM code here, with any statements, directives, etc.
#end
$mytool.write2File($mystrvar,"/vmexec_output.html")
<-code
I get the output of this block in a separate file.
It can be even more interesting:
code->
#macro( applyxsl $xslfile )
#tostring ($newvar)
$macro_body
#end
$mytool.applyxsl($newvar,$xslfile)
#end
#macro (CanAccess $resource)
#if ($currentUser.canAccess($resource) )
$macro_body
#else
access denied
#end
#end
<-code
so when I use it:
code->
#applyxsl ( "mystyle.xsl")
<somexml>
#canAccess("hello")
<menu url="hello.html">hello</menu>
#end
more xml mixed with velocity
</somexml>
#end
<-code
I get some parts of my html defined with custom tags that I transform with
my xsl of desire.
We can even have a macro that redirects output, or that makes a new loop
like structure, anything.
something like that:
code->
#macro( foreachother $looping $looper )
#foreach $looping in $looper
#if ($foreach_couter % 2 == 1)
$macro_body
#end
#end
#end
#set ($mylist = ['you','are','the','least','beauty' ] )
#foreachother ("word", $mylist)
$word
#end
##should output: "you the beauty"
<-code
Ofcourse this is not much usefull, but perhaps a conditional other than the
parity of the loop counter is usefull, so to have a while VM, or alike.
And this is a little complicated, a transformer like the XSL above but
instead of using XSLT transformations, uses a much more powerfull
trasformer: DVSL
code->
#macro( applydvsl $dvslfile )
#tostring ($newvar)
$macro_body
#end
#set ( $xml = $mytool.bulidXML($newvar) ) ##build a dom tree out
of the string output of the block execution by vm (executed only once).
##*
initialize a DVSL instance to process the xml with the current velocity
engine, and current context, and specify dvslfile to parse for the
transformation.
By the way, is DVSL thread safe to be used in a servlet?
*##
#set ( $dvsl_instance =
$mytool.initializeDVSL($current_velocity_engine_instace,$current_context,$dv
slfile) )
#set ($output = $mytool.newStringWriter()) ##create a new string
writer to pass to dvsl
$dvsl_instance.transform($xml,$output) ##apply the transformation
$output ##expose content after first parse of velocity transformed
into xml and then transformed/filtered/xml_document_oriented_execution by
DVSL
#end
applydvsl ( "mydvsltransformation.dvsl")
<xml>
<queryDB query="select * from departments" into="depts"/>
<show var="depts">
#if (currentUser.LikesToSeeIds()) {
<value name="id"><title>Department Identification
Number</title></value>
}
<value name="description"><title>Department Name</title></value>
</show>
</xml>
#end
<-code
and the dvsl file could be something like:
code->
#match("xml")
<div bgcolor="$currentUser.getFavoriteColor()"> ## this $currentUser
comes from the context of the caller vm file that executed the applydvsl
macro
$context.applyTemplates()
</div>
#end
#match("queryDB")
#set ($query = $node.valueOf('query') )
#set ($into = $node.valueOf('into') )
#set ( $context.put($into, $mytool.execQuery_AddToContext($query) ) )
#end
#match("show")
#set ( $resultSet = $context.get( $node.valueOf('var') ) )
<table>
<tr>
#foreach ($column in node.children() )
<th> $column.valueOf('title.text()') </th>
#end
</tr>
#foreach ($rs in $resultSet )
<tr>
#foreach ($column in node.children() )
<td> $rs.get($column.valueOf('name')) </th>
#end
</tr>
#end
</table>
#end
<-code
and the output could be:
<div bgcolor="blue">
<table>
<tr>
<th>Department Identification Number</th>
<th>Department Name</th>
</tr>
<tr>
<td>13333</td>
<td>Brain overflow department</td>
</tr>
<tr>
<td>99444</td>
<td>Psychomaniac department</td>
</tr>
</table>
</div>
I can go on forever... this $macro_body would give a lot of power.
I guess that a kind of workarround is possible now... it would be to have
the body of this macros separated in a different file, or put inside a
string somehow (by the way, how can I put a chunk of text with enters, etc
into a string with Velocity?) and then give this string to the macro, and
the macro would execute this string using Velocity tools. But, I beleave
that to have it integrated is much better. Also it gets parsed only once
into a Velocity Node for multiple executions.
I hope to have managed to explain myself, and that not too much english
errors have splitted out there along my message.
Best regards,
Dario.
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
Re: DVSL
Posted by Jeff Schnitzer <je...@infohazard.org>.
DVSL works fine for this (although I don't know what the performance
characteristics are). You might want to look at Maverick - we have
a DVSL transform that does exactly what you are describing.
http://mav.sourceforge.net
Jeff Schnitzer
jeff@infohazard.org
On Thu, Feb 27, 2003 at 01:13:22PM -0300, Dario Liberman wrote:
> Hi all,
>
> has anyone used DVSL?
>
> Can I use it in a servlet ? I mean, is it thread safe?
>
> I am planning to have a main DVSL transformer for all my pages, so to give
> them the final look&feel, and be able to work with custom HTML tags.
>
> The view layer will be composed of:
> 1. Velocity execution of a vm file - Instead of outputting it directly
> to the response, I will capture it in a string.
> 2. build an XML Dom.
> 3. Execute a DVSL transformation with output to the response.
>
> The context, velocity instance, and alike, used in the face 1, should be
> reused in the face 3.
> Can it be done freely?
>
> different calls should reuse the instance so that a cached parsed tree is
> used both at 1 and 3.
>
> Thanks,
> Dario.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
DVSL
Posted by Dario Liberman <ve...@ineba.net>.
Hi all,
has anyone used DVSL?
Can I use it in a servlet ? I mean, is it thread safe?
I am planning to have a main DVSL transformer for all my pages, so to give
them the final look&feel, and be able to work with custom HTML tags.
The view layer will be composed of:
1. Velocity execution of a vm file - Instead of outputting it directly
to the response, I will capture it in a string.
2. build an XML Dom.
3. Execute a DVSL transformation with output to the response.
The context, velocity instance, and alike, used in the face 1, should be
reused in the face 3.
Can it be done freely?
different calls should reuse the instance so that a cached parsed tree is
used both at 1 and 3.
Thanks,
Dario.
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
Re: VM body node execution
Posted by Bill Burton <bi...@progress.com>.
Hello,
Dario Liberman wrote:
> Thank you for your prompt response Bill,
You're welcome.
> How exactly do I write this directive?
In Java. See the existing directives in the source.
> I dont undestand how it would be used...
> Is it instead of the #macro directive???
Yes. You would write an application specific directive to solve a
particular problem.
> Also the parsing of macros right now does not include the '#end' tag, it is
> like a simple line directive. Do you think it would be posible to
> diferentiate macros defined with a new directive (say #macrowb -a macro with
> body) that assigns to its macros the property of accepting child nodes
> during parse time?
If you want to enhance the existing macro functionality to support a
body, that would be a challenge. The existing #macro directive probably
could not be used unless there was a way to specify the begin, body and
end sections as in a JSP tag and then at runtime detect if the macro is
a block or inline version.
>
> Would you join me if I start developing this?
Don't have much time right now but I'll be happy to look at something
you write.
> Do you think it is possible?
Adding support for block macros would probably take a bit of effort and
require patching the existing code.
> Do you think it is simple to do?
Not really but I'm not an expert in the Velocity internals.
> best regards,
> Dario.
>
> PD: Perhaps a simpler startup is to have a simple directive that catches all
> its content and puts it in a string. Say:
>
> #text into $mystr until '---end---'
> here code or anything
> ---end---
>
> so with this simple directive I could perform a parse on runtime and even
> cache the internal representation in a hashTable according to filename and
> linenumber.
Yes, that would be lots simpler. It might look something like:
#setinline($mystr)
body
text, etc.
#end.
or maybe #collect would be a better name.
You can do something similar right now with a null parameter macro. But
since you probably want to allow redefining this macro, you have to
change one of the Velocity properties to allow it.
-Bill
> Then it is just a matter of naming things or alike so to have this variable
> accesible from within any VM.
>
> What do you think?
>
>
> ----- Original Message -----
> From: "Bill Burton" <bi...@progress.com>
> To: "Velocity Developers List" <ve...@jakarta.apache.org>
> Sent: Wednesday, February 26, 2003 14:45
> Subject: Re: VM body node execution
>
>
>
>>Hello,
>>
>>Dario Liberman wrote:
>>
>>>Hello,
>>>
>>>I am new to velocity, I am from the near webmacro world. :-)
>>>
>>>I was thinking about migrating to Velocity.
>>>And when I came to the VMs I found it interesting, but would like to
>>>increment its expressive power now that I got tempted to use it.
>>>The improvement goes subject to this question:
>>> I would like to have access inside a VM to the body node of the
>
> macro's
>
>>>callee, is it possible?
>>>
>>>
>>>I am new to this, and I would like to accomplish it if it is possible.
>
> Is
>
>>>there any guru that can help me out?
>>>
>>>Any comments are welcome.
>>>
>>>Here I describe what uses I would be giving to it, with examples, so
>
> that I
>
>>>can explain myself.
>>>
>>>for example:
>>>
>>>code->
>>>#macro( tostring $strvar )
>>> #set ( $strvar = "$macro_body") ## this would do a kind of
>>>Velocity.execute($current_context, new
>>>stringwriter(req.get("$strvar")),macro_body_node)
>>>#end
>>><-code
>>>
>>>so when I use it:
>>>code->
>>>#tostring ( $mystrvar)
>>> VM code here, with any statements, directives, etc.
>>>#end
>>
>>The problem is macros can't have a body when being executed :( . It
>>would be *really* nice if there were a way to do this. Right now, you
>>would have to write a directive in order access the body.
>>
>>-Bill
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
Re: VM body node execution
Posted by Dario Liberman <ve...@ineba.net>.
Thank you for your prompt response Bill,
How exactly do I write this directive?
I dont undestand how it would be used...
Is it instead of the #macro directive???
Also the parsing of macros right now does not include the '#end' tag, it is
like a simple line directive. Do you think it would be posible to
diferentiate macros defined with a new directive (say #macrowb -a macro with
body) that assigns to its macros the property of accepting child nodes
during parse time?
Would you join me if I start developing this?
Do you think it is possible?
Do you think it is simple to do?
best regards,
Dario.
PD: Perhaps a simpler startup is to have a simple directive that catches all
its content and puts it in a string. Say:
#text into $mystr until '---end---'
here code or anything
---end---
so with this simple directive I could perform a parse on runtime and even
cache the internal representation in a hashTable according to filename and
linenumber.
Then it is just a matter of naming things or alike so to have this variable
accesible from within any VM.
What do you think?
----- Original Message -----
From: "Bill Burton" <bi...@progress.com>
To: "Velocity Developers List" <ve...@jakarta.apache.org>
Sent: Wednesday, February 26, 2003 14:45
Subject: Re: VM body node execution
> Hello,
>
> Dario Liberman wrote:
> > Hello,
> >
> > I am new to velocity, I am from the near webmacro world. :-)
> >
> > I was thinking about migrating to Velocity.
> > And when I came to the VMs I found it interesting, but would like to
> > increment its expressive power now that I got tempted to use it.
> > The improvement goes subject to this question:
> > I would like to have access inside a VM to the body node of the
macro's
> > callee, is it possible?
> >
> >
> > I am new to this, and I would like to accomplish it if it is possible.
Is
> > there any guru that can help me out?
> >
> > Any comments are welcome.
> >
> > Here I describe what uses I would be giving to it, with examples, so
that I
> > can explain myself.
> >
> > for example:
> >
> > code->
> > #macro( tostring $strvar )
> > #set ( $strvar = "$macro_body") ## this would do a kind of
> > Velocity.execute($current_context, new
> > stringwriter(req.get("$strvar")),macro_body_node)
> > #end
> > <-code
> >
> > so when I use it:
> > code->
> > #tostring ( $mystrvar)
> > VM code here, with any statements, directives, etc.
> > #end
>
> The problem is macros can't have a body when being executed :( . It
> would be *really* nice if there were a way to do this. Right now, you
> would have to write a directive in order access the body.
>
> -Bill
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
Re: VM body node execution
Posted by Bill Burton <bi...@progress.com>.
Hello,
Dario Liberman wrote:
> Hello,
>
> I am new to velocity, I am from the near webmacro world. :-)
>
> I was thinking about migrating to Velocity.
> And when I came to the VMs I found it interesting, but would like to
> increment its expressive power now that I got tempted to use it.
> The improvement goes subject to this question:
> I would like to have access inside a VM to the body node of the macro's
> callee, is it possible?
>
>
> I am new to this, and I would like to accomplish it if it is possible. Is
> there any guru that can help me out?
>
> Any comments are welcome.
>
> Here I describe what uses I would be giving to it, with examples, so that I
> can explain myself.
>
> for example:
>
> code->
> #macro( tostring $strvar )
> #set ( $strvar = "$macro_body") ## this would do a kind of
> Velocity.execute($current_context, new
> stringwriter(req.get("$strvar")),macro_body_node)
> #end
> <-code
>
> so when I use it:
> code->
> #tostring ( $mystrvar)
> VM code here, with any statements, directives, etc.
> #end
The problem is macros can't have a body when being executed :( . It
would be *really* nice if there were a way to do this. Right now, you
would have to write a directive in order access the body.
-Bill
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org