Guest User

Untitled

a guest
Dec 18th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. import com.github.mustachejava.DefaultMustacheFactory;
  2. import com.github.mustachejava.Mustache;
  3. import com.github.mustachejava.MustacheFactory;
  4. import com.github.mustachejava.reflect.MissingWrapper;
  5. import com.github.mustachejava.reflect.ReflectionObjectHandler;
  6. import com.github.mustachejava.util.Wrapper;
  7.  
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.io.StringReader;
  11. import java.util.Arrays;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15.  
  16. public class MustacheExample {
  17.  
  18. public List<Item> items() {
  19. return Arrays.asList(new Item("Item 1", "$19.99", Arrays.asList(new Feature("New!"), new Feature("Awesome!"))),
  20. new Item("Item 2", "$29.99", Arrays.asList(new Feature("Old."), new Feature("Ugly."))));
  21. }
  22.  
  23. public static void main(String[] args) throws IOException {
  24. MustacheExample example = new MustacheExample();
  25.  
  26. MustacheFactory mf = example.getMustacheFactory(); // new DefaultMustacheFactory();
  27.  
  28. // Mustache mustache = mf.compile("template.mustache");
  29. // mustache.execute(new PrintWriter(System.out), new MustacheExample()).flush();
  30.  
  31. // String templete = "{{#items}}\r\n" +
  32. // " Name: {{name}}\r\n" +
  33. // " Price: {{price}}\r\n" +
  34. // " {{#features}}\r\n" +
  35. // " Feature: {{description}}\r\n" +
  36. // " {{/features}}\r\n" +
  37. // "{{/items}}";
  38. //
  39. // Mustache mustache2 = mf.compile(new StringReader(templete), "any");
  40. // mustache2.execute(new PrintWriter(System.out), new
  41. // MustacheExample()).flush();'
  42.  
  43. String templete2 = "Hello {{name|mustache.java}} \n" + "You have just won ${{value}}! \n" + "{{#in_ca}} "
  44. + "Well, ${{taxed_value}}, after taxes. \n" + "{{/in_ca}} ";
  45.  
  46. Map<String, Object> ctx = new HashMap<String, Object>();
  47. // ctx.put("name", "Chris");
  48. ctx.put("value", "10000");
  49. ctx.put("taxed_value", 10000 - (10000 * 0.4));
  50. ctx.put("in_ca", true);
  51.  
  52. Mustache mustache3 = mf.compile(new StringReader(templete2), "any");
  53.  
  54. mustache3.execute(new PrintWriter(System.out), ctx).flush();
  55.  
  56. }
  57.  
  58. public MustacheFactory getMustacheFactory() {
  59. DefaultMustacheFactory mf = new DefaultMustacheFactory();
  60. mf.setObjectHandler(new ReflectionObjectHandler() {
  61. @Override
  62. public Wrapper find(String name, List<Object> scopes) {
  63. int i;
  64. if ((i = name.indexOf("|")) != -1) {
  65. String newName = name.substring(0, i);
  66. String defaultValue = name.substring(i + 1);
  67. Wrapper wrapper = super.find(newName, scopes);
  68. if (wrapper instanceof MissingWrapper) {
  69. return scopes1 -> {
  70. // Test the guards returned in the missing wrapper
  71. //System.out.println("missing -> " + wrapper.call(scopes1));
  72. return defaultValue;
  73. };
  74. }
  75. return wrapper;
  76. }
  77. return super.find(name, scopes);
  78. }
  79. });
  80. return mf;
  81. }
  82.  
  83. static class Feature {
  84. private String description;
  85.  
  86. public Feature(String description) {
  87. this.description = description;
  88. }
  89.  
  90. public String getDescription() {
  91. return description;
  92. }
  93.  
  94. public void setDescription(String description) {
  95. this.description = description;
  96. }
  97. }
  98.  
  99. public static class Item {
  100.  
  101. private String name, price;
  102. private List<Feature> features;
  103.  
  104. public Item(String name, String price, List<Feature> features) {
  105. this.name = name;
  106. this.price = price;
  107. this.features = features;
  108. }
  109.  
  110. public String getName() {
  111. return name;
  112. }
  113.  
  114. public void setName(String name) {
  115. this.name = name;
  116. }
  117.  
  118. public String getPrice() {
  119. return price;
  120. }
  121.  
  122. public void setPrice(String price) {
  123. this.price = price;
  124. }
  125.  
  126. public List<Feature> getFeatures() {
  127. return features;
  128. }
  129.  
  130. public void setFeatures(List<Feature> features) {
  131. this.features = features;
  132. }
  133. }
  134. }
Add Comment
Please, Sign In to add comment