
org.jdbi.v3.freemarker.FreemarkerEngine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jdbi3-freemarker Show documentation
Show all versions of jdbi3-freemarker Show documentation
jdbi-freemarker locates SQL statements in Freemarker files on the classpath
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jdbi.v3.freemarker;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Optional;
import java.util.function.Function;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.jdbi.v3.core.config.ConfigRegistry;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.core.statement.TemplateEngine;
/**
* Rewrites a Freemarker template, using the attributes on the {@link StatementContext} as template parameters.
*/
public class FreemarkerEngine implements TemplateEngine.Parsing {
private static final FreemarkerEngine INSTANCE = new FreemarkerEngine();
/**
* @deprecated use {@link #instance()} for a shared engine instead
*/
@Deprecated
public FreemarkerEngine() {}
public static FreemarkerEngine instance() {
return INSTANCE;
}
@Override
public Optional> parse(String sqlTemplate, ConfigRegistry config) {
try {
Template template = new Template(null, sqlTemplate,
config.get(FreemarkerConfig.class).getFreemarkerConfiguration());
return Optional.of(ctx -> {
try {
StringWriter writer = new StringWriter();
template.process(ctx.getAttributes(), writer);
return writer.toString();
} catch (IOException | TemplateException e) {
throw new IllegalStateException("Failed to render template: " + sqlTemplate, e);
}
});
} catch (IOException e) {
throw new IllegalStateException("Failed to process template: " + sqlTemplate, e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy