jlibs.core.util.regex
Class TemplateMatcher
java.lang.Object
jlibs.core.util.regex.TemplateMatcher
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.
For example:
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
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 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 file template.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, Map 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 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{
}
- Author:
- Santhosh Kumar T
|
Method Summary |
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)
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
TemplateMatcher
public TemplateMatcher(String leftBrace,
String rightBrace)
TemplateMatcher
public TemplateMatcher(String prefix)
replace
public String replace(CharSequence input,
TemplateMatcher.VariableResolver resolver)
replace
public String replace(String input,
Map<String,String> variables)
replace
public void replace(Reader reader,
Writer writer,
TemplateMatcher.VariableResolver resolver)
throws IOException
- Throws:
IOException
replace
public void replace(Reader reader,
Writer writer,
Map<String,String> variables)
throws IOException
- Throws:
IOException
copyInto
public void copyInto(File source,
File targetDir,
TemplateMatcher.VariableResolver resolver)
throws IOException
- Throws:
IOException
copyInto
public void copyInto(File source,
File targetDir,
Map<String,String> variables)
throws IOException
- Throws:
IOException
main
public static void main(String[] args)
Copyright © 2018. All rights reserved.