Skip to content

TemplateMatcher

Santhosh Kumar Tekuri edited this page Mar 18, 2015 · 1 revision

TemplateMatcher is a simple template engine provided with jlibs.

import jlibs.core.util.regex.TemplateMatcher;

String msg = "Hai ${user}, your mail to ${email} has been sent successfully.";

TemplateMatcher matcher = new TemplateMatcher("${", "}");

Map<String, String> vars = new HashMap<String, String>();
vars.put("user", "santhosh");
vars.put("email", "[email protected]");
System.out.println(matcher.replace(msg, vars));

prints following:

Hai santhosh, your mail to [email protected] has been sent successfully.

The two arguments to TemplateMatcher are leftBrace and rightBrace.
For example:

String msg = "Hai ___user___, your mail to ___email___ has been sent successfully.";
TemplateMatcher matcher = new TemplateMatcher("___", "___");

Map<String, String> vars = new HashMap<String, String>();
vars.put("user", "santhosh");
vars.put("email", "[email protected]");
System.out.println(matcher.replace(msg, vars));

also prints the same output;

NOTE:

  • if a variables resolves to null, then it appears as it is in result string

Right Brace is optional. in such case use new TemplateMatcher(leftBrace):

String msg = "Hai $user, your mail to $email has been sent successfully.";

TemplateMatcher matcher = new TemplateMatcher("$");

Map<String, String> vars = new HashMap<String, String>();
vars.put("user", "santhosh");
vars.put("email", "[email protected]");
System.out.println(matcher.replace(msg, vars));

also prints the same output;

Variable Resolution:

you can also resolve variables dynamically:

String msg = "Hai ${user.name}, you are using JVM from ${java.vendor}.";

TemplateMatcher matcher = new TemplateMatcher("${", "}");
String result = matcher.replace(msg, new TemplateMatcher.VariableResolver(){
@Override
public String resolve(String variable){
return System.getProperty(variable);
}
});

prints

Hai santhosh, you are using JVM from Apple Inc..

VariableResolver interface contains single method:

public String resolve(String variable)

Using with writers:

let us say you have file template.txt which contains:

Hai ${user},
your mail to ${email} has been sent successfully.

running the following code:

TemplateMatcher matcher = new TemplateMatcher("${", "}");

Map<String, String> vars = new HashMap<String, String>();
vars.put("user", "santhosh");
vars.put("email", "[email protected]");
matcher.replace(new FileReader("templte.txt"), new FileWriter("result.txt"), vars);

will creates file result.txt with following content:

Hai santhosh,
your mail to [email protected] has been sent successfully.

Copying Files/Directories:

TemplateMatcher provides method to copy files/directories:

public void copyInto(File source, File targetDir, Map<String, String> variables) throws IOException;

Name of each file and directory is treated as a template.
If name of directory is ${xyz} after applying template, if resolves to "a/b/c",
then it expands into the directory structure a/b/c;

for example we have following directory structure:

${root}
|- ${class}.java

and content of ${class}.java file is:

package ${rootpackage};

public class ${class} extends Comparator{

}

now running following code:

TemplateMatcher matcher = new TemplateMatcher("${", "}");

Map<String, String> vars = new HashMap<String, String>();
vars.put("root", "org/example");
vars.put("rootpackage", "org.example");
vars.put("class", "MyClass");

matcher.copyInto(new File("${root}"), new File("."), vars);

creates:

org
|-example
|-MyClass.java

and content of MyClass.java will be:

package org.example;

public class MyClass extends Comparator{

}
Clone this wiki locally