dev.jbang.util.TemplateEngine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbang-cli Show documentation
Show all versions of jbang-cli Show documentation
JBang Command Line Interface
package dev.jbang.util;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import dev.jbang.source.ResourceRef;
import io.quarkus.qute.Engine;
import io.quarkus.qute.ReflectionValueResolver;
import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateLocator;
import io.quarkus.qute.Variant;
public class TemplateEngine {
final Engine engine;
static TemplateEngine instance;
TemplateEngine() {
engine = Engine .builder()
.addDefaults()
.removeStandaloneLines(true)
.addValueResolver(new ReflectionValueResolver())
// .addResultMapper(new PropertyNotFoundThrowException())
.addLocator(this::locate)
.build();
}
/**
* @param ref
* @return the optional reader
*/
private Optional locate(String ref) {
return Optional.of(new ResourceRefTemplateLocation(ResourceRef.forResource(ref)));
}
public Template getTemplate(ResourceRef templateRef) {
return engine.getTemplate(templateRef.getOriginalResource());
}
static class ResourceRefTemplateLocation implements TemplateLocator.TemplateLocation {
private final ResourceRef resourceRef;
private Optional variant = Optional.empty();
public ResourceRefTemplateLocation(ResourceRef resourceRef) {
this.resourceRef = resourceRef;
this.variant = Optional.empty();
}
@Override
public Reader read() {
try {
return new InputStreamReader(resourceRef.getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) {
return null;
}
}
@Override
public Optional getVariant() {
return variant;
}
}
public static TemplateEngine instance() {
if (instance == null) {
instance = new TemplateEngine();
}
return instance;
}
}