public class TemplateMatcher extends Object
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 vars = new HashMap();
vars.put("user", "santhosh");
vars.put("email", "scott@gmail.com");
System.out.println(matcher.replace(msg, vars));
prints following:
Hai santhosh, your mail to scott@gmail.com has been sent successfully.The two arguments to
TemplateMatcher are leftBrace and rightBrace.
String msg = "Hai ___user___, your mail to ___email___ has been sent successfully.";
TemplateMatcher matcher = new TemplateMatcher("___", "___");
Map vars = new HashMap();
vars.put("user", "santhosh");
vars.put("email", "scott@gmail.com");
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
new TemplateMatcher(leftBrace):
String msg = "Hai $user, your mail to $email has been sent successfully.";
TemplateMatcher matcher = new TemplateMatcher("$");
Map vars = new HashMap();
vars.put("user", "santhosh");
vars.put("email", "scott@gmail.com");
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 filetemplate.txt which contains:
Hai ${user},
your mail to ${email} has been sent successfully.
running the following code:
TemplateMatcher matcher = new TemplateMatcher("${", "}");
Map vars = new HashMap();
vars.put("user", "santhosh");
vars.put("email", "scott@gmail.com");
matcher.replace(new FileReader("templte.txt"), new FileWriter("result.txt"), vars);
will creates file result.txt with following content:
Hai santhosh,
your mail to scott@gmail.com has been sent successfully.
Copying Files/Directories:
TemplateMatcher provides method to copy files/directories:
public void copyInto(File source, File targetDir, MapName of each file and directory is treated as a template.variables) throws IOException;
${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 vars = new HashMap();
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{
}
| Modifier and Type | Class and Description |
|---|---|
static class |
TemplateMatcher.MapVariableResolver |
static interface |
TemplateMatcher.VariableResolver |
| Constructor and Description |
|---|
TemplateMatcher(String prefix) |
TemplateMatcher(String leftBrace,
String rightBrace) |
| Modifier and Type | Method and Description |
|---|---|
void |
copyInto(File source,
File targetDir,
Map<String,String> variables) |
void |
copyInto(File source,
File targetDir,
TemplateMatcher.VariableResolver resolver) |
static void |
main(String[] args) |
String |
replace(CharSequence input,
TemplateMatcher.VariableResolver resolver) |
void |
replace(Reader reader,
Writer writer,
Map<String,String> variables) |
void |
replace(Reader reader,
Writer writer,
TemplateMatcher.VariableResolver resolver) |
String |
replace(String input,
Map<String,String> variables) |
public TemplateMatcher(String prefix)
public String replace(CharSequence input, TemplateMatcher.VariableResolver resolver)
public void replace(Reader reader, Writer writer, TemplateMatcher.VariableResolver resolver) throws IOException
IOExceptionpublic void replace(Reader reader, Writer writer, Map<String,String> variables) throws IOException
IOExceptionpublic void copyInto(File source, File targetDir, TemplateMatcher.VariableResolver resolver) throws IOException
IOExceptionpublic void copyInto(File source, File targetDir, Map<String,String> variables) throws IOException
IOExceptionpublic static void main(String[] args)
Copyright © 2021. All rights reserved.