System.out.println("template not found, skipping: " + filename);
return;
}
MustacheFactory mc = new DefaultMustacheFactory(rootDir);
Mustache mustache = mc.compile(filename);
String base = filename.substring(0, filename.lastIndexOf("."));
File file = new File(mocks, base + ".json");
Map parameters = new HashMap<Object, Object>(req.getParameterMap()) {
@Override
public Object get(Object o) {
Object result = super.get(o); // To change body of overridden methods use File |
// Settings | File Templates.
if (result instanceof String[]) {
String[] strings = (String[]) result;
if (strings.length == 1) {
return strings[0];
}
}
return result;
}
};
List<Object> scs = new ArrayList<Object>();
if (null != scopes) scs.addAll(Arrays.<Object>asList(scopes));
scs.add(parameters);
scs.add(new Object() {
Function slots = new TemplateFunction() {
@Override
public String apply(String input) {
return "{{>" + input.trim() + "}}";
}
};
});
if (file.exists()) {
BufferedReader br =
new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
JsonParser parser = JSON_FACTORY.createJsonParser(br);
JsonNode json = parser.readValueAsTree();
br.close();
scs.add(0, toObject(json));
}
mustache.execute(res.getWriter(), scs.toArray());
}
private String simpleEscape(String string) {
return string.replace("&", "&").replace("<", "<").replace(">", ">");
}