Last active
December 24, 2015 20:19
-
-
Save manzke/6856743 to your computer and use it in GitHub Desktop.
A class to format message which contain named parameters like "send an email from {sender} to {recipient}".
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
package de.devsurf.echo.sync.utils; | |
import java.util.Collections; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import com.google.common.base.Preconditions; | |
/** | |
* TODO | |
* - add optional filling of missing parameter | |
* - add optional failing on missing parameter | |
* - add optional failing on brackets errors | |
*/ | |
public class Templating { | |
public static final String LEFT_DELIMITER = "${"; | |
public static final String RIGHT_DELIMITER = "}"; | |
private Map<String, String> replacements; | |
private Map<Integer, String> parameters = new LinkedHashMap<>(); | |
public static String format(String message, Map<String, String> parameters) { | |
Templating formatter = new Templating(); | |
formatter.replacements = parameters; | |
return formatter.format(message); | |
} | |
private Templating() { | |
}; | |
public String format(String message) { | |
StringBuilder result = new StringBuilder(message.length()); | |
String pattern = analyze(message); | |
int lastOffset = 0; | |
for (Entry<Integer, String> entry : parameters.entrySet()) { | |
int index = entry.getKey(); | |
result.append(pattern.substring(lastOffset, index)); | |
lastOffset = index; | |
String key = entry.getValue(); | |
if (replacements.containsKey(key)) { | |
String value = replacements.get(key); | |
if (value != null) { | |
result.append(value); | |
continue; | |
} | |
} | |
result.append(LEFT_DELIMITER).append(RIGHT_DELIMITER); | |
} | |
result.append(pattern.substring(lastOffset, pattern.length())); | |
return result.toString(); | |
} | |
public String analyze(String message) throws IllegalArgumentException { | |
StringBuilder outputBuilder = new StringBuilder(message.length()); | |
int index = 0; | |
for (int leftIndex = 0; leftIndex > -1;) { | |
leftIndex = message.indexOf(LEFT_DELIMITER, index); | |
int rightIndex; | |
if (leftIndex >= 0) { | |
rightIndex = message.indexOf(RIGHT_DELIMITER, leftIndex | |
+ LEFT_DELIMITER.length()); | |
// sanity check | |
int newLextIndex = message.indexOf(LEFT_DELIMITER, leftIndex | |
+ LEFT_DELIMITER.length()); | |
Preconditions.checkArgument(rightIndex > 0, | |
"Right Bracket is missing near position %s", leftIndex); | |
Preconditions.checkArgument(rightIndex < newLextIndex | |
|| newLextIndex == -1, | |
"Right Bracket is missing near position %s", leftIndex); | |
outputBuilder.append(message.substring(index, leftIndex)); | |
parameters.put(outputBuilder.length(), message.substring( | |
leftIndex + LEFT_DELIMITER.length(), rightIndex)); | |
index = rightIndex + RIGHT_DELIMITER.length(); | |
} | |
} | |
outputBuilder.append(message.substring(index)); | |
return outputBuilder.toString(); | |
} | |
public static void main(String[] args) { | |
System.out.println(Templating.format( | |
"send an email from ${sender} to ${recipient}", | |
Collections.singletonMap("sender", "[email protected]"))); | |
try { | |
System.out.println(Templating.format( | |
"send an email from ${sender to ${recipient}", | |
Collections.singletonMap("sender", "[email protected]"))); | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
} | |
try { | |
System.out.println(Templating.format( | |
"send an email from ${sender} to ${recipient", | |
Collections.singletonMap("sender", "[email protected]"))); | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
} | |
System.out.println(Templating.format( | |
"send an email from ${sender} to ${recipient}", | |
Collections.singletonMap("bla", "[email protected]"))); | |
System.out.println(Templating.format( | |
"A class to format message which contain named parameters like \"send an email from ${sender} to ${recipient}\".", | |
Collections.singletonMap("sender", "[email protected]"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment