htmlcompiler.compilers.templates.Thymeleaf Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of htmlcompiler Show documentation
Show all versions of htmlcompiler Show documentation
An HTML compiler, with maven plugin
package htmlcompiler.compilers.templates;
import htmlcompiler.pojos.error.InvalidTemplate;
import org.apache.maven.project.MavenProject;
import org.thymeleaf.context.IContext;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Thymeleaf implements HtmlTemplateEngine {
private org.thymeleaf.TemplateEngine engine;
private IContext context;
public Thymeleaf(final MavenProject project) {
this.engine = new org.thymeleaf.TemplateEngine();
final Map map = applyMavenProjectContext(applyEnvironmentContext(new HashMap<>()), project);
this.context = new IContext() {
public Locale getLocale() {
return Locale.getDefault();
}
public boolean containsVariable(String s) {
return map.containsKey(s);
}
public Set getVariableNames() {
return map.keySet();
}
public Object getVariable(String s) {
return map.get(s);
}
};
}
@Override
public String compile(File file) throws IOException, InvalidTemplate {
engine.process(Files.readString(file.toPath()), context);
return null;
}
private static Map applyMavenProjectContext(final Map context, final MavenProject project) {
for (final Entry