com.github.mustachejava.resolver.ClasspathResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compiler Show documentation
Show all versions of compiler Show documentation
Implementation of mustache.js for Java
package com.github.mustachejava.resolver;
import com.github.mustachejava.MustacheResolver;
import com.google.common.base.Charsets;
import java.io.*;
/**
* MustacheResolver implementation that resolves
* mustache files from the classpath.
*/
public class ClasspathResolver implements MustacheResolver {
private final String resourceRoot;
public ClasspathResolver() {
this.resourceRoot = null;
}
/**
* Use the classpath to resolve mustache templates.
*
* @param resourceRoot
*/
public ClasspathResolver(String resourceRoot) {
if (!resourceRoot.endsWith("/")) {
resourceRoot += "/";
}
this.resourceRoot = resourceRoot;
}
@Override
public Reader getReader(String resourceName) {
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
String name = (resourceRoot == null ? "" : resourceRoot) + resourceName;
InputStream is = ccl.getResourceAsStream(name);
if (is == null) {
is = ClasspathResolver.class.getClassLoader().getResourceAsStream(name);
}
if (is != null) {
return new BufferedReader(new InputStreamReader(is, Charsets.UTF_8));
} else {
return null;
}
}
public String getResourceRoot() {
return resourceRoot;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy