To do that a variable->value mapping must be provided.
For example, if name="Cenk" and the template string is "Hello {$name}" the result would be "Hello Cenk".
- Should evaluate template single variable expression:
mapOfVariables.put("name","Cenk");
templateEngine.evaluate("Hello {$name}", mapOfVariables)
=> should evaluate to "Hello Cenk"
- Should evaluate template with multiple expressions:
mapOfVariables.put("firstName","Cenk");
mapOfVariables.put("lastName","Civici");
templateEngine.evaluate("Hello {$firstName} ${lastName}", mapOfVariables);
=> should evaluate to "Hello Cenk Civici"
- Should give error if template variable does not exist in the map:
map empty
templateEngine.evaluate("Hello {$firstName} ", mapOfVariables);
=> should throw missingvalueexception
- Should evaluate complex cases:
Case 1:
mapOfVariables.put("name","Cenk");
templateEngine.evaluate("Hello ${$name}}", mapOfVariables);
=> should evaluate to "Hello ${Cenk}"
Case 2:
mapOfVariables.put("name","Cenk");
mapOfVariables.put("Cenk ","ABCCenk");
templateEngine.evaluate("Hello ${$name}}", mapOfVariables);
=> should evaluate to "Hello ABCCenk"
import java.util.Map;
import com.techdive.exception.MissingValueExcpetion;
public interface TemplateEngine<T> {
T evaluate(T t, Map<T, T> map) throws MissingValueExcpetion;
}
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.techdive.exception.MissingValueExcpetion;
public class StringTemplateEngine implements TemplateEngine<String> {
private String startCharRegex = "\\$";
private String startBracketRegex = "\\{";
private String endBracketRegex = "\\}";
/**
* @return the startCharRegex
*/
public String getStartCharRegex() {
return startCharRegex;
}
/**
* @param startCharRegex the startCharRegex to set
*/
public void setStartCharRegex(String startCharRegex) {
this.startCharRegex = startCharRegex;
}
/**
* @return the startBracketRegex
*/
public String getStartBracketRegex() {
return startBracketRegex;
}
/**
* @param startBracketRegex the startBracketRegex to set
*/
public void setStartBracketRegex(String startBracketRegex) {
this.startBracketRegex = startBracketRegex;
}
/**
* @return the endBracketRegex
*/
public String getEndBracketRegex() {
return endBracketRegex;
}
/**
* @param endBracketRegex the endBracketRegex to set
*/
public void setEndBracketRegex(String endBracketRegex) {
this.endBracketRegex = endBracketRegex;
}
@Override
public String evaluate(String patternString,
Map<String, String> mapOfVariables) throws MissingValueExcpetion {
if (mapOfVariables.isEmpty())
throw new MissingValueExcpetion("Empty Map");
String finalStr = patternString;
for (String s : mapOfVariables.keySet()) {
Pattern patt = Pattern.compile(startCharRegex + startBracketRegex
+ s + endBracketRegex);
Matcher m = patt.matcher(finalStr);
if (m.find()) {
String text = m.replaceAll(mapOfVariables.get(s));
finalStr = text;
}
}
return finalStr;
}
}
public class MissingValueExcpetion extends Exception {
/**
*
*/
private static final long serialVersionUID = 511983494959375382L;
public MissingValueExcpetion() {
super();
}
public MissingValueExcpetion(String message) {
super(message);
System.out.println("Exception occured "+message);
}
}
import java.util.LinkedHashMap;
import java.util.Map;
import com.n4.exception.MissingValueExcpetion;
import com.n4.serivce.StringTemplateEngine;
import com.n4.serivce.TemplateEngine;
/**
* @author Arun
*
* To Test the String Template Engine
*
*/
public class TemplateEngineTest {
public static void main(String[] args) {
TemplateEngine<String> templateEngine = new StringTemplateEngine();
Map<String, String> map = new LinkedHashMap<String, String>();//To maintain the order in which key/value are inserted
map.put("name", "john");
map.put("john", "clement");
map.put("clement", "alex");
String templateString = "Hello ${clement} how are you ${${${name}}}";
try {
String instStr = templateEngine.evaluate(templateString, map);
System.out.println(instStr);
} catch (MissingValueExcpetion e) {
System.out.println(e.getMessage());
}
}
}