|
| 1 | +package com.github.mustachejava; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.StringWriter; |
| 6 | +import java.io.Writer; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.Callable; |
| 10 | +import java.util.concurrent.Future; |
| 11 | +import java.util.concurrent.atomic.AtomicLong; |
| 12 | + |
| 13 | +import com.github.mustachejava.codes.PartialCode; |
| 14 | +import com.github.mustachejava.util.GuardException; |
| 15 | +import com.github.mustachejava.util.Wrapper; |
| 16 | + |
| 17 | +/** |
| 18 | + * This allows you to automatically defer evaluation of partials. By default |
| 19 | + * it generates HTML but you can override that behavior. |
| 20 | + */ |
| 21 | +public class DeferringMustacheFactory extends DefaultMustacheFactory { |
| 22 | + |
| 23 | + public static final Object DEFERRED = new Object(); |
| 24 | + |
| 25 | + public DeferringMustacheFactory() { |
| 26 | + } |
| 27 | + |
| 28 | + public DeferringMustacheFactory(String resourceRoot) { |
| 29 | + super(resourceRoot); |
| 30 | + } |
| 31 | + |
| 32 | + public DeferringMustacheFactory(File fileRoot) { |
| 33 | + super(fileRoot); |
| 34 | + } |
| 35 | + |
| 36 | + private static class Deferral { |
| 37 | + final long id; |
| 38 | + final Future<Object> future; |
| 39 | + |
| 40 | + Deferral(long id, Future<Object> future) { |
| 41 | + this.id = id; |
| 42 | + this.future = future; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static class DeferredCallable implements Callable<String> { |
| 47 | + |
| 48 | + private List<Deferral> deferrals = new ArrayList<Deferral>(); |
| 49 | + |
| 50 | + public void add(Deferral deferral) { |
| 51 | + deferrals.add(deferral); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String call() throws Exception { |
| 56 | + StringBuilder sb = new StringBuilder(); |
| 57 | + for (Deferral deferral : deferrals) { |
| 58 | + Object o = deferral.future.get(); |
| 59 | + if (o != null) { |
| 60 | + writeDeferral(sb, deferral, o); |
| 61 | + } |
| 62 | + } |
| 63 | + return sb.toString(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public MustacheVisitor createMustacheVisitor() { |
| 69 | + final AtomicLong id = new AtomicLong(0); |
| 70 | + return new DefaultMustacheVisitor(this) { |
| 71 | + @Override |
| 72 | + public void partial(TemplateContext templateContext, final String variable) { |
| 73 | + TemplateContext partialTC = new TemplateContext("{{", "}}", templateContext.file(), |
| 74 | + templateContext.line()); |
| 75 | + final Long divid = id.incrementAndGet(); |
| 76 | + list.add(new PartialCode(partialTC, cf, variable) { |
| 77 | + Wrapper deferredWrapper; |
| 78 | + |
| 79 | + @Override |
| 80 | + protected Writer partialExecute(Writer writer, final Object[] scopes) { |
| 81 | + final Object object = get(variable, scopes); |
| 82 | + final DeferredCallable deferredCallable = getDeferred(scopes); |
| 83 | + if (object == DEFERRED && deferredCallable != null) { |
| 84 | + try { |
| 85 | + writeTarget(writer, divid); |
| 86 | + } catch (IOException e) { |
| 87 | + throw new MustacheException("Failed to write", e); |
| 88 | + } |
| 89 | + deferredCallable.add( |
| 90 | + new Deferral(divid, getExecutorService().submit(new Callable<Object>() { |
| 91 | + @Override |
| 92 | + public Object call() { |
| 93 | + try { |
| 94 | + StringWriter writer = new StringWriter(); |
| 95 | + execute(writer, object, scopes).close(); |
| 96 | + return writer.toString(); |
| 97 | + } catch (IOException e) { |
| 98 | + throw new MustacheException("Failed to writer", e); |
| 99 | + } |
| 100 | + } |
| 101 | + }))); |
| 102 | + return writer; |
| 103 | + } else { |
| 104 | + return super.partialExecute(writer, scopes); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private DeferredCallable getDeferred(Object[] scopes) { |
| 109 | + try { |
| 110 | + if (deferredWrapper == null) { |
| 111 | + deferredWrapper = getObjectHandler().find("deferred", scopes); |
| 112 | + } |
| 113 | + return (DeferredCallable) deferredWrapper.call(scopes); |
| 114 | + } catch (GuardException e) { |
| 115 | + deferredWrapper = null; |
| 116 | + return getDeferred(scopes); |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + protected void writeTarget(Writer writer, Long divid) throws IOException { |
| 125 | + writer.append("<div id=\""); |
| 126 | + writer.append(divid.toString()); |
| 127 | + writer.append("\"></div>\n"); |
| 128 | + } |
| 129 | + |
| 130 | + protected static void writeDeferral(StringBuilder sb, Deferral deferral, Object o) { |
| 131 | + sb.append("<script>document.getElementById(\""); |
| 132 | + sb.append(deferral.id); |
| 133 | + sb.append("\").innerHTML=\""); |
| 134 | + sb.append(o.toString().replace("<", "<").replace("\"", "\\\"").replace("\n", "\\n")); |
| 135 | + sb.append("\";</script>"); |
| 136 | + } |
| 137 | +} |
0 commit comments