You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Christoph Reck <Ch...@dlr.de> on 2001/01/12 15:28:42 UTC

[PATCH] convert WebMacro

I have successfully ported my Turbine application from WM to Velocity!

Below you will find the patches required for updating the conversion
code from WM to the current velocity features. Some pittfalls yet 
exist:
  * single line comments are not yet handled correctly (how
    can it with the simple regexp!), e.g.
      #if ( $foo ) #begin
        yes
      #end ## this comment will leave the following else dangling
      #else ## this comment will skip removing the following begin
      #begin
        no
      #end
    Anyhow who will do such a strange thing?
   * A #set $foo = "a text with ## characters" does not convert 
     properly.
   * Embedded line comments may cause some other strange effects.
       #set $foo = "anything" ## converts properly
     whereas:
       #set $bar = "woobie" ## (## did I really mean this)
     still is converted but the brackets are a bit mixed up...
  * A set with anything other than a comment fillowing on the same 
    line will be placed within its brackets.
  * The WM C-style form {} assumes that it is gluing on the left 
    line, otherwise the closing bracket is not converted to #end.
    This is because most times JavaScript is formatted properly
    and indented. If this is not the case for one user, he will 
    have to edit the corresponding convert.WebMacro pattern.
I used my application as a cross check. It has *many* strange things
and it does convert properly!

Velocity *is* quicker than VM. I cannot quantify it, but you feel it.
Besides that in my application a process call creating a file (on a 
remote machine) did not propagate the change via NFS fast enough to 
process the next $File.exists($name) check (within WM it never 
failed, was slow enough!).

I did need to patch Vel to cope with some WM standard features:
  * ASTReference.setValue enhanced to handle Maps.
  * ASTSetDirective.render (and init) enhanced to suppress error
    message when RHS is null and has a bang: set( $foo = $!bar )
These will be sent in separate mails.

[PATCHES follow]
---------------------------------------------------------------------
--- Apache-Velocity-20010109/src/java/org/apache/velocity/convert/WebMacro.java Wed Dec 20 08:11:49 2000
+++ Apache-Velocity/src/java/org/apache/velocity/convert/WebMacro.java  Fri Jan 12 14:27:29 2001
@@ -86,33 +86,52 @@
      */
     protected String[] res =
     {
-        // Remove all #begin statements.
-        "#begin\\n", 
-        "",
-            
-        // Remove the "{" for the start of block directives,
-        // Velocity doesn't use them.
-        "(#\\w+\\s*.*)\\n?\\s*\\{",
-        "$1",
+        // Make #if directive match the Velocity directive style.
+        "#if\\s*[(]\\s*(.*\\S)\\s*[)]\\s*(#begin|{)[ \\t]?",
+        "#if( $1 )",
+
+        // Remove the WM #end #else #begin usage.
+        "[ \\t]?(#end|})\\s*#else\\s*(#begin|{)[ \\t]?(\\w)",
+        "#else#**#$3", // avoid touching a followup word with embedded comment
+        "[ \\t]?(#end|})\\s*#else\\s*(#begin|{)[ \\t]?",
+        "#else",
+
+        // Convert WM style #foreach to Velocity directive style.
+        "#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$[^\\s#]+)\\s*(#begin|{)[ \\t]?",
+        "#foreach( $1 in $2 )",
 
         // Change the "}" to #end. Have to get more
         // sophisticated here. Will assume either {}
         // and no javascript, or #begin/#end with the
         // possibility of javascript.
-        "}",
-        "#end",
-            
-        // Convert WM style if/else to Velocity style.
-        "#end.*\\n?\\s*(#else)",
-        "$1",
-            
-        // Convert WM style #foreach to Velocity #foreach.
-        "#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$\\w+)",
-        "#foreach ($1 in $2)",
+        "\n}", // assumes that javascript is indented, WMs not!!!
+        "\n#end",
         
-        // Change parse to include.
-        "#parse",
-        "#include",
+        // Convert WM style #set to Velocity directive style.
+        "#set\\s+(\\$[^\\s=]+)\\s*=\\s*(.*\\S)[ \\t]*",
+        "#set( $1 = $2 )",
+        "(##[# \\t\\w]*)\\)", // fix comments included at end of line
+        ")$1",
+
+        // Convert WM style #parse to Velocity directive style.
+        "#parse\\s+([^\\s#]+)[ \\t]?",
+        "#parse( $1 )",
+
+        // Convert WM style #include to Velocity directive style.
+        "#include\\s+([^\\s#]+)[ \\t]?",
+        "#include( $1 )",
+
+        // Convert WM formal reference to VL syntax.
+        "\\$\\(([^\\)]+)\\)",
+        "${$1}",
+        "\\${([^}\\(]+)\\(([^}]+)}\\)", // fix encapsulated brakets: {(})
+        "${$1($2)}",
+
+        // Velocity currently does not permit leading underscore.
+        "\\$_",
+        "$l_",
+        "\\${(_[^}]+)}", // within a formal reference
+        "${l$1}",
             
         // Change extensions when seen.
         "\\.wm",
@@ -233,8 +252,12 @@
     public String convertTemplate(String template)
     {
         orignalTemplate = StringUtils.fileContentsToString(template);
-        perl = new Perl5Util();
 
+        // overcome current velocity 0.71 limitation
+        if ( !orignalTemplate.endsWith("\n") )
+          orignalTemplate += "\n";
+
+        perl = new Perl5Util();
         for (int i = 0; i < res.length; i += 2)
             while (perl.match("/" + res[i] + "/", orignalTemplate))
                 orignalTemplate = perl.substitute(
---------------------------------------------------------------------
diff -u -r Apache-Velocity-20010109/convert/convert-wm.sh Apache-Velocity/convert/convert-wm.sh
--- Apache-Velocity-20010109/convert/convert-wm.sh      Sun Oct 22 03:52:10 2000
+++ Apache-Velocity/convert/convert-wm.sh       Tue Jan  9 12:37:59 2001
@@ -5,6 +5,6 @@
           && echo \
           && exit
 
-CLASSPATH=../bin/velocity-0.4.jar
+CLASSPATH=`ls -1 ../bin/*.jar |tr '\n' ':'`.
 
 java -cp ${CLASSPATH} org.apache.velocity.convert.WebMacro $1
---------------------------------------------------------------------

:) Christoph

Re: [PATCH] convert WebMacro

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Christoph Reck wrote:
> 
> I have successfully ported my Turbine application from WM to Velocity!

You are a god!

> 
> Below you will find the patches required for updating the conversion
> code from WM to the current velocity features. Some pittfalls yet
> exist:
>   * single line comments are not yet handled correctly (how
>     can it with the simple regexp!), e.g.
>       #if ( $foo ) #begin
>         yes
>       #end ## this comment will leave the following else dangling
>       #else ## this comment will skip removing the following begin
>       #begin
>         no
>       #end
>     Anyhow who will do such a strange thing?

You would.  And since my wife is away this weekend, I can fix these once
I get my VM fixes checked in that not one person cares about :)

>    * A #set $foo = "a text with ## characters" does not convert
>      properly.

Ok.  But please, stop using the old #set. It's on now just so we
wouldn't shock people, but I am going to simply turn it off :)

Use #set($foo = " blarg")

>    * Embedded line comments may cause some other strange effects.
>        #set $foo = "anything" ## converts properly
>      whereas:
>        #set $bar = "woobie" ## (## did I really mean this)
>      still is converted but the brackets are a bit mixed up...

I will look at it, but only for the new #set() 

>   * A set with anything other than a comment fillowing on the same
>     line will be placed within its brackets.

If you use the old DEPRECATED set.

>   * The WM C-style form {} assumes that it is gluing on the left
>     line, otherwise the closing bracket is not converted to #end.
>     This is because most times JavaScript is formatted properly
>     and indented. If this is not the case for one user, he will
>     have to edit the corresponding convert.WebMacro pattern.
> I used my application as a cross check. It has *many* strange things
> and it does convert properly!
> 
> Velocity *is* quicker than VM. I cannot quantify it, but you feel it.
> Besides that in my application a process call creating a file (on a
> remote machine) did not propagate the change via NFS fast enough to
> process the next $File.exists($name) check (within WM it never
> failed, was slow enough!).
> 
> I did need to patch Vel to cope with some WM standard features:
>   * ASTReference.setValue enhanced to handle Maps.

hm.  I am rapidly moving towards yes on this ...

>   * ASTSetDirective.render (and init) enhanced to suppress error
>     message when RHS is null and has a bang: set( $foo = $!bar )

The new VM fixes coincidentally have that working again.  No, the bang
has nothing to do with it.  It's a general switch, to shut of alerts
when the RHS is null.  It was in there a while back, in my code.  I
guessed I screwed up when committing......  there is a property for it
:)

> These will be sent in separate mails.
> 
> [PATCHES follow]
> ---------------------------------------------------------------------
> --- Apache-Velocity-20010109/src/java/org/apache/velocity/convert/WebMacro.java Wed Dec 20 08:11:49 2000
> +++ Apache-Velocity/src/java/org/apache/velocity/convert/WebMacro.java  Fri Jan 12 14:27:29 2001
> @@ -86,33 +86,52 @@
>       */
>      protected String[] res =
>      {
> -        // Remove all #begin statements.
> -        "#begin\\n",
> -        "",
> -
> -        // Remove the "{" for the start of block directives,
> -        // Velocity doesn't use them.
> -        "(#\\w+\\s*.*)\\n?\\s*\\{",
> -        "$1",
> +        // Make #if directive match the Velocity directive style.
> +        "#if\\s*[(]\\s*(.*\\S)\\s*[)]\\s*(#begin|{)[ \\t]?",
> +        "#if( $1 )",
> +
> +        // Remove the WM #end #else #begin usage.
> +        "[ \\t]?(#end|})\\s*#else\\s*(#begin|{)[ \\t]?(\\w)",
> +        "#else#**#$3", // avoid touching a followup word with embedded comment
> +        "[ \\t]?(#end|})\\s*#else\\s*(#begin|{)[ \\t]?",
> +        "#else",
> +
> +        // Convert WM style #foreach to Velocity directive style.
> +        "#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$[^\\s#]+)\\s*(#begin|{)[ \\t]?",
> +        "#foreach( $1 in $2 )",
> 
>          // Change the "}" to #end. Have to get more
>          // sophisticated here. Will assume either {}
>          // and no javascript, or #begin/#end with the
>          // possibility of javascript.
> -        "}",
> -        "#end",
> -
> -        // Convert WM style if/else to Velocity style.
> -        "#end.*\\n?\\s*(#else)",
> -        "$1",
> -
> -        // Convert WM style #foreach to Velocity #foreach.
> -        "#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$\\w+)",
> -        "#foreach ($1 in $2)",
> +        "\n}", // assumes that javascript is indented, WMs not!!!
> +        "\n#end",
> 
> -        // Change parse to include.
> -        "#parse",
> -        "#include",
> +        // Convert WM style #set to Velocity directive style.
> +        "#set\\s+(\\$[^\\s=]+)\\s*=\\s*(.*\\S)[ \\t]*",
> +        "#set( $1 = $2 )",
> +        "(##[# \\t\\w]*)\\)", // fix comments included at end of line
> +        ")$1",
> +
> +        // Convert WM style #parse to Velocity directive style.
> +        "#parse\\s+([^\\s#]+)[ \\t]?",
> +        "#parse( $1 )",
> +
> +        // Convert WM style #include to Velocity directive style.
> +        "#include\\s+([^\\s#]+)[ \\t]?",
> +        "#include( $1 )",
> +
> +        // Convert WM formal reference to VL syntax.
> +        "\\$\\(([^\\)]+)\\)",
> +        "${$1}",
> +        "\\${([^}\\(]+)\\(([^}]+)}\\)", // fix encapsulated brakets: {(})
> +        "${$1($2)}",
> +
> +        // Velocity currently does not permit leading underscore.
> +        "\\$_",
> +        "$l_",
> +        "\\${(_[^}]+)}", // within a formal reference
> +        "${l$1}",
> 
>          // Change extensions when seen.
>          "\\.wm",
> @@ -233,8 +252,12 @@
>      public String convertTemplate(String template)
>      {
>          orignalTemplate = StringUtils.fileContentsToString(template);
> -        perl = new Perl5Util();
> 
> +        // overcome current velocity 0.71 limitation
> +        if ( !orignalTemplate.endsWith("\n") )
> +          orignalTemplate += "\n";
> +
> +        perl = new Perl5Util();
>          for (int i = 0; i < res.length; i += 2)
>              while (perl.match("/" + res[i] + "/", orignalTemplate))
>                  orignalTemplate = perl.substitute(
> ---------------------------------------------------------------------
> diff -u -r Apache-Velocity-20010109/convert/convert-wm.sh Apache-Velocity/convert/convert-wm.sh
> --- Apache-Velocity-20010109/convert/convert-wm.sh      Sun Oct 22 03:52:10 2000
> +++ Apache-Velocity/convert/convert-wm.sh       Tue Jan  9 12:37:59 2001
> @@ -5,6 +5,6 @@
>            && echo \
>            && exit
> 
> -CLASSPATH=../bin/velocity-0.4.jar
> +CLASSPATH=`ls -1 ../bin/*.jar |tr '\n' ':'`.
> 
>  java -cp ${CLASSPATH} org.apache.velocity.convert.WebMacro $1
> ---------------------------------------------------------------------
> 
> :) Christoph

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity

Re: [PATCH] convert WebMacro

Posted by Christoph Reck <Ch...@dlr.de>.
"Geir Magnusson Jr." wrote:
> 
> it's checked in.   When you have a moment, could you give it a try to
> make sure all is well?
> 
> geir

yes the checked in convert.WebMacro now matches my version.

If the ASTReference.setValue patch is ingested, it should 
now be possible to seamlesly migrate from WM to Vel!

:) Christoph

Re: [PATCH] convert WebMacro

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Christoph Reck wrote:
> 
> Geir you seemed to have read this mail wrongly. This is a patch
> that makes the tool usable. If the set of 4 patches I submitted on
> friday are ingested, it should then be possible
> 

Christoph : I wasn't at all remarking on your fixes to the WM tool!  I
think they are great!  I will put them in today!

> The problems that are known are documented and are of minimal
> impact. The regexp approach does not allow a better automatic
> conversion (I do consider myself a script/regexp expert). Some
> extended perl-regexp features not available in the java impl
> would have made a more perfect conversion possible but its not
> possbile at the moment. So I documented the pittfalls.

Please, you misinterpreted me!  I think it's great.

I started to comment below, but stopped. This is all my fault.  I
misread what you were saying.  Once I get a few personal things done, I
will get right to this.

Sorry about that.

:)

geir

> 
> "Geir Magnusson Jr." wrote:
> >
> > Christoph Reck wrote:
> > >
> > > I have successfully ported my Turbine application from WM to Velocity!
> >
> > You are a god!
> 
> I hope the patches I submitted brings me(/us) some followers then!


> 
> >
> > >
> > > Below you will find the patches required for updating the conversion
> > > code from WM to the current velocity features. Some pittfalls yet
> > > exist:
> > >   * single line comments are not yet handled correctly (how
> > >     can it with the simple regexp!), e.g.
> > >       #if ( $foo ) #begin
> > >         yes
> > >       #end ## this comment will leave the following else dangling
> > >       #else ## this comment will skip removing the following begin
> > >       #begin
> > >         no
> > >       #end
> > >     Anyhow who will do such a strange thing?
> >
> > You would.  And since my wife is away this weekend, I can fix these once
> > I get my VM fixes checked in that not one person cares about :)
> 
> Nothing more fixable with the current regexp supported features.

Yep (I thought you were pointing out more parser bugs...)
 
> >
> > >    * A #set $foo = "a text with ## characters" does not convert
> > >      properly.
> >
> > Ok.  But please, stop using the old #set. It's on now just so we
> > wouldn't shock people, but I am going to simply turn it off :)
> 
> I'm stating the WM source. It gets converted to:
>   #set( $foo = "a text with )## characters"
> Which is not nice. If the regexp would allow back-refenreces you
> could convert this properly. Maybe adding another set of
> pattern/substitute could also fix this.

Again, my mistake. I thought again there was a parser issue.

> 
> >
> > Use #set($foo = " blarg")
> 
> Yes thats what convet.WebMacro generates now!

Excellent!  Again, sorry.  My misread.
> 
> >
> > >    * Embedded line comments may cause some other strange effects.
> > >        #set $foo = "anything" ## converts properly
> > >      whereas:
> > >        #set $bar = "woobie" ## (## did I really mean this)
> > >      still is converted but the brackets are a bit mixed up...
> >
> > I will look at it, but only for the new #set()
> 
> Just documented my hack that this gets conveted to:
>   #set( $bar = "woobie" )## ()## did I really mean this
> which is not quite right, but usable!

Again, I thought it was a parser bug.

> >
> > >   * A set with anything other than a comment fillowing on the same
> > >     line will be placed within its brackets.
> >
> > If you use the old DEPRECATED set.
> 
> I meant in the automatic conversion. The code generated will lead to
> parser errors.
> In WM a
>   $set $foo = $bar;
> emitted the semicolon during rendering.
> My patched conversion makes a
>   $set( $foo = $bar; )
> out of it, which will make the parser bark!
> 
> >
> > >   * The WM C-style form {} assumes that it is gluing on the left
> > >     line, otherwise the closing bracket is not converted to #end.
> > >     This is because most times JavaScript is formatted properly
> > >     and indented. If this is not the case for one user, he will
> > >     have to edit the corresponding convert.WebMacro pattern.
> > > I used my application as a cross check. It has *many* strange things
> > > and it does convert properly!
> > >
> > > Velocity *is* quicker than VM. I cannot quantify it, but you feel it.
> > > Besides that in my application a process call creating a file (on a
> > > remote machine) did not propagate the change via NFS fast enough to
> > > process the next $File.exists($name) check (within WM it never
> > > failed, was slow enough!).
> > >
> > > I did need to patch Vel to cope with some WM standard features:
> > >   * ASTReference.setValue enhanced to handle Maps.
> >
> > hm.  I am rapidly moving towards yes on this ...
> 
> Either use my patch I submitted for this or do it in any of your
> ways...
> 
> >
> > >   * ASTSetDirective.render (and init) enhanced to suppress error
> > >     message when RHS is null and has a bang: set( $foo = $!bar )
> >
> > The new VM fixes coincidentally have that working again.  No, the bang
> > has nothing to do with it.  It's a general switch, to shut of alerts
> > when the RHS is null.  It was in there a while back, in my code.  I
> > guessed I screwed up when committing......  there is a property for it
> > :)
> 
> Not sufficient. See my patch mail on this subject.
> 
> >
> > > These will be sent in separate mails.
> > >
> > > [PATCHES follow]
> > > ---------------------------------------------------------------------
> > > --- Apache-Velocity-20010109/src/java/org/apache/velocity/convert/WebMacro.java Wed Dec 20 08:11:49 2000
> > > +++ Apache-Velocity/src/java/org/apache/velocity/convert/WebMacro.java  Fri Jan 12 14:27:29 2001
> > > @@ -86,33 +86,52 @@
> > >       */
> > >      protected String[] res =
> > >      {
> > > -        // Remove all #begin statements.
> > > -        "#begin\\n",
> > > -        "",
> > > -
> > > -        // Remove the "{" for the start of block directives,
> > > -        // Velocity doesn't use them.
> > > -        "(#\\w+\\s*.*)\\n?\\s*\\{",
> > > -        "$1",
> > > +        // Make #if directive match the Velocity directive style.
> > > +        "#if\\s*[(]\\s*(.*\\S)\\s*[)]\\s*(#begin|{)[ \\t]?",
> > > +        "#if( $1 )",
> > > +
> > > +        // Remove the WM #end #else #begin usage.
> > > +        "[ \\t]?(#end|})\\s*#else\\s*(#begin|{)[ \\t]?(\\w)",
> > > +        "#else#**#$3", // avoid touching a followup word with embedded comment
> > > +        "[ \\t]?(#end|})\\s*#else\\s*(#begin|{)[ \\t]?",
> > > +        "#else",
> > > +
> > > +        // Convert WM style #foreach to Velocity directive style.
> > > +        "#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$[^\\s#]+)\\s*(#begin|{)[ \\t]?",
> > > +        "#foreach( $1 in $2 )",
> > >
> > >          // Change the "}" to #end. Have to get more
> > >          // sophisticated here. Will assume either {}
> > >          // and no javascript, or #begin/#end with the
> > >          // possibility of javascript.
> > > -        "}",
> > > -        "#end",
> > > -
> > > -        // Convert WM style if/else to Velocity style.
> > > -        "#end.*\\n?\\s*(#else)",
> > > -        "$1",
> > > -
> > > -        // Convert WM style #foreach to Velocity #foreach.
> > > -        "#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$\\w+)",
> > > -        "#foreach ($1 in $2)",
> > > +        "\n}", // assumes that javascript is indented, WMs not!!!
> > > +        "\n#end",
> > >
> > > -        // Change parse to include.
> > > -        "#parse",
> > > -        "#include",
> > > +        // Convert WM style #set to Velocity directive style.
> > > +        "#set\\s+(\\$[^\\s=]+)\\s*=\\s*(.*\\S)[ \\t]*",
> > > +        "#set( $1 = $2 )",
> > > +        "(##[# \\t\\w]*)\\)", // fix comments included at end of line
> > > +        ")$1",
> > > +
> > > +        // Convert WM style #parse to Velocity directive style.
> > > +        "#parse\\s+([^\\s#]+)[ \\t]?",
> > > +        "#parse( $1 )",
> > > +
> > > +        // Convert WM style #include to Velocity directive style.
> > > +        "#include\\s+([^\\s#]+)[ \\t]?",
> > > +        "#include( $1 )",
> > > +
> > > +        // Convert WM formal reference to VL syntax.
> > > +        "\\$\\(([^\\)]+)\\)",
> > > +        "${$1}",
> > > +        "\\${([^}\\(]+)\\(([^}]+)}\\)", // fix encapsulated brakets: {(})
> > > +        "${$1($2)}",
> > > +
> > > +        // Velocity currently does not permit leading underscore.
> > > +        "\\$_",
> > > +        "$l_",
> > > +        "\\${(_[^}]+)}", // within a formal reference
> > > +        "${l$1}",
> > >
> > >          // Change extensions when seen.
> > >          "\\.wm",
> > > @@ -233,8 +252,12 @@
> > >      public String convertTemplate(String template)
> > >      {
> > >          orignalTemplate = StringUtils.fileContentsToString(template);
> > > -        perl = new Perl5Util();
> > >
> > > +        // overcome current velocity 0.71 limitation
> > > +        if ( !orignalTemplate.endsWith("\n") )
> > > +          orignalTemplate += "\n";
> > > +
> > > +        perl = new Perl5Util();
> > >          for (int i = 0; i < res.length; i += 2)
> > >              while (perl.match("/" + res[i] + "/", orignalTemplate))
> > >                  orignalTemplate = perl.substitute(
> > > ---------------------------------------------------------------------
> > > diff -u -r Apache-Velocity-20010109/convert/convert-wm.sh Apache-Velocity/convert/convert-wm.sh
> > > --- Apache-Velocity-20010109/convert/convert-wm.sh      Sun Oct 22 03:52:10 2000
> > > +++ Apache-Velocity/convert/convert-wm.sh       Tue Jan  9 12:37:59 2001
> > > @@ -5,6 +5,6 @@
> > >            && echo \
> > >            && exit
> > >
> > > -CLASSPATH=../bin/velocity-0.4.jar
> > > +CLASSPATH=`ls -1 ../bin/*.jar |tr '\n' ':'`.
> > >
> > >  java -cp ${CLASSPATH} org.apache.velocity.convert.WebMacro $1
> > > ---------------------------------------------------------------------
> > >
> > > :) Christoph
> >
> > --
> > Geir Magnusson Jr.                               geirm@optonline.com
> > Velocity : it's not just a good idea. It should be the law.
> > http://jakarta.apache.org/velocity

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity

Re: [PATCH] convert WebMacro

Posted by Christoph Reck <Ch...@dlr.de>.
Geir you seemed to have read this mail wrongly. This is a patch
that makes the tool usable. If the set of 4 patches I submitted on 
friday are ingested, it should then be possible 

The problems that are known are documented and are of minimal
impact. The regexp approach does not allow a better automatic
conversion (I do consider myself a script/regexp expert). Some
extended perl-regexp features not available in the java impl
would have made a more perfect conversion possible but its not
possbile at the moment. So I documented the pittfalls.

"Geir Magnusson Jr." wrote:
> 
> Christoph Reck wrote:
> >
> > I have successfully ported my Turbine application from WM to Velocity!
> 
> You are a god!

I hope the patches I submitted brings me(/us) some followers then!

> 
> >
> > Below you will find the patches required for updating the conversion
> > code from WM to the current velocity features. Some pittfalls yet
> > exist:
> >   * single line comments are not yet handled correctly (how
> >     can it with the simple regexp!), e.g.
> >       #if ( $foo ) #begin
> >         yes
> >       #end ## this comment will leave the following else dangling
> >       #else ## this comment will skip removing the following begin
> >       #begin
> >         no
> >       #end
> >     Anyhow who will do such a strange thing?
> 
> You would.  And since my wife is away this weekend, I can fix these once
> I get my VM fixes checked in that not one person cares about :)

Nothing more fixable with the current regexp supported features.

> 
> >    * A #set $foo = "a text with ## characters" does not convert
> >      properly.
> 
> Ok.  But please, stop using the old #set. It's on now just so we
> wouldn't shock people, but I am going to simply turn it off :)

I'm stating the WM source. It gets converted to:
  #set( $foo = "a text with )## characters"
Which is not nice. If the regexp would allow back-refenreces you 
could convert this properly. Maybe adding another set of 
pattern/substitute could also fix this.

> 
> Use #set($foo = " blarg")

Yes thats what convet.WebMacro generates now!

> 
> >    * Embedded line comments may cause some other strange effects.
> >        #set $foo = "anything" ## converts properly
> >      whereas:
> >        #set $bar = "woobie" ## (## did I really mean this)
> >      still is converted but the brackets are a bit mixed up...
> 
> I will look at it, but only for the new #set()

Just documented my hack that this gets conveted to:
  #set( $bar = "woobie" )## ()## did I really mean this
which is not quite right, but usable!

> 
> >   * A set with anything other than a comment fillowing on the same
> >     line will be placed within its brackets.
> 
> If you use the old DEPRECATED set.

I meant in the automatic conversion. The code generated will lead to
parser errors.
In WM a
  $set $foo = $bar;
emitted the semicolon during rendering.
My patched conversion makes a
  $set( $foo = $bar; )
out of it, which will make the parser bark!

> 
> >   * The WM C-style form {} assumes that it is gluing on the left
> >     line, otherwise the closing bracket is not converted to #end.
> >     This is because most times JavaScript is formatted properly
> >     and indented. If this is not the case for one user, he will
> >     have to edit the corresponding convert.WebMacro pattern.
> > I used my application as a cross check. It has *many* strange things
> > and it does convert properly!
> >
> > Velocity *is* quicker than VM. I cannot quantify it, but you feel it.
> > Besides that in my application a process call creating a file (on a
> > remote machine) did not propagate the change via NFS fast enough to
> > process the next $File.exists($name) check (within WM it never
> > failed, was slow enough!).
> >
> > I did need to patch Vel to cope with some WM standard features:
> >   * ASTReference.setValue enhanced to handle Maps.
> 
> hm.  I am rapidly moving towards yes on this ...

Either use my patch I submitted for this or do it in any of your 
ways...

> 
> >   * ASTSetDirective.render (and init) enhanced to suppress error
> >     message when RHS is null and has a bang: set( $foo = $!bar )
> 
> The new VM fixes coincidentally have that working again.  No, the bang
> has nothing to do with it.  It's a general switch, to shut of alerts
> when the RHS is null.  It was in there a while back, in my code.  I
> guessed I screwed up when committing......  there is a property for it
> :)

Not sufficient. See my patch mail on this subject.

> 
> > These will be sent in separate mails.
> >
> > [PATCHES follow]
> > ---------------------------------------------------------------------
> > --- Apache-Velocity-20010109/src/java/org/apache/velocity/convert/WebMacro.java Wed Dec 20 08:11:49 2000
> > +++ Apache-Velocity/src/java/org/apache/velocity/convert/WebMacro.java  Fri Jan 12 14:27:29 2001
> > @@ -86,33 +86,52 @@
> >       */
> >      protected String[] res =
> >      {
> > -        // Remove all #begin statements.
> > -        "#begin\\n",
> > -        "",
> > -
> > -        // Remove the "{" for the start of block directives,
> > -        // Velocity doesn't use them.
> > -        "(#\\w+\\s*.*)\\n?\\s*\\{",
> > -        "$1",
> > +        // Make #if directive match the Velocity directive style.
> > +        "#if\\s*[(]\\s*(.*\\S)\\s*[)]\\s*(#begin|{)[ \\t]?",
> > +        "#if( $1 )",
> > +
> > +        // Remove the WM #end #else #begin usage.
> > +        "[ \\t]?(#end|})\\s*#else\\s*(#begin|{)[ \\t]?(\\w)",
> > +        "#else#**#$3", // avoid touching a followup word with embedded comment
> > +        "[ \\t]?(#end|})\\s*#else\\s*(#begin|{)[ \\t]?",
> > +        "#else",
> > +
> > +        // Convert WM style #foreach to Velocity directive style.
> > +        "#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$[^\\s#]+)\\s*(#begin|{)[ \\t]?",
> > +        "#foreach( $1 in $2 )",
> >
> >          // Change the "}" to #end. Have to get more
> >          // sophisticated here. Will assume either {}
> >          // and no javascript, or #begin/#end with the
> >          // possibility of javascript.
> > -        "}",
> > -        "#end",
> > -
> > -        // Convert WM style if/else to Velocity style.
> > -        "#end.*\\n?\\s*(#else)",
> > -        "$1",
> > -
> > -        // Convert WM style #foreach to Velocity #foreach.
> > -        "#foreach\\s+(\\$\\w+)\\s+in\\s+(\\$\\w+)",
> > -        "#foreach ($1 in $2)",
> > +        "\n}", // assumes that javascript is indented, WMs not!!!
> > +        "\n#end",
> >
> > -        // Change parse to include.
> > -        "#parse",
> > -        "#include",
> > +        // Convert WM style #set to Velocity directive style.
> > +        "#set\\s+(\\$[^\\s=]+)\\s*=\\s*(.*\\S)[ \\t]*",
> > +        "#set( $1 = $2 )",
> > +        "(##[# \\t\\w]*)\\)", // fix comments included at end of line
> > +        ")$1",
> > +
> > +        // Convert WM style #parse to Velocity directive style.
> > +        "#parse\\s+([^\\s#]+)[ \\t]?",
> > +        "#parse( $1 )",
> > +
> > +        // Convert WM style #include to Velocity directive style.
> > +        "#include\\s+([^\\s#]+)[ \\t]?",
> > +        "#include( $1 )",
> > +
> > +        // Convert WM formal reference to VL syntax.
> > +        "\\$\\(([^\\)]+)\\)",
> > +        "${$1}",
> > +        "\\${([^}\\(]+)\\(([^}]+)}\\)", // fix encapsulated brakets: {(})
> > +        "${$1($2)}",
> > +
> > +        // Velocity currently does not permit leading underscore.
> > +        "\\$_",
> > +        "$l_",
> > +        "\\${(_[^}]+)}", // within a formal reference
> > +        "${l$1}",
> >
> >          // Change extensions when seen.
> >          "\\.wm",
> > @@ -233,8 +252,12 @@
> >      public String convertTemplate(String template)
> >      {
> >          orignalTemplate = StringUtils.fileContentsToString(template);
> > -        perl = new Perl5Util();
> >
> > +        // overcome current velocity 0.71 limitation
> > +        if ( !orignalTemplate.endsWith("\n") )
> > +          orignalTemplate += "\n";
> > +
> > +        perl = new Perl5Util();
> >          for (int i = 0; i < res.length; i += 2)
> >              while (perl.match("/" + res[i] + "/", orignalTemplate))
> >                  orignalTemplate = perl.substitute(
> > ---------------------------------------------------------------------
> > diff -u -r Apache-Velocity-20010109/convert/convert-wm.sh Apache-Velocity/convert/convert-wm.sh
> > --- Apache-Velocity-20010109/convert/convert-wm.sh      Sun Oct 22 03:52:10 2000
> > +++ Apache-Velocity/convert/convert-wm.sh       Tue Jan  9 12:37:59 2001
> > @@ -5,6 +5,6 @@
> >            && echo \
> >            && exit
> >
> > -CLASSPATH=../bin/velocity-0.4.jar
> > +CLASSPATH=`ls -1 ../bin/*.jar |tr '\n' ':'`.
> >
> >  java -cp ${CLASSPATH} org.apache.velocity.convert.WebMacro $1
> > ---------------------------------------------------------------------
> >
> > :) Christoph
> 
> --
> Geir Magnusson Jr.                               geirm@optonline.com
> Velocity : it's not just a good idea. It should be the law.
> http://jakarta.apache.org/velocity