com.github.jknack.handlebars.io.TemplateSource Java Examples

The following examples show how to use com.github.jknack.handlebars.io.TemplateSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IrisResourceLoader.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private TemplateSource createStringTemplateSource(String location) throws IOException {
	
	String uri = getPrefix() + location + getSuffix();
	LOGGER.info("Loading Resource {}",uri);
	
	Resource resource = Resources.getResource(uri);		
	String content = null;
	
	try (InputStream is = resource.open()) {
	    content = IOUtils.toString(is);
	}
	
	if(resource.isWatchable()){
		resource.addWatch(()->{resourceCache.invalidate(location);});
	}

	return new StringTemplateSource(location, content);
}
 
Example #2
Source File: IrisResourceLoader.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public TemplateSource sourceAt(String location) throws IOException {
	try {
		return resourceCache.get(location);
	} catch (ExecutionException e) {
		LOGGER.error("Error loading template from iris resource",e);
		throw new RuntimeException(e);
	}
}
 
Example #3
Source File: HandlebarsEngineAdapter.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public String compileTemplate(TemplatingExecutor executor,
                              Map<String, Object> bundle, String templateFile) throws IOException {
    TemplateLoader loader = new AbstractTemplateLoader() {
        @Override
        public TemplateSource sourceAt(String location) {
            return findTemplate(executor, location);
        }
    };

    Context context = Context
            .newBuilder(bundle)
            .resolver(
                    MapValueResolver.INSTANCE,
                    JavaBeanValueResolver.INSTANCE,
                    FieldValueResolver.INSTANCE)
            .build();

    Handlebars handlebars = new Handlebars(loader);
    handlebars.registerHelperMissing((obj, options) -> {
        LOGGER.warn(String.format(Locale.ROOT, "Unregistered helper name '%s', processing template:\n%s", options.helperName, options.fn.text()));
        return "";
    });
    handlebars.registerHelper("json", Jackson2Helper.INSTANCE);
    StringHelpers.register(handlebars);
    handlebars.registerHelpers(ConditionalHelpers.class);
    handlebars.registerHelpers(org.openapitools.codegen.templating.handlebars.StringHelpers.class);
    Template tmpl = handlebars.compile(templateFile);
    return tmpl.apply(context);
}
 
Example #4
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public Template get(final TemplateSource source, final Parser parser) throws IOException {
    notNull(source, "The source is required.");
    notNull(parser, "The parser is required.");

    /**
     * Don't keep duplicated entries, remove old one if a change is detected.
     */
    return cacheGet(source, parser);
}
 
Example #5
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Get/Parse a template source.
 *
 * @param source The template source.
 * @param parser The parser.
 * @return A Handlebars template.
 * @throws IOException If we can't read input.
 */
private Template cacheGet(final TemplateSource source, final Parser parser) throws IOException {
    Pair<TemplateSource, Template> entry = cache.get(source);
    if (entry == null) {
        log.debug("Loading: {}", source);
        entry = Pair.of(source, parser.parse(source));
        cache.put(source, entry);
    } else {
        log.debug("Found in cache: {}", source);
    }
    return entry.getValue();
}
 
Example #6
Source File: HandlebarsTemplateEngine.java    From spark-template-engines with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example #7
Source File: HandlebarsTemplateEngineImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public TemplateSource sourceAt(String location) {
  final String loc = resolve(location);
  final Buffer templ;

  if (vertx.fileSystem().existsBlocking(loc)) {
    templ = vertx.fileSystem()
      .readFileBlocking(loc);
  } else {
    templ = null;
  }

  if (templ == null) {
    throw new IllegalArgumentException("Cannot find resource " + loc);
  }

  long lastMod = System.currentTimeMillis();

  return new TemplateSource() {
    @Override
    public String content(Charset charset) {
      return templ.toString(charset);
    }

    @Override
    public String filename() {
      return loc;
    }

    @Override
    public long lastModified() {
      return lastMod;
    }
  };
}
 
Example #8
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public Template get(final TemplateSource source, final Parser parser) throws IOException {
    notNull(source, "The source is required.");
    notNull(parser, "The parser is required.");

    /**
     * Don't keep duplicated entries, remove old one if a change is detected.
     */
    return cacheGet(source, parser);
}
 
Example #9
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Get/Parse a template source.
 *
 * @param source The template source.
 * @param parser The parser.
 * @return A Handlebars template.
 * @throws IOException If we can't read input.
 */
private Template cacheGet(final TemplateSource source, final Parser parser) throws IOException {
    Pair<TemplateSource, Template> entry = cache.get(source);
    if (entry == null) {
        log.debug("Loading: {}", source);
        entry = Pair.of(source, parser.parse(source));
        cache.put(source, entry);
    } else {
        log.debug("Found in cache: {}", source);
    }
    return entry.getValue();
}
 
Example #10
Source File: IrisResourceLoader.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public TemplateSource load(String location) throws IOException {
  return createStringTemplateSource(location);
}
 
Example #11
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * Creates a new ConcurrentMapTemplateCache.
 */
public ForeverTemplateCache() {
    this(new ConcurrentHashMap<TemplateSource, Pair<TemplateSource, Template>>());
}
 
Example #12
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@Override
public void evict(final TemplateSource source) {
    cache.remove(source);
}
 
Example #13
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * Creates a new ConcurrentMapTemplateCache.
 */
public ForeverTemplateCache() {
    this(new ConcurrentHashMap<TemplateSource, Pair<TemplateSource, Template>>());
}
 
Example #14
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@Override
public void evict(final TemplateSource source) {
    cache.remove(source);
}
 
Example #15
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Creates a new ConcurrentMapTemplateCache.
 *
 * @param cache The concurrent map cache. Required.
 */
protected ForeverTemplateCache(
        final ConcurrentMap<TemplateSource, Pair<TemplateSource, Template>> cache) {
    this.cache = notNull(cache, "The cache is required.");
}
 
Example #16
Source File: ForeverTemplateCache.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Creates a new ConcurrentMapTemplateCache.
 *
 * @param cache The concurrent map cache. Required.
 */
protected ForeverTemplateCache(
        final ConcurrentMap<TemplateSource, Pair<TemplateSource, Template>> cache) {
    this.cache = notNull(cache, "The cache is required.");
}