@Override
  public void execute() throws MojoExecutionException, MojoFailureException {
    File basedir = new File(prefix);
    TemplateLoader loader = new FileTemplateLoader(basedir, suffix);
    Handlebars handlebars = new Handlebars(loader);
    File output = new File(this.output);
    PrintWriter writer = null;
    boolean error = true;
    InputStream runtimeIS = getClass().getResourceAsStream("/handlebars.runtime.js");
    try {
      writer = new PrintWriter(output);
      if (includeRuntime) {
        IOUtil.copy(runtimeIS, writer);
      }
      List<File> files = FileUtils.getFiles(basedir, "/**/*" + suffix, null);
      getLog().info("Compiling templates...");
      getLog().debug("Options:");
      getLog().debug("  output: " + output);
      getLog().debug("  prefix: " + prefix);
      getLog().debug("  suffix: " + suffix);
      getLog().debug("  minimize: " + minimize);
      getLog().debug("  includeRuntime: " + includeRuntime);

      Context nullContext = Context.newContext(null);
      for (File file : files) {
        String templateName = file.getPath().replace(prefix, "").replace(suffix, "");
        if (templateName.startsWith("/")) {
          templateName = templateName.substring(1);
        }
        getLog().debug("compiling: " + templateName);
        Template template = handlebars.compileInline("{{precompile \"" + templateName + "\"}}");
        Options opts = new Options.Builder(handlebars, TagType.VAR, nullContext, template).build();

        writer.append("// Source: ").append(file.getPath()).append("\n");
        writer.append(PrecompileHelper.INSTANCE.apply(templateName, opts)).append("\n\n");
      }
      writer.flush();
      IOUtil.close(writer);
      if (minimize) {
        minimize(output);
      }
      if (files.size() > 0) {
        getLog().info("  templates were saved in: " + output);
        error = false;
      } else {
        getLog().warn("  no templates were found");
      }
    } catch (IOException ex) {
      throw new MojoFailureException("Can't scan directory " + basedir, ex);
    } finally {
      IOUtil.close(runtimeIS);
      IOUtil.close(writer);
      if (error) {
        output.delete();
      }
    }
  }
 @Override
 public String render(ModelAndView modelAndView) {
   String viewName = modelAndView.getViewName();
   try {
     Template template = handlebars.compile(viewName);
     return template.apply(modelAndView.getModel());
   } catch (IOException e) {
     throw new RuntimeIOException(e);
   }
 }
Esempio n. 3
0
 static {
   try {
     handlebars.registerHelper(
         "compare",
         new Helper<Object>() {
           @Override
           public CharSequence apply(Object context, Options options) throws IOException {
             if (options.params[0].equals(context == "last")) {
               return options.fn();
             } else {
               return options.inverse();
             }
           }
         });
     template = handlebars.compile("/com/fantasy/system/job/dicts");
   } catch (IOException e) {
     LOGGER.error(e.getMessage(), e);
   }
 }
  @Override
  public String process(String templateId, Object data) {
    String renderedTemplate;
    try {
      Template template = handlebars.compile(templateId);
      renderedTemplate = template.apply(data);
    } catch (IOException e) {
      throw new MailException();
    }

    return renderedTemplate;
  }
Esempio n. 5
0
  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.error("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(
            servicesContextRoot
                + 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;
  }
  /**
   * Constructs a handlebars template engine
   *
   * @param resourceRoot the resource root
   */
  public HandlebarsTemplateEngine(String resourceRoot) {
    TemplateLoader templateLoader = new ClassPathTemplateLoader();
    templateLoader.setPrefix(resourceRoot);
    templateLoader.setSuffix(null);

    handlebars = new Handlebars(templateLoader);

    // Set Guava cache.
    Cache<TemplateSource, Template> cache =
        CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(1000).build();

    handlebars = handlebars.with(new GuavaTemplateCache(cache));
  }
 @Override
 protected void configure(final Handlebars handlebars) {
   handlebars.registerHelper("dateFormat", StringHelpers.dateFormat);
 }
Esempio n. 8
0
 @Override
 protected void configure(final Handlebars handlebars) {
   handlebars.prettyPrint(true);
 }
Esempio n. 9
0
 public String evalToString(String templateContent) throws Exception {
   Template template = handlebars.compileInline(templateContent);
   return template.apply(context);
 }