2

When trying to use a java source code as template for Velocity, it crashes at this line of the template:

/*  @see panama.form.Validator#validate(java.lang.Object) */

with this Exception:

Exception in thread "main" org.apache.velocity.exception.ParseErrorException: Lexical error,   Encountered: "l" (108), after : "." at *unset*[line 23, column 53]
at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1301)
at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1265)
at org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:199)

Apparently it takes the #validate for a macro and crashes when it tries to parse the arguments for the macro. Is there anything one could do about this?

I'm using Velocity 1.7.

Edit

I know I could escape the # characters in the template files, but there are quite a number of them which also might change now and then, so I would prefer a way that would not require manual changes on the files.

Ridcully
  • 23,362
  • 7
  • 71
  • 86

1 Answers1

2

First option Try this solution from here: Escaping VTL Directives

VTL directives can be escaped with the backslash character ("\") in a manner similar to valid VTL references.

## #include( "a.txt" ) renders as <contents of a.txt>
#include( "a.txt" )

## \#include( "a.txt" ) renders as #include( "a.txt" )
\#include( "a.txt" )

## \\#include ( "a.txt" ) renders as \<contents of a.txt>
\\#include ( "a.txt" )

Second option You have this tool [EscapeTool][2]. Tool for working with escaping in Velocity templates.

It provides methods to escape outputs for Java, JavaScript, HTML, XML and SQL. Also provides methods to render VTL characters that otherwise needs escaping.

Third option: You may also try this workaround, I didn't use it but it should work:

You can at the beginning read your template as a String and then pre-parse it. For example replace all # with \#, or add to the beginning of file

  #set( $H = '#' )
  $H$H

see this answer: How to escape a # in velocity And then from that pre-parsed String create Template by using this answer: How to use String as Velocity Template?

Community
  • 1
  • 1
Anatoly
  • 5,056
  • 9
  • 62
  • 136
  • Thanks, but please see my edit to the question -- I'd rather a solution that does not require manual changes to the files. – Ridcully Apr 21 '15 at 13:48
  • I know that tool but that's not what I need, because Velocity crashes already when reading the template. – Ridcully Apr 21 '15 at 14:01
  • Updated my answer again :) – Anatoly Apr 21 '15 at 14:22
  • I ended up with the pre-parsing idea myself already but thanks for the hint on escaping the # using backslash - I hadn't thought about that and used {HASH} :-) -- as there are no other answers I guess there is not better way than that and accept your answer. Btw. one has to post-parse the merged template again and remove the escape again. – Ridcully Apr 21 '15 at 18:35
  • glad that it was helpful :) – Anatoly Apr 21 '15 at 18:55