Search in sources :

Example 1 with Template

use of com.github.jknack.handlebars.Template 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.getRootContext() + FORWARD_SLASH + CATALOG_URL_PATH + FORWARD_SLASH + KML_TRANSFORM_PARAM + FORWARD_SLASH + "sources", 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)

Example 2 with Template

use of com.github.jknack.handlebars.Template 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 Template

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

the class RegistryReportBuilder method getHtmlFromRegistryPackage.

public String getHtmlFromRegistryPackage(RegistryPackageType registryPackage, String handlebarTemplate) throws IOException {
    Map<String, Object> reportMap = new HashMap<>();
    if (handlebarTemplate.equals(ORGANIZATIONS)) {
        reportMap = getOrganizationMap(registryPackage);
    } else if (handlebarTemplate.equals(REPORT)) {
        reportMap = getFullRegistryMap(registryPackage);
    }
    Template template = handlebars.compile(handlebarTemplate);
    String html = template.apply(reportMap);
    html = html.replaceAll("\n", "");
    return html;
}
Also used : HashMap(java.util.HashMap) Template(com.github.jknack.handlebars.Template)

Example 4 with Template

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

the class RegistryReportBuilder method getErrorHtml.

public String getErrorHtml(String errorMessage) throws IOException {
    Template template = handlebars.compile(ERROR);
    String html = template.apply(errorMessage);
    return html;
}
Also used : Template(com.github.jknack.handlebars.Template)

Example 5 with Template

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

the class LandingPage method compileTemplateWithProperties.

// package-private for unit testing
String compileTemplateWithProperties() {
    title = getTitle();
    version = branding.map(registry -> registry.getAttributeFromBranding(BrandingPlugin::getProductName)).orElse("");
    logoToUse = StringUtils.isNotEmpty(logo) ? logo : getProductImage();
    // FieldValueResolver so this class' fields can be accessed in the template.
    // MapValueResolver so we can access {{@index}} in the #each helper in the template.
    final Context context = Context.newBuilder(this).resolver(FieldValueResolver.INSTANCE, MapValueResolver.INSTANCE).build();
    // The template is "index.handlebars".
    final TemplateLoader templateLoader = new ClassPathTemplateLoader("/", ".handlebars");
    final Handlebars handlebars = new Handlebars(templateLoader);
    // extractDate(), extractAnnouncement(), expanded(), and in() are helper functions used in the template.
    handlebars.registerHelpers(this);
    String landingPageHtml;
    try {
        final Template template = handlebars.compile(LANDING_PAGE_FILE);
        landingPageHtml = template.apply(context);
    } catch (IOException e) {
        LOGGER.info("Unable to compile Landing Page template.", e);
        landingPageHtml = "<p>We are experiencing some issues. Please contact an administrator.</p>";
    }
    return landingPageHtml;
}
Also used : Context(com.github.jknack.handlebars.Context) Handlebars(com.github.jknack.handlebars.Handlebars) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) TemplateLoader(com.github.jknack.handlebars.io.TemplateLoader) ClassPathTemplateLoader(com.github.jknack.handlebars.io.ClassPathTemplateLoader) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template)

Aggregations

Template (com.github.jknack.handlebars.Template)7 Handlebars (com.github.jknack.handlebars.Handlebars)4 IOException (java.io.IOException)4 Context (com.github.jknack.handlebars.Context)2 Date (java.util.Date)2 ClassPathTemplateLoader (com.github.jknack.handlebars.io.ClassPathTemplateLoader)1 FileTemplateLoader (com.github.jknack.handlebars.io.FileTemplateLoader)1 TemplateLoader (com.github.jknack.handlebars.io.TemplateLoader)1 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)1 LineString (com.vividsolutions.jts.geom.LineString)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 Kml (de.micromata.opengis.kml.v_2_2_0.Kml)1 Link (de.micromata.opengis.kml.v_2_2_0.Link)1 NetworkLink (de.micromata.opengis.kml.v_2_2_0.NetworkLink)1 Placemark (de.micromata.opengis.kml.v_2_2_0.Placemark)1 TimeSpan (de.micromata.opengis.kml.v_2_2_0.TimeSpan)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1