Last active
January 16, 2018 11:16
-
-
Save volyx/86fadc74ecc51b947863e9afd67567d2 to your computer and use it in GitHub Desktop.
Fully extendable java html template engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// regex for uri template pattern | |
public static Pattern TEMPLATE_PATTERN = Pattern.compile("\\{(.*?)\\}"); | |
public static String templateReplace(String template, | |
Map<String, Object> properties) { | |
String result = template; | |
try { | |
if (template != null && template.contains("{") | |
&& template.contains("}")) { | |
Matcher m = TEMPLATE_PATTERN.matcher(template); | |
while (m.find()) { | |
String param = m.group(1); | |
if (properties.containsKey(param)) { | |
// replace template tokens | |
result = template.replaceAll( | |
"\\{" + Pattern.quote(param) + "\\}", | |
properties.get(param).toString()); | |
} | |
} | |
} | |
} catch (Exception e) { | |
logger.error("An error occurred while replacing tokens in [" | |
+ template + "]", e); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment