Velocity giving parsing exception for hyphen present in normal string

35 views Asked by At

For the string #TestString (test-string this is random string), the velocity engine while evaluating throws following parsing exception.

org.apache.velocity.exception.ParseErrorException: Encountered "-" at PERSONALISATION[line 1, column 18]
Was expecting one of:
    <RPAREN> ...
    <WHITESPACE> ...
    <WHITESPACE> ...

Velocity 1.7 is being used, and the context passed while evaluating is empty except the Escape Tool.

VelocityContext velocityContext = new VelocityContext();
velocityContext.put("esc", new EscapeTool());
StringWriter writer = new StringWriter();
getVelocityEngine().evaluate(velocityContext, writer, "TEST-TEMPLATE", text); 
1

There are 1 answers

0
devatherock On

In Velocity, the directives begin with # like #if, #foreach, etc. The # at the beginning of the line makes Velocity think that #TestString is a directive and so after it encounters the open parenthesis, it is looking for a closing parenthesis and encounters the - instead. Hence the parsing error. Something like #TestString(test), #TestString('test-string this is random string') or TestString# wouldn't cause any parsing errors. If your rendered string needs to look exactly like #TestString (test-string this is random string), you can use something like the below:

#set($hash = '#')
${hash}TestString (test-string this is random string)