forked from spullara/mustache.java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMustache.java
More file actions
112 lines (95 loc) · 2.97 KB
/
Mustache.java
File metadata and controls
112 lines (95 loc) · 2.97 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
package com.github.mustachejava;
import com.github.mustachejava.util.BaseIndentWriter;
import com.github.mustachejava.util.IndentWriter;
import com.github.mustachejava.util.InternalArrayList;
import com.github.mustachejava.util.Node;
import java.io.Writer;
import java.util.List;
import static com.github.mustachejava.ObjectHandler.makeList;
import static java.util.Collections.addAll;
/**
* The interface to Mustache objects
*/
public interface Mustache extends Code {
/**
* Append text to the mustache output.
*
* @param text the text to append
*/
void append(String text);
/**
* Deep clone of the mustache object.
*
* @return the clone
*/
Object clone();
/**
* Execute the mustache object with a given writer and a single scope context.
*
* @param writer write the output of the executed template here
* @param scope the root object to use
* @return the new writer
*/
default Writer execute(Writer writer, Object scope) {
return execute(writer, makeList(scope));
}
// Support the previous behavior for users
default Writer execute(Writer writer, Object[] scopes) {
List<Object> newscopes = new InternalArrayList<>();
addAll(newscopes, scopes);
return execute(writer, newscopes);
}
/**
* Execute the mustache with a given writer and an array of scope objects. The
* scopes are searched right-to-left for references.
*
* @param writer write the output of the executed template here
* @param scopes an ordered list of scopes for variable resolution
* @return the new writer
*/
default Writer execute(Writer writer, List<Object> scopes) {
return execute(new BaseIndentWriter(writer), scopes);
}
IndentWriter execute(IndentWriter writer, List<Object> scopes);
/**
* Get the underlying code objects.
*
* @return the array of child codes
*/
Code[] getCodes();
/**
* Execute the mustache to output itself.
*
* @param writer write the output of the executed template here
*/
void identity(IndentWriter writer);
default void identity(Writer writer) {
this.identity(new BaseIndentWriter(writer));
}
/**
* Initialize the mustache before executing. This is must be called at least once
* and is normally called already by the time you have a mustache instance.
*/
void init();
/**
* Change the underlying codes of the mustache implementation.
*
* @param codes set the children of this code
*/
void setCodes(Code[] codes);
/**
* Only executes the codes. Does not append the text.
*
* @param writer write the output of the executed template here
* @param scopes the array of scopes to execute
* @return the replacement writer
*/
IndentWriter run(IndentWriter writer, List<Object> scopes);
/**
* Invert this mustache given output text.
*
* @param text the text to parse
* @return a tree of nodes representing the variables that when passed as a scope would reproduce the text
*/
Node invert(String text);
}