use of com.github.jknack.handlebars.Handlebars in project cw-omnibus by commonsguy.
the class WebServerService method onCreate.
@Override
public void onCreate() {
super.onCreate();
handlebars = new Handlebars(new AssetTemplateLoader(getAssets()));
try {
t = handlebars.compile("demo.hbs");
server = new AsyncHttpServer();
server.get("/demo", new TemplateRequestCallback());
server.get("/.*", new AssetRequestCallback());
server.listen(4999);
raiseReadyEvent();
foregroundify();
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Exception starting Web server", e);
}
}
use of com.github.jknack.handlebars.Handlebars in project ddf by codice.
the class KMLTransformerImpl method performDefaultTransformation.
/**
* The default Transformation from a {@link Metacard} to a KML {@link Placemark}. Protected to
* easily allow other default transformations.
*
* @param entry
* - the {@link Metacard} to transform.
* @param urlToMetacard
* @return
* @throws javax.xml.transform.TransformerException
*/
protected Placemark performDefaultTransformation(Metacard entry, String url) throws CatalogTransformerException {
// wrap metacard to work around classLoader/reflection issues
entry = new MetacardImpl(entry);
Placemark kmlPlacemark = KmlFactory.createPlacemark();
kmlPlacemark.setId("Placemark-" + entry.getId());
kmlPlacemark.setName(entry.getTitle());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String effectiveTime = null;
if (entry.getEffectiveDate() == null) {
effectiveTime = dateFormat.format(new Date());
} else {
effectiveTime = dateFormat.format(entry.getEffectiveDate());
}
TimeSpan timeSpan = KmlFactory.createTimeSpan();
timeSpan.setBegin(effectiveTime);
kmlPlacemark.setTimePrimitive(timeSpan);
kmlPlacemark.setGeometry(getKmlGeoFromWkt(entry.getLocation()));
String description = entry.getTitle();
Handlebars handlebars = new Handlebars(templateLoader);
handlebars.registerHelpers(templateHelper);
try {
Template template = handlebars.compile(DESCRIPTION_TEMPLATE);
description = template.apply(new HandlebarsMetacard(entry));
LOGGER.debug(description);
} catch (IOException e) {
LOGGER.debug("Failed to apply description Template", e);
}
kmlPlacemark.setDescription(description);
String styleUrl = styleMapper.getStyleForMetacard(entry);
if (StringUtils.isNotBlank(styleUrl)) {
kmlPlacemark.setStyleUrl(styleUrl);
}
return kmlPlacemark;
}
use of com.github.jknack.handlebars.Handlebars in project ddf by codice.
the class HandlebarsHelperTest method inlineTemplate.
private Template inlineTemplate() throws IOException {
Handlebars handlebars = new Handlebars();
HandlebarsHelper helper = new HandlebarsHelper();
handlebars.registerHelpers(helper);
return handlebars.compileInline("{{#ifeq String1 String2 }}True{{else}}False{{/ifeq}}");
}
use of com.github.jknack.handlebars.Handlebars in project ballerina by ballerina-lang.
the class CodeGenerator method compileTemplate.
private Template compileTemplate(String defaultTemplateDir, String templateName) throws IOException {
String templatesDirPath = System.getProperty(GeneratorConstants.TEMPLATES_DIR_PATH_KEY, defaultTemplateDir);
ClassPathTemplateLoader cpTemplateLoader = new ClassPathTemplateLoader((templatesDirPath));
FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(templatesDirPath);
cpTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
fileTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
Handlebars handlebars = new Handlebars().with(cpTemplateLoader, fileTemplateLoader);
handlebars.registerHelpers(StringHelpers.class);
handlebars.registerHelper("equals", (object, options) -> {
CharSequence result;
Object param0 = options.param(0);
if (param0 == null) {
throw new IllegalArgumentException("found 'null', expected 'string'");
}
if (object != null && object.toString().equals(param0.toString())) {
result = options.fn(options.context);
} else {
result = null;
}
return result;
});
return handlebars.compile(templateName);
}
use of com.github.jknack.handlebars.Handlebars in project ballerina by ballerina-lang.
the class ModelGenerator method generateSourceFiles.
public static void generateSourceFiles(JsonObject node, String tpl, String name) {
try {
String templateString = FileUtils.readFileToString(new File(TEMPLATE_PATH + tpl), "UTF-8");
Handlebars handlebars = new Handlebars();
Template template = handlebars.compileInline(templateString);
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Object>>() {
}.getType();
Map<String, Object> map = gson.fromJson(node, type);
Context context = Context.newBuilder(map).build();
String generated = template.apply(context);
FileUtils.writeStringToFile(new File(GENERATE_PATH + name), generated, "UTF-8");
} catch (IOException e) {
throw new AssertionError("Template files should be always defined.");
}
}
Aggregations