I'm using JEXL to evalute a String as below:
'GroupName'.'ProductName'.'item'.'fields'.'duration'
where GroupName and ProductName are String variables while the rest are fixed strings.
I built a context as: Map<String, Map<String, CustomClass>>
which ends up like below:
GroupName >> ProductName >> CustomClass
Before I evaluate the expression I do replace all single quotes by empty character.
Issue: The evaluation is not working when ProductName contains a dot itself.
Question: Is there any way I can tell JEXL engine to use a custom character rather than the dot as separator to evaluate the expression?
Update: in the oracle documentation about velocity it states: Do not use the '.' character in property names. If I understood correct JEXL use Velocity for parsing the expression, does it means there is no way to overcome the issue above?
Regards, Vincenzo
JEXL does not use Velocity for parsing; JEXL was inspired by Velocity and JSP/EL (10 years ago). Anyhow, if you are able to use the most current JEXL code (ie compile JEXL 3.2 from the trunk), you should be able to remove the quotes only from the first member of your expression. Example test case follows. Cheers
public static class CustomEnzo {
private final String name;
public CustomEnzo(String nm) {
this.name = nm;
}
public CustomEnzo getItem() {
return this;
}
public String getName() {
return name;
}
}
@Test
public void testEnzo001() throws Exception {
Map<String, Object> cmap = new TreeMap<>();
Map<String, CustomEnzo> vmap = new TreeMap<>();
vmap.put("ProductName", new CustomEnzo("000"));
vmap.put("Product.Name", new CustomEnzo("001"));
vmap.put("Product...Name", new CustomEnzo("002"));
cmap.put("GroupName", vmap);
JexlContext ctxt = new MapContext(cmap);
JexlEngine jexl = new JexlBuilder().create();
Object result;
result = jexl.createExpression("GroupName.ProductName.item.name").evaluate(ctxt);
Assert.assertEquals("000", result);
result = jexl.createExpression("GroupName.'ProductName'.item.name").evaluate(ctxt);
Assert.assertEquals("000", result);
result = jexl.createExpression("GroupName.'Product.Name'.item.name").evaluate(ctxt);
Assert.assertEquals("001", result);
result = jexl.createExpression("GroupName.'Product...Name'.item.name").evaluate(ctxt);
Assert.assertEquals("002", result);
result = jexl.createExpression("GroupName.'Product...Name'.'item'.'name'").evaluate(ctxt);
Assert.assertEquals("002", result);
}