Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.Writer;
  3. import java.util.Map;
  4.  
  5. public interface Template {
  6. public void write(Writer out, Map<String, String> params) throws IOException;
  7. }
  8.  
  9. import java.io.*;
  10. import java.lang.reflect.Constructor;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. import javax.tools.*;
  16. import org.mdkt.compiler.InMemoryJavaCompiler;
  17.  
  18. public class TemplateCompiler {
  19. private static final Pattern SUBST_PAT = Pattern.compile(
  20. "(?<LITERAL>.*?)(?:\{\{(?<SUBST>[^}]*)\}\})"
  21. );
  22.  
  23. /**
  24. * Instantiates a <code>Template</code> that performs simple
  25. * text substitutions for <code>{{PLACEHOLDERS}}</code>.
  26. */
  27. public static Template compile(String templateText) {
  28. int rest = 0;
  29. StringBuilder script = new StringBuilder(
  30. "import java.io.IOException;n" +
  31. "import java.io.Writer;n" +
  32. "import java.util.Map;n" +
  33. "public class C implements Template {n" +
  34. " public void write(Writer out, Map<String, String> params) throws IOException {n"
  35. );
  36. for (Matcher m = SUBST_PAT.matcher(templateText); m.find(); rest = m.end()) {
  37. script.append("out.write(")
  38. .append(stringLiteral(m.group("LITERAL")))
  39. .append(");nout.write(params.get(")
  40. .append(stringLiteral(m.group("SUBST")))
  41. .append("));n");
  42. }
  43. script.append("out.write(")
  44. .append(stringLiteral(templateText.substring(rest)))
  45. .append(");n");
  46.  
  47. script.append("out.flush();n")
  48. .append("}}");
  49.  
  50. try {
  51. @SuppressWarnings("unchecked")
  52. Class <? extends Template> c = (Class <? extends Template>)InMemoryJavaCompiler.compile("C", script.toString());
  53. Constructor<? extends Template> ctr = c.getConstructor();
  54. return ctr.newInstance();
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. return null;
  58. }
  59. }
  60.  
  61. private static final Pattern UNSAFE_CHARS = Pattern.compile("[^A-Za-z0-9 ]");
  62.  
  63. private static String stringLiteral(String s) {
  64. StringBuffer result = new StringBuffer(""");
  65. Matcher matcher = UNSAFE_CHARS.matcher(s);
  66. while (matcher.find()) {
  67. char c = matcher.group().charAt(0);
  68. switch (c) {
  69. // JLS SE7 3.10.5:
  70. // It is a compile-time error for a line terminator to appear
  71. case 'r':
  72. matcher.appendReplacement(result, "\r");
  73. break;
  74. case 'n':
  75. matcher.appendReplacement(result, "\n");
  76. break;
  77. default:
  78. String.format("\\u%04x", (int)c);
  79. }
  80. }
  81. matcher.appendTail(result);
  82. result.append(""");
  83. return result.toString();
  84. }
  85. }
  86.  
  87. Template t = TemplateCompiler.compile(
  88. "Dear {{USER_NAME}},nn" +
  89. "According to our records, your phone number is {{USER_PHONE}} and " +
  90. "your e-mail address is {{USER_EMAIL}}. If this is incorrect, please " +
  91. "go to {{LOGIN_URL}} and update your contact information."
  92. );
  93. for (Contact c : contacts) {
  94. Map<String, String> params = new HashMap<>();
  95. params.put("USER_NAME", c.getUserName());
  96. params.put("USER_EMAIL", c.getEmail());
  97. params.put("USER_PHONE", c.getPhone());
  98. params.put("LOGIN_URL", c.getLoginUrl());
  99. StringWriter sw = new StringWriter();
  100. t.write(sw, params);
  101. sw.toString();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement