
org.bitbucket.gkutiel.at.Render Maven / Gradle / Ivy
package org.bitbucket.gkutiel.at;
import static org.bitbucket.gkutiel.at.Parser.parser;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class Render {
static Context context(final Context parent, final JsonElement e) {
if (e.isJsonObject()) {
final ContextMap ctx = new ContextMap(parent);
e.getAsJsonObject().entrySet().forEach(en -> ctx.set(en.getKey(), context(ctx, en.getValue())));
return ctx;
}
if (e.isJsonArray()) {
final ContextList ctx = new ContextList(parent);
e.getAsJsonArray().forEach(el -> ctx.add(context(ctx, el)));
return ctx;
}
return new ContextVal(parent, e.getAsString());
}
static Context context(final String json) {
return context(null, jsonParser.parse(json));
}
public static String render(final String template, final Context ctx) {
final VisitorCmd v = new VisitorCmd();
parser(template).parse().accept(v);
return v.getRootCmd().eval(ctx);
}
public static String render(final String template, final String json) {
return render(template, context(json));
}
static final JsonParser jsonParser = new JsonParser();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy