forked from spullara/mustache.java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartialCode.java
More file actions
105 lines (91 loc) · 3.12 KB
/
PartialCode.java
File metadata and controls
105 lines (91 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.github.mustachejava.codes;
import com.github.mustachejava.*;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
public class PartialCode extends DefaultCode {
protected final String extension;
protected final String dir;
protected Mustache partial;
protected int recrusionLimit;
protected boolean isRecursive;
protected PartialCode(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 PartialCode(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, tc);
}
}
@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 List<Object> scopes) {
DepthLimitedWriter depthLimitedWriter = null;
// If the mustache wasn't found to recurse at compilation time we
// don't need to track the recursion depth and therefore don't need
// to create and use the DepthLimitedWriter. Another allocation slain.
if (isRecursive) {
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, tc);
}
writer = depthLimitedWriter;
}
Writer execute = executePartial(writer, scopes);
if (isRecursive) {
assert depthLimitedWriter != null;
depthLimitedWriter.decr();
}
return appendText(execute);
}
protected Writer executePartial(Writer writer, final List<Object> scopes) {
return partial.execute(writer, scopes);
}
@Override
public synchronized void init() {
filterText();
partial = df.compilePartial(partialName());
if (partial instanceof DefaultMustache && ((DefaultMustache)partial).isRecursive()) {
isRecursive = true;
}
if (partial == null) {
throw new MustacheException("Failed to compile partial: " + name, tc);
}
}
/**
* 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);
}
}