-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathDefaultMustacheVisitor.java
More file actions
135 lines (115 loc) · 4.32 KB
/
DefaultMustacheVisitor.java
File metadata and controls
135 lines (115 loc) · 4.32 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.github.mustachejava;
import com.github.mustachejava.codes.DefaultCode;
import com.github.mustachejava.codes.DefaultMustache;
import com.github.mustachejava.codes.ExtendCode;
import com.github.mustachejava.codes.ExtendNameCode;
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;
import java.io.Writer;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
/**
* The default implementation that builds up Code lists
*/
public class DefaultMustacheVisitor implements MustacheVisitor {
protected static Logger logger = Logger.getLogger(DefaultMustacheVisitor.class.getSimpleName());
private static final Code EOF = new DefaultCode() {
@Override
public Node invert(Node node, String text, AtomicInteger position) {
return text.length() == position.get() ? node : null;
}
};
protected final List<Code> list = new LinkedList<Code>();
private final Map<String, PragmaHandler> handlers = new HashMap<String, PragmaHandler>() {{
put("implicit-iterator", new PragmaHandler() {
@Override
public Code handle(TemplateContext tc, String pragma, String args) {
return new DefaultCode() {
@Override
public Writer execute(Writer writer, Object[] scopes) {
return super.execute(writer, scopes);
}
};
}
});
}};
protected DefaultMustacheFactory df;
public DefaultMustacheVisitor(DefaultMustacheFactory df) {
this.df = df;
}
public void addPragmaHandler(String pragma, PragmaHandler handler) {
handlers.put(pragma.toLowerCase(), handler);
}
@Override
public Mustache mustache(TemplateContext templateContext) {
return new DefaultMustache(templateContext, df, list.toArray(new Code[list.size()]), templateContext.file());
}
@Override
public void iterable(TemplateContext templateContext, String variable, Mustache mustache) {
list.add(new IterableCode(templateContext, df, mustache, variable));
}
@Override
public void notIterable(TemplateContext templateContext, String variable, Mustache mustache) {
list.add(new NotIterableCode(templateContext, df, mustache, variable));
}
@Override
public void name(TemplateContext templateContext, String variable, Mustache mustache) {
list.add(new ExtendNameCode(templateContext, df, mustache, variable));
}
@Override
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) {
list.add(new ValueCode(tc, df, variable, encoded));
}
@Override
public void write(TemplateContext tc, final String text) {
if (text.length() > 0) {
int size = list.size();
if (size > 0) {
Code code = list.get(size - 1);
code.append(text);
} else {
list.add(new WriteCode(tc, df, text));
}
}
}
@Override
public void pragma(TemplateContext tc, String pragma, String args) {
PragmaHandler pragmaHandler = handlers.get(pragma.toLowerCase());
if (pragmaHandler == null) {
// By default, warn that no pragmas are understood
logger.warning("Unimplemented pragma: " + pragma);
} else {
Code code = pragmaHandler.handle(tc, pragma, args);
if (code != null) {
list.add(code);
}
}
}
@Override
public void eof(TemplateContext templateContext) {
list.add(EOF);
}
@Override
public void extend(TemplateContext templateContext, String variable, Mustache mustache) {
list.add(new ExtendCode(templateContext, df, mustache, variable));
}
}