Search in sources :

Example 1 with Handlebars

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);
    }
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) IOException(java.io.IOException)

Example 2 with Handlebars

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;
}
Also used : TimeSpan(de.micromata.opengis.kml.v_2_2_0.TimeSpan) Placemark(de.micromata.opengis.kml.v_2_2_0.Placemark) Handlebars(com.github.jknack.handlebars.Handlebars) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) LineString(com.vividsolutions.jts.geom.LineString) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Date(java.util.Date) Template(com.github.jknack.handlebars.Template)

Example 3 with Handlebars

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}}");
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars)

Example 4 with Handlebars

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);
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) FileTemplateLoader(com.github.jknack.handlebars.io.FileTemplateLoader)

Example 5 with Handlebars

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.");
    }
}
Also used : Context(com.github.jknack.handlebars.Context) Handlebars(com.github.jknack.handlebars.Handlebars) Gson(com.google.gson.Gson) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template) Type(java.lang.reflect.Type) JsonObject(com.google.gson.JsonObject) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Handlebars (com.github.jknack.handlebars.Handlebars)24 IOException (java.io.IOException)13 Template (com.github.jknack.handlebars.Template)10 ClassPathTemplateLoader (com.github.jknack.handlebars.io.ClassPathTemplateLoader)8 Context (com.github.jknack.handlebars.Context)6 FileTemplateLoader (com.github.jknack.handlebars.io.FileTemplateLoader)6 TemplateLoader (com.github.jknack.handlebars.io.TemplateLoader)3 Date (java.util.Date)3 UIContext (com.github.bordertech.wcomponents.UIContext)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 Provides (com.google.inject.Provides)2 Singleton (com.google.inject.Singleton)2 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)2 Placemark (de.micromata.opengis.kml.v_2_2_0.Placemark)2 TimeSpan (de.micromata.opengis.kml.v_2_2_0.TimeSpan)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 DateFormat (java.text.DateFormat)2