Jodd is an open-source java library with lot of reusable components and feature rich utilities.
This requires the library jodd-3.3.2.jar to be in classpath.
The following example shows how to use BeanTemplateMacroResolver class.
/**
* @param args
*/ public static void main(String[] args) {
// prepare template string
String template = "${student.name} is a good student. He likes to play ${student.hobby}." +
"\nTemplate parsed on : ${parsedOn}";
// merge template and bean context
BeanTemplateParser templateParser = new BeanTemplateParser();
String result = templateParser.parse(template, new BeanTemplateMacroResolver() {
@Override public Object resolve(String name) { if ("student.name".equals(name)) { return "Sriram";
} else if ("student.hobby".equals(name)) { return "Chess";
}
return name.toUpperCase() + " - NOT FOUND !!";
}
});
System.out.println(result);
}
}
It gives the following output,
Sriram is a good student. He likes to play Chess.
Template parsed on : PARSEDON - NOT FOUND !!