freemarker.cache.MultiTemplateLoader Java Examples
The following examples show how to use
freemarker.cache.MultiTemplateLoader.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DrillRestServer.java From Bats with Apache License 2.0 | 7 votes |
/** * 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; }
Example #2
Source File: FreemarkerTemplateEngine.java From enkan with Eclipse Public License 1.0 | 6 votes |
private TemplateLoader createTemplateLoader() { TemplateLoader classTemplateLoader = new ClassTemplateLoader(classLoader, prefix); if (templateLoader != null) { if (templateLoader instanceof MultiTemplateLoader) { MultiTemplateLoader mtl = (MultiTemplateLoader) templateLoader; TemplateLoader[] loaders = new TemplateLoader[mtl.getTemplateLoaderCount() + 1]; for(int i=0; i < mtl.getTemplateLoaderCount(); i++) { loaders[i] = mtl.getTemplateLoader(i); } loaders[mtl.getTemplateLoaderCount() + 1] = classTemplateLoader; return new MultiTemplateLoader(loaders); } else { return new MultiTemplateLoader(new TemplateLoader[]{templateLoader, classTemplateLoader}); } } else { return classTemplateLoader; } }
Example #3
Source File: AdminFreemarkerTemplateProvider.java From karyon with Apache License 2.0 | 6 votes |
@PostConstruct public void commonConstruct() { // Just look for files in the class path fmConfig.setTemplateLoader(new MultiTemplateLoader(new TemplateLoader[]{ new ViewableResourceTemplateLoader(), new ClassTemplateLoader(getClass(), "/"), })); fmConfig.setNumberFormat("0"); fmConfig.setLocalizedLookup(false); fmConfig.setTemplateUpdateDelay(0); try { if (manager != null) { fmConfig.setSharedVariable("Global", manager.getGlobalModel()); fmConfig.setSharedVariable("Explorers", manager); } fmConfig.setSharedVariable("toJson", new ToJsonMethod()); } catch (TemplateModelException e) { throw new RuntimeException(e); } }
Example #4
Source File: FreeMarkerRenderer.java From opoopress with Apache License 2.0 | 6 votes |
private TemplateLoader buildTemplateLoader(Site site) { try { List<TemplateLoader> loaders = new ArrayList<TemplateLoader>(); loaders.add(new FileTemplateLoader(workingTemplateDir)); loaders.add(new FileTemplateLoader(templateDir)); loaders.add(new ClassTemplateLoader(AbstractFreeMarkerRenderer.class, "/org/opoo/press/templates")); //template registered by plugins List<TemplateLoader> instances = site.getFactory().getPluginManager().getObjectList(TemplateLoader.class); if (instances != null && !instances.isEmpty()) { loaders.addAll(instances); } TemplateLoader[] loadersArray = loaders.toArray(new TemplateLoader[loaders.size()]); return new MultiTemplateLoader(loadersArray); } catch (IOException e) { throw new IllegalArgumentException(e); } }
Example #5
Source File: FreeMarkerWorker.java From scipio-erp with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") public static <T extends TemplateLoader> T getTemplateLoader(MultiTemplateLoader multiLoader, Class<T> templateLoaderClass) { List<MultiTemplateLoader> multiLoaderList = null; // NOTE: optimized for simple setups for(int i = 0; i < multiLoader.getTemplateLoaderCount(); i++) { TemplateLoader loader = multiLoader.getTemplateLoader(i); if (templateLoaderClass.isAssignableFrom(loader.getClass())) { return (T) loader; } else if (loader instanceof MultiTemplateLoader) { if (multiLoaderList == null) { multiLoaderList = new ArrayList<>(); } multiLoaderList.add((MultiTemplateLoader) loader); } } if (multiLoaderList != null) { for(MultiTemplateLoader nextMultiLoader : multiLoaderList) { T nextLoader = getTemplateLoader(nextMultiLoader, templateLoaderClass); if (nextLoader != null) { return nextLoader; } } } return null; }
Example #6
Source File: CreateExtensionMojo.java From quarkus with Apache License 2.0 | 6 votes |
static TemplateLoader createTemplateLoader(File basedir, String templatesUriBase) throws IOException { final TemplateLoader defaultLoader = new ClassTemplateLoader(CreateExtensionMojo.class, DEFAULT_TEMPLATES_URI_BASE.substring(CLASSPATH_PREFIX.length())); if (DEFAULT_TEMPLATES_URI_BASE.equals(templatesUriBase)) { return defaultLoader; } else if (templatesUriBase.startsWith(CLASSPATH_PREFIX)) { return new MultiTemplateLoader( // new TemplateLoader[] { // new ClassTemplateLoader(CreateExtensionMojo.class, templatesUriBase.substring(CLASSPATH_PREFIX.length())), // defaultLoader // }); } else if (templatesUriBase.startsWith(FILE_PREFIX)) { final Path resolvedTemplatesDir = basedir.toPath().resolve(templatesUriBase.substring(FILE_PREFIX.length())); return new MultiTemplateLoader( // new TemplateLoader[] { // new FileTemplateLoader(resolvedTemplatesDir.toFile()), defaultLoader // }); } else { throw new IllegalStateException(String.format( "Cannot handle templatesUriBase '%s'; only value starting with '%s' or '%s' are supported", templatesUriBase, CLASSPATH_PREFIX, FILE_PREFIX)); } }
Example #7
Source File: TemplateHelpers.java From stategen with GNU Affero General Public License v3.0 | 6 votes |
public static Configuration getConfiguration(String... tempRoots) throws IOException { List<File> templateRootDirs = new ArrayList<File>(tempRoots.length); for (String fileName : tempRoots) { File file = FileHelpers.getFile(fileName); if (file.exists() && file.isDirectory()){ templateRootDirs.add(file); } } Configuration conf = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); FileTemplateLoader[] templateLoaders = new FileTemplateLoader[templateRootDirs.size()]; for (int i = 0; i < templateRootDirs.size(); i++) { templateLoaders[i] = new FileTemplateLoader((File) templateRootDirs.get(i)); } MultiTemplateLoader multiTemplateLoader = new MultiTemplateLoader(templateLoaders); conf.setTemplateLoader(multiTemplateLoader); return conf; }
Example #8
Source File: FreeMarkerConfigurationFactory.java From lams with GNU General Public License v2.0 | 5 votes |
/** * 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(List<TemplateLoader> templateLoaders) { int loaderCount = templateLoaders.size(); switch (loaderCount) { case 0: logger.info("No FreeMarker TemplateLoaders specified"); return null; case 1: return templateLoaders.get(0); default: TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[loaderCount]); return new MultiTemplateLoader(loaders); } }
Example #9
Source File: WordprocessingMLFreemarkerTemplate.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * 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(List<TemplateLoader> templateLoaders) { int loaderCount = templateLoaders.size(); switch (loaderCount) { case 0: LOG.info("No FreeMarker TemplateLoaders specified"); return null; case 1: return templateLoaders.get(0); default: TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[loaderCount]); return new MultiTemplateLoader(loaders); } }
Example #10
Source File: Generator.java From stategen with GNU Affero General Public License v3.0 | 5 votes |
public static Configuration newFreeMarkerConfiguration(List<File> templateRootDirs, String defaultEncoding, String templateName) throws IOException { Configuration conf = new Configuration(); FileTemplateLoader[] templateLoaders = new FileTemplateLoader[templateRootDirs.size()]; for (int i = 0; i < templateRootDirs.size(); i++) { templateLoaders[i] = new FileTemplateLoader((File) templateRootDirs.get(i)); } MultiTemplateLoader multiTemplateLoader = new MultiTemplateLoader(templateLoaders); conf.setTemplateLoader(multiTemplateLoader); conf.setNumberFormat("###############"); conf.setBooleanFormat("true,false"); conf.setDefaultEncoding(defaultEncoding); final String macro_include_ftl ="macro.include.ftl"; String autoIncludes = new File(new File(templateName).getParent(),macro_include_ftl).getPath(); List<String> availableAutoInclude = TemplateHelpers.getAvailableAutoInclude(conf, Arrays.asList(macro_include_ftl,autoIncludes)); conf.setAutoIncludes(availableAutoInclude); GLogger.info("[set Freemarker.autoIncludes]"+availableAutoInclude+" for templateName:"+templateName); // GLogger.info(templateName); // List<String> autoIncludes = getParentPaths(templateName, "macro.include.ftl"); // GLogger.info(autoIncludes.toString()); // List<String> availableAutoInclude = TemplateHelpers.getAvailableAutoInclude( // conf, autoIncludes); conf.setAutoIncludes(availableAutoInclude); GLogger.trace("set Freemarker.autoIncludes:" + availableAutoInclude + " for templateName:" + templateName + " autoIncludes:" + autoIncludes); return conf; }
Example #11
Source File: FreeMarkerWorker.java From scipio-erp with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public static <T extends TemplateLoader> T getTemplateLoader(Configuration config, Class<T> templateLoaderClass) { TemplateLoader loader = config.getTemplateLoader(); if (templateLoaderClass.isAssignableFrom(loader.getClass())) { return (T) loader; } else if (loader instanceof MultiTemplateLoader) { return getTemplateLoader((MultiTemplateLoader) loader, templateLoaderClass); } Debug.logWarning("No " + templateLoaderClass.getName() + " template loader class found for Freemarker Configuration " + config, module); return null; }
Example #12
Source File: FreeMarkerConfigurationFactory.java From java-technology-stack with MIT License | 5 votes |
/** * 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 */ @Nullable protected TemplateLoader getAggregateTemplateLoader(List<TemplateLoader> templateLoaders) { switch (templateLoaders.size()) { case 0: logger.debug("No FreeMarker TemplateLoaders specified"); return null; case 1: return templateLoaders.get(0); default: TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[0]); return new MultiTemplateLoader(loaders); } }
Example #13
Source File: ScipioFtlTemplateLoader.java From scipio-erp with Apache License 2.0 | 5 votes |
/** Gets the configured instance, expected to match the default instance unless extended. Not expected to return null unless misconfiguration. */ public static ScipioFtlTemplateLoader getInstance(Configuration config) { TemplateLoader loader = config.getTemplateLoader(); if (loader instanceof MultiTemplateLoader) { loader = FreeMarkerWorker.getTemplateLoader((MultiTemplateLoader) loader, ScipioFtlTemplateLoader.class); } return (ScipioFtlTemplateLoader) loader; }
Example #14
Source File: FreeMarkerConfigurationFactory.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * 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(List<TemplateLoader> templateLoaders) { int loaderCount = templateLoaders.size(); switch (loaderCount) { case 0: logger.info("No FreeMarker TemplateLoaders specified"); return null; case 1: return templateLoaders.get(0); default: TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[loaderCount]); return new MultiTemplateLoader(loaders); } }
Example #15
Source File: FreeMarkerConfiguration.java From graviteeio-access-management with Apache License 2.0 | 5 votes |
@Bean public freemarker.template.Configuration getConfiguration() { final freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22); configuration.setLocalizedLookup(false); configuration.setOutputFormat(HTMLOutputFormat.INSTANCE); configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER); try { TemplateLoader[] templateLoaders = { overrideTemplateLoader(), new FileTemplateLoader(new File(templatesPath)) }; configuration.setTemplateLoader(new MultiTemplateLoader(templateLoaders)); } catch (final IOException e) { LOGGER.warn("Error occurred while trying to read email templates", e); } return configuration; }
Example #16
Source File: TextReporter.java From revapi with Apache License 2.0 | 5 votes |
/** * Creates a new FreeMarker configuration. * By default, it is configured as follows: * <ul> * <li>compatibility level is set to 2.3.23 * <li>the object wrapper is configured to expose fields * <li>API builtins are enabled * <li>there are 2 template loaders - 1 for loading templates from /META-INF using a classloader and a second * one to load templates from files. * </ul> * @return */ protected Configuration createFreeMarkerConfiguration() { DefaultObjectWrapperBuilder bld = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_23); bld.setExposeFields(true); Configuration freeMarker = new Configuration(Configuration.VERSION_2_3_23); freeMarker.setObjectWrapper(bld.build()); freeMarker.setAPIBuiltinEnabled(true); freeMarker.setTemplateLoader(new MultiTemplateLoader( new TemplateLoader[]{new ClassTemplateLoader(getClass(), "/META-INF"), new NaiveFileTemplateLoader()})); return freeMarker; }
Example #17
Source File: FreeMarkerConfigurationFactory.java From spring-analysis-note with MIT License | 5 votes |
/** * 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 */ @Nullable protected TemplateLoader getAggregateTemplateLoader(List<TemplateLoader> templateLoaders) { switch (templateLoaders.size()) { case 0: logger.debug("No FreeMarker TemplateLoaders specified"); return null; case 1: return templateLoaders.get(0); default: TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[0]); return new MultiTemplateLoader(loaders); } }
Example #18
Source File: CqUtils.java From camel-quarkus with Apache License 2.0 | 5 votes |
static TemplateLoader createTemplateLoader(Path basePath, String defaultUriBase, String templatesUriBase) { final TemplateLoader defaultLoader = new ClassTemplateLoader(CqUtils.class, defaultUriBase.substring(CLASSPATH_PREFIX.length())); if (defaultUriBase.equals(templatesUriBase)) { return defaultLoader; } else if (templatesUriBase.startsWith(CLASSPATH_PREFIX)) { return new MultiTemplateLoader( // new TemplateLoader[] { // new ClassTemplateLoader(CqUtils.class, templatesUriBase.substring(CLASSPATH_PREFIX.length())), // defaultLoader // }); } else if (templatesUriBase.startsWith(FILE_PREFIX)) { final Path resolvedTemplatesDir = basePath.resolve(templatesUriBase.substring(FILE_PREFIX.length())); try { return new MultiTemplateLoader( // new TemplateLoader[] { // new FileTemplateLoader(resolvedTemplatesDir.toFile()), defaultLoader // }); } catch (IOException e) { throw new RuntimeException(e); } } else { throw new IllegalStateException(String.format( "Cannot handle templatesUriBase '%s'; only value starting with '%s' or '%s' are supported", templatesUriBase, CLASSPATH_PREFIX, FILE_PREFIX)); } }
Example #19
Source File: FtlUtils.java From onetwo with Apache License 2.0 | 4 votes |
public static TemplateLoader getMultiTemplateLoader(List<TemplateLoader> templateLoaders) { Assert.notEmpty(templateLoaders); TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[templateLoaders.size()]); return new MultiTemplateLoader(loaders); }