Reputation: 9191
I have raised this issue in the Apache Velocity Mailing List but I have not received a response so I am reposting it here...
I am using the newly released Apache Velocity 2.1 and I am testing the hyphen as identifier name but I am hitting a bug or something? I have set the property Velocity.PARSER_HYPHEN_ALLOWED but it is only allowed on single data not on map or collection data.
I have this template:
hello-world.properties.vm
----------------------------------------------------------
Slash: ${sample-slash}
Slash in a Map: ${map.sample-slash}
And I have this sample test case:
public class ApacheVelocityTest {
private final String RESOURCES_DIR = "src/test/resources";
@Test
public void testVelocity() {
Path templatePath = Paths.get(RESOURCES_DIR, "templates", "hello-world.properties.vm");
VelocityEngine ve = new VelocityEngine();
ve.setProperty(Velocity.PARSER_HYPHEN_ALLOWED, true);
ve.init();
Template t = ve.getTemplate(templatePath.toString());
VelocityContext context = new VelocityContext();
context.put("sample-slash", "SLASH");
Map<String, String> sampleData = createData();
context.put("map", sampleData);
StringWriter writer = new StringWriter();
t.merge(context, writer);
System.out.println(writer.toString());
}
public Map<String, String> createData() {
Map<String, String> mapData = new HashMap<String, String>();
mapData.put("sample-slash", "USER1");
return mapData;
}
}
Now, the first "sample-slash" is rendered correctly but the one that is in the java map is not..it is throwing out an error like this:
org.apache.velocity.exception.ParseErrorException: Encountered "-slash}" at src\test\resources\templates\hello-world.properties.vm[line 5, column 22]
Was expecting one of:
"[" ...
"|" ...
"}" ...
"}" ...
at org.apache.velocity.Template.process(Template.java:154)
The parser exception is being thrown by the object that is embedded into the java map.
Do I have any workaround for this developers? Any pointers are greatly appreciated.
Upvotes: 1
Views: 1235
Reputation: 4130
It's a bug with the parser.allow_hyphen_in_identifiers
backward compatibility mode in version 2.1.
You can workaround it with:
$map.get("sample-slash")
or
$map["sample-slash"]
The bug is fixed in the main development branch.
Upvotes: 1