Search in sources :

Example 21 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) 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;
    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(getKmlGeoWithPointsFromWkt(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);
    setExtendedData(kmlPlacemark, entry);
    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) 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 22 with Handlebars

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

the class Writer method writeHtmlDocument.

/**
 * Write the HTML document from the Page object for a bal package.
 * @param object Page object which is generated from the bal package.
 * @param packageTemplateName hbs template file to be used.
 * @param filePath path of the file to write the output.
 */
public static void writeHtmlDocument(Object object, String packageTemplateName, String filePath) {
    String templatesFolderPath = System.getProperty(BallerinaDocConstants.TEMPLATES_FOLDER_PATH_KEY, File.separator + "docerina-templates" + File.separator + "html");
    PrintWriter writer = null;
    try {
        Handlebars handlebars = new Handlebars().with(new ClassPathTemplateLoader(templatesFolderPath), new FileTemplateLoader(templatesFolderPath));
        handlebars.registerHelpers(StringHelpers.class);
        Template template = handlebars.compile(packageTemplateName);
        writer = new PrintWriter(filePath, "UTF-8");
        Context context = Context.newBuilder(object).resolver(FieldValueResolver.INSTANCE).build();
        writer.println(template.apply(context));
        out.println("HTML file written: " + filePath);
    } catch (IOException e) {
        out.println("docerina: could not write HTML file " + filePath + System.lineSeparator() + e.getMessage());
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : Context(com.github.jknack.handlebars.Context) Handlebars(com.github.jknack.handlebars.Handlebars) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) IOException(java.io.IOException) FileTemplateLoader(com.github.jknack.handlebars.io.FileTemplateLoader) PrintWriter(java.io.PrintWriter) Template(com.github.jknack.handlebars.Template)

Example 23 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project fess by codelibs.

the class ViewHelper method createCacheContent.

public String createCacheContent(final Map<String, Object> doc, final String[] queries) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final FileTemplateLoader loader = new FileTemplateLoader(ResourceUtil.getViewTemplatePath().toFile());
    final Handlebars handlebars = new Handlebars(loader);
    Locale locale = ComponentUtil.getRequestManager().getUserLocale();
    if (locale == null) {
        locale = Locale.ENGLISH;
    }
    String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
    if (url == null) {
        url = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
    }
    doc.put(fessConfig.getResponseFieldUrlLink(), getUrlLink(doc));
    String createdStr;
    final Date created = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCreated(), Date.class);
    if (created != null) {
        final SimpleDateFormat sdf = new SimpleDateFormat(CoreLibConstants.DATE_FORMAT_ISO_8601_EXTEND);
        createdStr = sdf.format(created);
    } else {
        createdStr = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
    }
    doc.put(CACHE_MSG, ComponentUtil.getMessageManager().getMessage(locale, "labels.search_cache_msg", url, createdStr));
    doc.put(QUERIES, queries);
    String cache = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCache(), String.class);
    if (cache != null) {
        final String mimetype = DocumentUtil.getValue(doc, fessConfig.getIndexFieldMimetype(), String.class);
        if (!ComponentUtil.getFessConfig().isHtmlMimetypeForCache(mimetype)) {
            cache = StringEscapeUtils.escapeHtml4(cache);
        }
        cache = ComponentUtil.getPathMappingHelper().replaceUrls(cache);
        if (queries != null && queries.length > 0) {
            doc.put(HL_CACHE, replaceHighlightQueries(cache, queries));
        } else {
            doc.put(HL_CACHE, cache);
        }
    } else {
        doc.put(fessConfig.getIndexFieldCache(), StringUtil.EMPTY);
        doc.put(HL_CACHE, StringUtil.EMPTY);
    }
    try {
        final Template template = handlebars.compile(cacheTemplateName);
        final Context hbsContext = Context.newContext(doc);
        return template.apply(hbsContext);
    } catch (final Exception e) {
        logger.warn("Failed to create a cache response.", e);
    }
    return null;
}
Also used : Locale(java.util.Locale) Context(com.github.jknack.handlebars.Context) ServletContext(javax.servlet.ServletContext) Handlebars(com.github.jknack.handlebars.Handlebars) FileTemplateLoader(com.github.jknack.handlebars.io.FileTemplateLoader) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) SimpleDateFormat(com.ibm.icu.text.SimpleDateFormat) Date(java.util.Date) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FessSystemException(org.codelibs.fess.exception.FessSystemException) ClientAbortException(org.apache.catalina.connector.ClientAbortException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Template(com.github.jknack.handlebars.Template)

Example 24 with Handlebars

use of com.github.jknack.handlebars.Handlebars in project knotx by Cognifide.

the class HandlebarsKnotProxyImpl method createHandlebars.

private Handlebars createHandlebars() {
    Handlebars newHandlebars = new Handlebars();
    DefaultHandlebarsHelpers.registerFor(newHandlebars);
    ServiceLoader.load(CustomHandlebarsHelper.class).iterator().forEachRemaining(helper -> {
        newHandlebars.registerHelper(helper.getName(), helper);
        LOGGER.info("Registered custom Handlebars helper: {}", helper.getName());
    });
    return newHandlebars;
}
Also used : Handlebars(com.github.jknack.handlebars.Handlebars)

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