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

The following examples show how to use com.github.jknack.handlebars.io.StringTemplateSource. 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: TemplateWatcher.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Compiles the given template file and create the associated template entry.
 * <p>IO and compile errors are logged but not rethrown.</p>
 *
 * @param templateFile the template file to compile
 * @return the created template entry, or {@code null} if any problem occurred
 */
public TemplateEntry compileTemplate( final File templateFile ) {

	TemplateEntry templateEntry = null;
	try {
		// Parse the template's content and find the (optional) output
		String templateFileContent = Utils.readFileContent( templateFile );
		Matcher m = Pattern.compile( "\\{\\{!\\s*roboconf-output:(.*)\\}\\}" ).matcher( templateFileContent.trim());

		String targetFilePath = null;
		if( m.find())
			targetFilePath = m.group( 1 ).trim();

		// Compile the template file
		final Template template = this.handlebars.compile(
				new StringTemplateSource(
						templateFile.toString(),
						templateFileContent ));

		// Create the entry
		templateEntry = new TemplateEntry(
				templateFile, targetFilePath, template,
				TemplateUtils.findApplicationName( this.templateDir, templateFile ));

	} catch( IOException | IllegalArgumentException | HandlebarsException e ) {
		this.logger.warning("Cannot compile template " + templateFile);
		Utils.logException(this.logger, e);
	}

	return templateEntry;
}