From b9748e52766b964cf0e19a37d240ba9499099a22 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 21 Jul 2015 17:48:02 +0100 Subject: [PATCH 1/3] Dynamic Partial --- .../mustachejava/DefaultMustacheVisitor.java | 7 ++ .../github/mustachejava/MustacheParser.java | 5 + .../github/mustachejava/MustacheVisitor.java | 3 + .../codes/DynamicPartialCode.java | 91 +++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 compiler/src/main/java/com/github/mustachejava/codes/DynamicPartialCode.java diff --git a/compiler/src/main/java/com/github/mustachejava/DefaultMustacheVisitor.java b/compiler/src/main/java/com/github/mustachejava/DefaultMustacheVisitor.java index 5998fceb0..6a233eaf0 100644 --- a/compiler/src/main/java/com/github/mustachejava/DefaultMustacheVisitor.java +++ b/compiler/src/main/java/com/github/mustachejava/DefaultMustacheVisitor.java @@ -7,6 +7,7 @@ import com.github.mustachejava.codes.IterableCode; import com.github.mustachejava.codes.NotIterableCode; import com.github.mustachejava.codes.PartialCode; +import com.github.mustachejava.codes.DynamicPartialCode; import com.github.mustachejava.codes.ValueCode; import com.github.mustachejava.codes.WriteCode; import com.github.mustachejava.util.Node; @@ -82,6 +83,12 @@ public void partial(TemplateContext tc, final String variable) { TemplateContext partialTC = new TemplateContext("{{", "}}", tc.file(), tc.line(), tc.startOfLine()); list.add(new PartialCode(partialTC, df, variable)); } + + @Override + public void dynamicPartial(TemplateContext tc, final String variable) { + TemplateContext partialTC = new TemplateContext("{{", "}}", tc.file(), tc.line(), tc.startOfLine()); + list.add(new DynamicPartialCode(partialTC, df, variable)); + } @Override public void value(TemplateContext tc, final String variable, boolean encoded) { diff --git a/compiler/src/main/java/com/github/mustachejava/MustacheParser.java b/compiler/src/main/java/com/github/mustachejava/MustacheParser.java index de1802e67..fd4b5223c 100644 --- a/compiler/src/main/java/com/github/mustachejava/MustacheParser.java +++ b/compiler/src/main/java/com/github/mustachejava/MustacheParser.java @@ -215,6 +215,11 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger sm = delimiters.substring(1, length / 2); em = delimiters.substring(length / 2, length - 1); break; + case '+': + out = write(mv, out, file, currentLine.intValue(), startOfLine); + startOfLine = startOfLine & onlywhitespace; + mv.dynamicPartial(new TemplateContext(sm, em, file, currentLine.get(), startOfLine), variable); + break; default: { if (c == -1) { throw new MustacheException( diff --git a/compiler/src/main/java/com/github/mustachejava/MustacheVisitor.java b/compiler/src/main/java/com/github/mustachejava/MustacheVisitor.java index ea2535872..0176ffad9 100644 --- a/compiler/src/main/java/com/github/mustachejava/MustacheVisitor.java +++ b/compiler/src/main/java/com/github/mustachejava/MustacheVisitor.java @@ -27,5 +27,8 @@ public interface MustacheVisitor { void extend(TemplateContext templateContext, String variable, Mustache mustache); void name(TemplateContext templateContext, String variable, Mustache mustache); + + void dynamicPartial(TemplateContext paramTemplateContext, + String paramString); } diff --git a/compiler/src/main/java/com/github/mustachejava/codes/DynamicPartialCode.java b/compiler/src/main/java/com/github/mustachejava/codes/DynamicPartialCode.java new file mode 100644 index 000000000..aafd732fd --- /dev/null +++ b/compiler/src/main/java/com/github/mustachejava/codes/DynamicPartialCode.java @@ -0,0 +1,91 @@ +package com.github.mustachejava.codes; + +import com.github.mustachejava.*; + +import java.io.IOException; +import java.io.Writer; + +public class DynamicPartialCode extends DefaultCode { + protected final String extension; + protected final String dir; + protected Mustache partial; + protected int recrusionLimit; + + protected DynamicPartialCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String type, String variable) { + super(tc, df, mustache, variable, type); + // Use the name of the parent to get the name of the partial + String file = tc.file(); + int dotindex = file.lastIndexOf("."); + extension = dotindex == -1 ? "" : file.substring(dotindex); + int slashindex = file.lastIndexOf("/"); + dir = file.substring(0, slashindex + 1); + recrusionLimit = df.getRecursionLimit(); + } + + public DynamicPartialCode(TemplateContext tc, DefaultMustacheFactory cf, String variable) { + this(tc, cf, null, ">", variable); + } + + @Override + public void identity(Writer writer) { + try { + if (name != null) { + super.tag(writer, type); + } + appendText(writer); + } catch (IOException e) { + throw new MustacheException(e); + } + } + + @Override + public Code[] getCodes() { + return partial == null ? null : partial.getCodes(); + } + + @Override + public void setCodes(Code[] newcodes) { + partial.setCodes(newcodes); + } + + @Override + public Writer execute(Writer writer, final Object[] scopes) { + DepthLimitedWriter depthLimitedWriter; + if (writer instanceof DepthLimitedWriter) { + depthLimitedWriter = (DepthLimitedWriter) writer; + } else { + depthLimitedWriter = new DepthLimitedWriter(writer); + } + if (depthLimitedWriter.incr() > recrusionLimit) { + throw new MustacheException("Maximum partial recursion limit reached: " + recrusionLimit); + } + try{ + if(get(scopes)!=null) + partial = df.compilePartial(get(scopes) +".html"); + }catch(Exception e){ + throw new MustacheException("Failed to compile partial: " + name); + } + if (partial == null) { + throw new MustacheException("Failed to compile partial: " + name); + } + Writer execute = partial.execute(depthLimitedWriter, scopes); + depthLimitedWriter.decr(); + return appendText(execute); + } + + @Override + public synchronized void init() { + filterText(); + } + + /** + * Builds the file name to be included by this partial tag. Default implementation ppends the tag contents with + * the current file's extension. + * + * @return The filename to be included by this partial tag + */ + protected String partialName() { + return df.resolvePartialPath(dir, name, extension); + } + +} From 0691fcf95d86ebf0d4f2f77bbed9586b1f9a7e7e Mon Sep 17 00:00:00 2001 From: ssin26 Date: Wed, 12 Aug 2015 10:27:13 +0100 Subject: [PATCH 2/3] Added test Case --- .../mustachejava/DyanmicPartialTest.java | 26 +++++++++++++++++++ compiler/src/test/resources/index.html | 8 ++++++ compiler/src/test/resources/parent.html | 1 + compiler/src/test/resources/partial.html | 2 ++ 4 files changed, 37 insertions(+) create mode 100644 compiler/src/test/java/com/github/mustachejava/DyanmicPartialTest.java create mode 100644 compiler/src/test/resources/index.html create mode 100644 compiler/src/test/resources/parent.html create mode 100644 compiler/src/test/resources/partial.html diff --git a/compiler/src/test/java/com/github/mustachejava/DyanmicPartialTest.java b/compiler/src/test/java/com/github/mustachejava/DyanmicPartialTest.java new file mode 100644 index 000000000..0b5de1788 --- /dev/null +++ b/compiler/src/test/java/com/github/mustachejava/DyanmicPartialTest.java @@ -0,0 +1,26 @@ +package com.github.mustachejava; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +public class DyanmicPartialTest { + static Map obj = new HashMap(); + + @Test + public void testAbstractClass() throws IOException { + + obj.put("firstName", "Check"); + obj.put("lastName", "Mate"); + obj.put("blogURL", "is something"); + obj.put("parent", "partial"); + + PrintWriter pw = new PrintWriter(System.out); + MustacheFactory mustachefactory = new DefaultMustacheFactory(); + Mustache mustacheTemplate = mustachefactory.compile("index.html"); + mustacheTemplate.execute(pw, obj).close(); + } +} diff --git a/compiler/src/test/resources/index.html b/compiler/src/test/resources/index.html new file mode 100644 index 000000000..f7d27907a --- /dev/null +++ b/compiler/src/test/resources/index.html @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/compiler/src/test/resources/parent.html b/compiler/src/test/resources/parent.html new file mode 100644 index 000000000..e9619b0a3 --- /dev/null +++ b/compiler/src/test/resources/parent.html @@ -0,0 +1 @@ +

Parent is: {{blogURL}}

\ No newline at end of file diff --git a/compiler/src/test/resources/partial.html b/compiler/src/test/resources/partial.html new file mode 100644 index 000000000..0d5428d35 --- /dev/null +++ b/compiler/src/test/resources/partial.html @@ -0,0 +1,2 @@ +

Dynamic Partial is: {{blogURL}}

+ From dad06db2b65853aa97ddcbd00a77667b86fa7cbc Mon Sep 17 00:00:00 2001 From: ssin26 Date: Wed, 12 Aug 2015 14:11:13 +0100 Subject: [PATCH 3/3] Added test Case --- .../com/github/mustachejava/MustacheParser.java | 8 ++++---- .../com/github/mustachejava/MustacheVisitor.java | 2 +- .../mustachejava/codes/DynamicPartialCode.java | 14 +++++++------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/compiler/src/main/java/com/github/mustachejava/MustacheParser.java b/compiler/src/main/java/com/github/mustachejava/MustacheParser.java index fd4b5223c..a62f7357d 100644 --- a/compiler/src/main/java/com/github/mustachejava/MustacheParser.java +++ b/compiler/src/main/java/com/github/mustachejava/MustacheParser.java @@ -216,10 +216,10 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger em = delimiters.substring(length / 2, length - 1); break; case '+': - out = write(mv, out, file, currentLine.intValue(), startOfLine); - startOfLine = startOfLine & onlywhitespace; - mv.dynamicPartial(new TemplateContext(sm, em, file, currentLine.get(), startOfLine), variable); - break; + out = write(mv, out, file, currentLine.intValue(), startOfLine); + startOfLine = startOfLine & onlywhitespace; + mv.dynamicPartial(new TemplateContext(sm, em, file, currentLine.get(), startOfLine), variable); + break; default: { if (c == -1) { throw new MustacheException( diff --git a/compiler/src/main/java/com/github/mustachejava/MustacheVisitor.java b/compiler/src/main/java/com/github/mustachejava/MustacheVisitor.java index 0176ffad9..6acb0f12d 100644 --- a/compiler/src/main/java/com/github/mustachejava/MustacheVisitor.java +++ b/compiler/src/main/java/com/github/mustachejava/MustacheVisitor.java @@ -29,6 +29,6 @@ public interface MustacheVisitor { void name(TemplateContext templateContext, String variable, Mustache mustache); void dynamicPartial(TemplateContext paramTemplateContext, - String paramString); + String paramString); } diff --git a/compiler/src/main/java/com/github/mustachejava/codes/DynamicPartialCode.java b/compiler/src/main/java/com/github/mustachejava/codes/DynamicPartialCode.java index aafd732fd..390657790 100644 --- a/compiler/src/main/java/com/github/mustachejava/codes/DynamicPartialCode.java +++ b/compiler/src/main/java/com/github/mustachejava/codes/DynamicPartialCode.java @@ -60,14 +60,14 @@ public Writer execute(Writer writer, final Object[] scopes) { throw new MustacheException("Maximum partial recursion limit reached: " + recrusionLimit); } try{ - if(get(scopes)!=null) - partial = df.compilePartial(get(scopes) +".html"); - }catch(Exception e){ - throw new MustacheException("Failed to compile partial: " + name); - } + if(get(scopes)!=null) + partial = df.compilePartial(get(scopes) +".html"); + }catch(Exception e){ + throw new MustacheException("Failed to compile partial: " + name); + } if (partial == null) { - throw new MustacheException("Failed to compile partial: " + name); - } + throw new MustacheException("Failed to compile partial: " + name); + } Writer execute = partial.execute(depthLimitedWriter, scopes); depthLimitedWriter.decr(); return appendText(execute);