本文整理汇总了Java中freemarker.cache.TemplateLoader类的典型用法代码示例。如果您正苦于以下问题:Java TemplateLoader类的具体用法?Java TemplateLoader怎么用?Java TemplateLoader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TemplateLoader类属于freemarker.cache包,在下文中一共展示了TemplateLoader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildFreemarkerHelper
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
private FreemarkerHelper buildFreemarkerHelper(File templateBaseDir) {
Configuration configuration = new Configuration(new Version(2, 3, 0));
try {
TemplateLoader templateLoader = new FileTemplateLoader(templateBaseDir);
configuration.setTemplateLoader(templateLoader);
} catch (IOException e) {
throw new GeneratorException("构建模板助手出错:" + e.getMessage());
}
configuration.setNumberFormat("###############");
configuration.setBooleanFormat("true,false");
configuration.setDefaultEncoding("UTF-8");
// 自动导入公共文件,用于支持灵活变量
if (autoIncludeFile.exists()) {
List<String> autoIncludeList = new ArrayList<>();
autoIncludeList.add(FREEMARKER_AUTO_INCLUDE_SUFFIX);
configuration.setAutoIncludes(autoIncludeList);
}
return new FreemarkerHelper(configuration);
}
示例2: FreemarkerTemplateContext
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
public FreemarkerTemplateContext(File path)
{
config = new Configuration(VERSION);
config.setDefaultEncoding("UTF-8");
config.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
try
{
//Web site location resources
FileTemplateLoader fileLoader = new FileTemplateLoader(path);
//Core common resources
ClassTemplateLoader classLoader = new ClassTemplateLoader(WebSite.class, "/");
config.setTemplateLoader(new MultiTemplateLoader(new TemplateLoader[]
{
fileLoader, classLoader
}));
}
catch (IOException ex)
{
LOG.log(Level.SEVERE, ex.getMessage(), ex);
}
}
示例3: freemarkerConfig
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
@Bean
public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
final FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
// If overwritten use path of user
if (isNotBlank(uaaProperties.getTemplatePath())) {
final TemplateLoader templateLoader = getTemplateLoader(uaaProperties.getTemplatePath());
factory.setPreTemplateLoaders(templateLoader);
}
// Default configurations
factory.setPostTemplateLoaders(getTemplateLoader("classpath:/templates/"));
factory.setDefaultEncoding("UTF-8");
final FreeMarkerConfigurer result = new FreeMarkerConfigurer();
result.setConfiguration(factory.createConfiguration());
return result;
}
示例4: init
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
public void init() throws IOException {
ClassTemplateLoader ctl = new ClassTemplateLoader(Application.class, "/freemarker");
MultiTemplateLoader mtl = new MultiTemplateLoader(new TemplateLoader[] {ctl});
Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
cfg.setTemplateLoader(mtl);
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
Pair<String, Template> clusterResourceQuota = new ImmutablePair<String, Template>("ClusterResourceQuota-ForUser", cfg.getTemplate("ClusterResourceQuota-ForUser.ftlh"));
Pair<String, Template> bestEffortResourceLimits = new ImmutablePair<String, Template>("LimitRange-BestEffortResourceLimits", cfg.getTemplate("LimitRange-BestEffortResourceLimits.ftlh"));
Pair<String, Template> burstableResourceLimits = new ImmutablePair<String, Template>("LimitRange-BurstableResourceLimits", cfg.getTemplate("LimitRange-BurstableResourceLimits.ftlh"));
Pair<String, Template> maxImageCounts = new ImmutablePair<String, Template>("LimitRange-MaxImageCounts", cfg.getTemplate("LimitRange-MaxImageCounts.ftlh"));
Pair<String, Template> bestEffort = new ImmutablePair<String, Template>("ResourceQuota-BestEffort", cfg.getTemplate("ResourceQuota-BestEffort.ftlh"));
Pair<String, Template> notTerminatingAndNotBestEffort = new ImmutablePair<String, Template>("ResourceQuota-NotTerminating-And-NotBestEffort",
cfg.getTemplate("ResourceQuota-NotTerminating-And-NotBestEffort.ftlh"));
Pair<String, Template> terminating = new ImmutablePair<String, Template>("ResourceQuota-Terminating", cfg.getTemplate("ResourceQuota-Terminating.ftlh"));
templates = Arrays.asList(clusterResourceQuota, bestEffortResourceLimits, burstableResourceLimits, maxImageCounts, bestEffort, notTerminatingAndNotBestEffort, terminating);
}
示例5: __processTemplate
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
protected static String __processTemplate(TemplateLoader templateLoader, String templateName,
Map<String, ?> parameterValues) {
Map<String, Object> params = prepareParams(parameterValues);
final StringWriter writer = new StringWriter();
try {
final Configuration configuration = new Configuration();
configuration.setTemplateLoader(templateLoader);
final Template template = configuration.getTemplate(templateName);
template.process(params, writer);
return writer.toString();
} catch (Throwable e) {
throw new RuntimeException("Unable to process template", e);
}
}
示例6: getFreemarkerConfiguration
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
/**
* Creates freemarker configuration settings,
* default output format to trigger auto-escaping policy
* and template loaders.
*
* @param servletContext servlet context
* @return freemarker configuration settings
*/
private Configuration getFreemarkerConfiguration(ServletContext servletContext) {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);
List<TemplateLoader> loaders = new ArrayList<>();
loaders.add(new WebappTemplateLoader(servletContext));
loaders.add(new ClassTemplateLoader(DrillRestServer.class, "/"));
try {
loaders.add(new FileTemplateLoader(new File("/")));
} catch (IOException e) {
logger.error("Could not set up file template loader.", e);
}
configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
return configuration;
}
示例7: createLoaders
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
static TemplateLoader[] createLoaders(String[] locations, ServletContext servletContext) {
Collection<TemplateLoader> templateLoaders = new ArrayList<TemplateLoader>();
for (String location : locations) {
String[] prefixAndBase = StringUtils.split(location, ":", 2);
if (prefixAndBase.length != 2) {
throw new OpenGammaRuntimeException("Invalid Freemarker template location: " + location);
}
String prefix = prefixAndBase[0].trim();
String base = prefixAndBase[1].trim();
if (SERVLET_CONTEXT.equals(prefix)) {
templateLoaders.add(new WebappTemplateLoader(servletContext, base));
} else if (FILE.equals(prefix)) {
try {
templateLoaders.add(new FileTemplateLoader(new File(base)));
} catch (IOException e) {
throw new OpenGammaRuntimeException("Unable to load Freemarker templates from " + base, e);
}
} else {
throw new OpenGammaRuntimeException("Invalid Freemarker template location: " + location);
}
}
return templateLoaders.toArray(new TemplateLoader[templateLoaders.size()]);
}
示例8: __doViewInit
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
@Override
protected void __doViewInit(IWebMvc owner) {
super.__doViewInit(owner);
// 初始化Freemarker模板引擎配置
if (__freemarkerConfig == null) {
__freemarkerConfig = new Configuration(Configuration.VERSION_2_3_22);
__freemarkerConfig.setDefaultEncoding(owner.getModuleCfg().getDefaultCharsetEncoding());
__freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
//
List<TemplateLoader> _tmpLoaders = new ArrayList<TemplateLoader>();
try {
if (__baseViewPath.startsWith("/WEB-INF")) {
_tmpLoaders.add(new FileTemplateLoader(new File(RuntimeUtils.getRootPath(), StringUtils.substringAfter(__baseViewPath, "/WEB-INF/"))));
} else {
_tmpLoaders.add(new FileTemplateLoader(new File(__baseViewPath)));
}
//
__freemarkerConfig.setTemplateLoader(new MultiTemplateLoader(_tmpLoaders.toArray(new TemplateLoader[_tmpLoaders.size()])));
} catch (IOException e) {
throw new Error(RuntimeUtils.unwrapThrow(e));
}
}
}
示例9: createFreeMarkerConfiguration
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
/**
* Creates the {@link freemarker.template.Configuration} instance
* and sets it up. If you want to change it (set another props, for
* example), you can override it in inherited class and use your own
* class in @Lang directive.
*/
protected freemarker.template.Configuration createFreeMarkerConfiguration() {
freemarker.template.Configuration cfg = new freemarker.template.Configuration(
freemarker.template.Configuration.VERSION_2_3_22);
TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass().getClassLoader(), basePackage);
cfg.setTemplateLoader(templateLoader);
// To avoid formatting numbers using spaces and commas in SQL
cfg.setNumberFormat("computer");
// Because it defaults to default system encoding, we should set it always explicitly
cfg.setDefaultEncoding(StandardCharsets.UTF_8.name());
return cfg;
}
示例10: getAggregateTemplateLoader
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
/**
* Return a TemplateLoader based on the given TemplateLoader list.
* If more than one TemplateLoader has been registered, a FreeMarker
* MultiTemplateLoader needs to be created.
*
* @param templateLoaders the final List of TemplateLoader instances
* @return the aggregate TemplateLoader
*/
protected TemplateLoader getAggregateTemplateLoader(TemplateLoader[] templateLoaders) {
int loaderCount = (templateLoaders != null ? templateLoaders.length : 0);
switch (loaderCount) {
case 0:
if (log.isDebugEnabled()) {
log.debug("No FreeMarker TemplateLoaders specified. Can be used only inner template source");
}
return null;
case 1:
if (log.isDebugEnabled()) {
log.debug("One FreeMarker TemplateLoader registered: " + templateLoaders[0]);
}
return templateLoaders[0];
default:
TemplateLoader loader = new MultiTemplateLoader(templateLoaders);
if (log.isDebugEnabled()) {
log.debug("Multiple FreeMarker TemplateLoader registered: " + loader);
}
return loader;
}
}
示例11: getTemplateLoaderForPath
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
public static TemplateLoader getTemplateLoaderForPath(ResourceLoader resourceLoader, String templateLoaderPath) {
try {
Resource path = resourceLoader.getResource(templateLoaderPath);
File file = path.getFile(); // will fail if not resolvable in the file system
if (logger.isDebugEnabled()) {
logger.debug(
"Template loader path [" + path + "] resolved to file path [" + file.getAbsolutePath() + "]");
}
return new FileTemplateLoader(file);
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Cannot resolve template loader path [" + templateLoaderPath +
"] to [java.io.File]: using SpringTemplateLoader as fallback", ex);
}
return new SpringTemplateLoader(resourceLoader, templateLoaderPath);
}
}
示例12: main
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
public static void main(String[] args){
PluginTemplateLoader j = new PluginTemplateLoader(new TemplateLoader[]{}, null);
System.out.println(j.pluginNameParser.getPluginName("lib/test/test.ftl"));
String path = "[codegen]/lib/test/test.ftl";
String pname = j.pluginNameParser.getPluginName(path);
System.out.println(pname);
System.out.println(path.substring(pname.length()+j.pluginNameParser.getLength()));
path = "[codegen]/test.ftl";
pname = j.pluginNameParser.getPluginName(path);
System.out.println(pname);
System.out.println(path.substring(pname.length()+j.pluginNameParser.getLength()));
path = "[[codegen-test";
pname = j.pluginNameParser.getPluginName(path);
System.out.println("[["+pname);
System.out.println("[["+path.substring(pname.length()+j.pluginNameParser.getLength()));
}
示例13: lookup
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
@Override
public TemplateLookupResult lookup(TemplateLookupContext ctx) throws IOException {
try {
ResourceResolver matchingResolver = resolverRegistry.getSupportingResolver(ctx.getTemplateName());
// no match, delegate to let delegate decide
if (matchingResolver == null) {
return delegate.lookup(ctx);
}
TemplateLoader matchingAdapter = adapter.adapt(matchingResolver);
// if it is a template content (directly a string)
// => skip locale resolution
if (matchingAdapter instanceof StringContentTemplateLoader) {
return ctx.lookupWithLocalizedThenAcquisitionStrategy(ctx.getTemplateName(), null);
}
// standard Freemarker behavior
return delegate.lookup(ctx);
} catch (ResolverAdapterException e) {
LOG.debug("Failed to determine which Freemarker adapter to use for template name " + ctx.getTemplateName(), e);
return delegate.lookup(ctx);
}
}
示例14: FlexibleConfiguration
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
@Inject
public FlexibleConfiguration(final javax.ws.rs.core.Configuration config, @Optional final ServletContext servletContext) {
super();
final List<TemplateLoader> loaders = new ArrayList<>();
if (servletContext != null) {
loaders.add(new WebappTemplateLoader(servletContext));
}
loaders.add(new ClassTemplateLoader(FlexibleConfiguration.class, "/"));
// Create Factory.
this.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
try {
settingConfiguration(config);
} catch (TemplateException e) {
throw new RuntimeException(e);
}
}
示例15: FreeMarkerFormatter
import freemarker.cache.TemplateLoader; //导入依赖的package包/类
public FreeMarkerFormatter(String templateName) throws IOException {
// If the resource doesn't exist abort so we can look elsewhere
try (
InputStream in = this.getClass().getResourceAsStream(TEMPLATES + "/" + templateName)) {
if (in == null) {
throw new IOException("Resource not found:" + templateName);
}
}
this.templateName = templateName;
Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), TEMPLATES);
cfg.setTemplateLoader(templateLoader);
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
// This is fatal - bomb out of application
try {
template = cfg.getTemplate(templateName);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}