Search in sources :

Example 6 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project acs-aem-commons by Adobe-Consulting-Services.

the class QueryReportExecutor method prepareStatement.

private void prepareStatement() throws ReportException {
    try {
        Map<String, String> parameters = new HashMap<String, String>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String key = paramNames.nextElement();
            parameters.put(key, StringEscapeUtils.escapeSql(request.getParameter(key)));
        }
        log.trace("Loading parameters from request: {}", parameters);
        Handlebars handlebars = new Handlebars();
        Template template = handlebars.compileInline(config.getQuery());
        statement = template.apply(parameters);
        log.trace("Loaded statement: {}", statement);
    } catch (IOException ioe) {
        throw new ReportException("Exception templating query", ioe);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Handlebars(com.github.jknack.handlebars.Handlebars) ReportException(com.adobe.acs.commons.reports.api.ReportException) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template)

Example 7 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project cloudbreak by hortonworks.

the class HandlebarUtils method handlebars.

public static Handlebars handlebars() {
    Handlebars handlebars = new Handlebars();
    handlebars.with(value -> StringEscapeUtils.escapeJava(value.toString()));
    handlebars.registerHelper(EqHelper.NAME, EqHelper.INSTANCE);
    handlebars.registerHelper(NeqHelper.NAME, NeqHelper.INSTANCE);
    handlebars.registerHelperMissing((context, options) -> options.fn.text());
    return handlebars;
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars)

Example 8 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project ballerina by ballerina-lang.

the class CodeGenerator method compileTemplate.

/**
 * This method will compile and return template of passed template definition.
 *
 * @param defaultTemplateDir template directory which contains set of templates
 * @param templateName       template file name to be used as template
 * @return compiled template generated for template definition.
 * @throws CodeGeneratorException throws IOException when compilation error occurs.
 */
private Template compileTemplate(String defaultTemplateDir, String templateName) throws CodeGeneratorException {
    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;
    });
    try {
        return handlebars.compile(templateName);
    } catch (IOException e) {
        throw new CodeGeneratorException("Error while compiling template", e);
    }
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) CodeGeneratorException(org.ballerinalang.code.generator.exception.CodeGeneratorException) IOException(java.io.IOException) FileTemplateLoader(com.github.jknack.handlebars.io.FileTemplateLoader)

Example 9 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project ballerina by ballerina-lang.

the class BalGenerationUtils method compileTemplate.

private static Template compileTemplate(String defaultTemplateDir, String templateName) throws IOException {
    String templatesDirPath = System.getProperty(TEMPLATES_DIR_PATH_KEY, defaultTemplateDir);
    ClassPathTemplateLoader cpTemplateLoader = new ClassPathTemplateLoader((templatesDirPath));
    FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(templatesDirPath);
    cpTemplateLoader.setSuffix(TEMPLATES_SUFFIX);
    fileTemplateLoader.setSuffix(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 n'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 10 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project ddf by codice.

the class KmlEndpoint method createRootNetworkLink.

private Kml createRootNetworkLink(UriInfo uriInfo) throws UnknownHostException {
    Kml kml = KmlFactory.createKml();
    NetworkLink rootNetworkLink = kml.createAndSetNetworkLink();
    rootNetworkLink.setName(this.productName);
    rootNetworkLink.setSnippet(KmlFactory.createSnippet().withMaxLines(0));
    UriBuilder baseUrlBuidler = UriBuilder.fromUri(uriInfo.getBaseUri());
    baseUrlBuidler.replacePath("");
    this.baseUrl = baseUrlBuidler.build().toString();
    String descriptionHtml = description;
    Handlebars handlebars = new Handlebars(templateLoader);
    try {
        Template template = handlebars.compile("description");
        descriptionHtml = template.apply(this);
        LOGGER.debug(descriptionHtml);
    } catch (IOException e) {
        LOGGER.debug("Failed to apply description Template", e);
    }
    rootNetworkLink.setDescription(descriptionHtml);
    rootNetworkLink.setOpen(true);
    rootNetworkLink.setVisibility(false);
    Link link = rootNetworkLink.createAndSetLink();
    UriBuilder builder = UriBuilder.fromUri(uriInfo.getBaseUri());
    builder = generateEndpointUrl(SystemBaseUrl.EXTERNAL.constructUrl(FORWARD_SLASH + CATALOG_URL_PATH + FORWARD_SLASH + KML_TRANSFORM_PARAM + FORWARD_SLASH + "sources", true), builder);
    link.setHref(builder.build().toString());
    link.setViewRefreshMode(ViewRefreshMode.NEVER);
    link.setRefreshMode(RefreshMode.ON_INTERVAL);
    link.setRefreshInterval(REFRESH_INTERVAL);
    return kml;
}
Also used : NetworkLink(de.micromata.opengis.kml.v_2_2_0.NetworkLink) Handlebars(com.github.jknack.handlebars.Handlebars) Kml(de.micromata.opengis.kml.v_2_2_0.Kml) IOException(java.io.IOException) UriBuilder(javax.ws.rs.core.UriBuilder) Link(de.micromata.opengis.kml.v_2_2_0.Link) NetworkLink(de.micromata.opengis.kml.v_2_2_0.NetworkLink) Template(com.github.jknack.handlebars.Template)

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