Skip to content

Instantly share code, notes, and snippets.

@spullara
Last active November 28, 2022 22:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spullara/72ebb61fd07b9bcda7e522a3a9dcd9c5 to your computer and use it in GitHub Desktop.
Save spullara/72ebb61fd07b9bcda7e522a3a9dcd9c5 to your computer and use it in GitHub Desktop.
Walk the mustache parse tree
package com.github.mustachejava;
import com.github.mustachejava.codes.IterableCode;
import com.github.mustachejava.codes.NotIterableCode;
import com.github.mustachejava.codes.ValueCode;
import org.junit.Test;
import java.io.StringReader;
/**
* Created by sam on 6/5/17.
*/
public class WalkTree {
@Test
public void testTree() {
MustacheFactory mf = new DefaultMustacheFactory();
Mustache test = mf.compile(new StringReader("Hello {{firstName}} {{lastName}}\n" +
"Your Education Qualification is\n" +
"\n" +
"{{#qualification}}\n" +
" - {{ college}}\n" +
" - {{ years }}\n" +
"{{/qualification }}"), "test");
walk(test, 0);
}
private void walk(Code test, int depth) {
for (Code code : test.getCodes()) {
if (code instanceof ValueCode) {
indent(depth);
System.out.println("- " + code.getName());
} else if (code instanceof NotIterableCode) {
System.out.println("^" + code.getName());
walk(code, depth + 2);
} else if (code instanceof IterableCode) {
System.out.println("#" + code.getName());
walk(code, depth + 2);
}
}
}
private void indent(int depth) {
for (int i = 0; i < depth; i++) {
System.out.print(" ");
}
}
}
@spullara
Copy link
Author

spullara commented Jun 5, 2017

Output is:

- firstName
- lastName
#qualification
  - college
  - years

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment