/** * Sets the file system directory from which to load templates. This is equivalent to * {@code setTemplateLoader(new FileTemplateLoader(dir))}, so see * {@link FileTemplateLoader#FileTemplateLoader(File)} for more details. * * <p> * Note that FreeMarker can load templates from non-file-system sources too. See * {@link #setTemplateLoader(TemplateLoader)} from more details. * * <p> * Note that this shouldn't be used for loading templates that are coming from a WAR; use * {@link #setServletContextForTemplateLoading(Object, String)} then. Servlet containers might not unpack the WAR * file, in which case you clearly can't access the contained files via {@link File}. Even if the WAR is unpacked, * the servlet container might not expose the location as a {@link File}. * {@link #setServletContextForTemplateLoading(Object, String)} on the other hand will work in all these cases. */ public void setDirectoryForTemplateLoading(File dir) throws IOException { TemplateLoader tl = getTemplateLoader(); if (tl instanceof FileTemplateLoader) { String path = ((FileTemplateLoader) tl).baseDir.getCanonicalPath(); if (path.equals(dir.getCanonicalPath())) return; } setTemplateLoader(new FileTemplateLoader(dir)); }
private TemplateLoader getDefaultTemplateLoader() { return createDefaultTemplateLoader(getIncompatibleImprovements(), getTemplateLoader()); }
@Test public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Exception { FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean(); fcfb.setTemplateLoaderPath("file:/mydir"); fcfb.afterPropertiesSet(); Configuration cfg = fcfb.getObject(); assertTrue(cfg.getTemplateLoader() instanceof SpringTemplateLoader); }
TemplateLoader tl = getTemplateLoader(); String msg; if (tl == null) {
@Override public boolean exist(String template) throws IOException { return this.freemarker.getTemplateLoader().findTemplateSource(template) != null; } @Override
/** * Set the explicit directory from which to load templates. */ public void setDirectoryForTemplateLoading(File dir) throws IOException { TemplateLoader tl = getTemplateLoader(); if (tl instanceof FileTemplateLoader) { String path = ((FileTemplateLoader) tl).baseDir.getCanonicalPath(); if (path.equals(dir.getCanonicalPath())) return; } setTemplateLoader(new FileTemplateLoader(dir)); }
/** * Pull this into a protected method so we can stub it out in the unit tests. * Other options: * 1) receive a TemplateLoader into the constructor of ObjectPropertyTemplateModel, * 2) provide a service that will check to see whether a given template name is valid, * 3) skip the test for valid template name until we try to use the thing. * This will do for now. */ protected TemplateLoader getFreemarkerTemplateLoader() { return FreemarkerConfiguration.getConfig(vreq).getTemplateLoader(); }
protected TemplateLoader getFreemarkerTemplateLoader() { return FreemarkerConfiguration.getConfig(vreq).getTemplateLoader(); }
public final TemplateRender compile(String templateStr) { StringTemplateLoader templateLoader = ((StringTemplateLoader) freemarkerCfg.getTemplateLoader()); String name = StringUtils.concat("template.", templateStr.hashCode()); templateLoader.putTemplate(name, templateStr); freemarkerCfg.setTemplateLoader(templateLoader); final String finalName = name; return vars -> { Template template = freemarkerCfg.getTemplate(finalName); StringWriter out = new StringWriter(); template.process(vars, out); return out.getBuffer().toString(); }; } }
public Time lastModifiedTime() { Object source; try { source = cfg.getTemplateLoader().findTemplateSource( templateName); } catch (IOException e) { // TODO: log this return null; } if (source != null) { long modified = cfg.getTemplateLoader().getLastModified(source); return Time.valueOf(modified); } return null; }
public Time lastModifiedTime() { Object source; try { source = cfg.getTemplateLoader().findTemplateSource(templateName); } catch (IOException e) { LOGGER.log( Level.WARNING, "Error getting last modified time from template \"" + templateName + "\"", e); return null; } if (source != null) { long modified = cfg.getTemplateLoader().getLastModified(source); return Time.valueOf(new Date(modified)); } return null; }
/** * Puts the template with the given key and source code to the template cache.<p> * * @param fileName the absolute path of the template file in the VFS of OpenCms * @param templateSource the source code of the template * @return the template that was stored in the cache */ private Template putTemplate(String fileName, String templateSource) { String cacheKey = getCacheKey(fileName); ((StringTemplateLoader)m_configuration.getTemplateLoader()).putTemplate(cacheKey, templateSource); try { return m_configuration.getTemplate(cacheKey); } catch (IOException e) { return null; } }
private TemplateLoader getDefaultTemplateLoader() { return createDefaultTemplateLoader(getIncompatibleImprovements(), getTemplateLoader()); }
private TemplateLoader getDefaultTemplateLoader() { return createDefaultTemplateLoader(getIncompatibleImprovements(), getTemplateLoader()); }
private ModuleGenerator() { fmCfg = new Configuration(); fmCfg.setClassForTemplateLoading(ModuleGenerator.class, ""); fmCfg.setObjectWrapper(new DefaultObjectWrapper()); fmCfg.setTemplateLoader(new OsSpecificTemplateLoader(fmCfg.getTemplateLoader())); }
public List<String> loadContent(String template) { TemplateLoader loader = conf.getTemplateLoader(); try { Method lookup = TemplateCache.class.getDeclaredMethod("lookupTemplate", String.class, Locale.class, Object.class); lookup.setAccessible(true); Field cache = Configuration.class.getDeclaredField("cache"); cache.setAccessible(true); TemplateLookupResult result = $.invokeVirtual(cache.get(conf), lookup, template, Locale.getDefault(), null); Method templateSource = TemplateLookupResult.class.getDeclaredMethod("getTemplateSource"); templateSource.setAccessible(true); Reader reader = loader.getReader($.invokeVirtual(result, templateSource), conf.getEncoding(conf.getLocale())); return IO.readLines(reader); } catch (IOException e1) { throw E.ioException(e1); } catch (Exception e) { throw E.unexpected(e); } }
@Override public void init() throws ServletException { super.init(); /* * we here need to inject our own template loader. We cannot do this in createConfiguration() as we would like, * because the FreemarkerServlet sets the template loader in the init() *after* createConfiguration() again, to the default * WebappTemplateLoader */ Configuration conf = super.getConfiguration(); TemplateLoader defaultLoader = conf.getTemplateLoader(); // repository template loader TemplateLoader repositoryLoader = new RepositoryTemplateLoader(); TemplateLoader[] loaders = new TemplateLoader[] { defaultLoader, repositoryLoader }; TemplateLoader multiLoader = new MultiTemplateLoader(loaders); conf.setTemplateLoader(multiLoader); conf.setLocalizedLookup(false); }
@Override public void removePerunNotifTemplateMessage(int id) throws InternalErrorException { PerunNotifTemplateMessage templateMessage = getPerunNotifTemplateMessageById(id); if (templateMessage == null) { throw new NotExistsException("Template message with id: " + id + " not exists."); } perunNotifTemplateDao.removePerunNotifTemplateMessage(id); PerunNotifTemplate template = allTemplatesById.get(templateMessage.getTemplateId()); template.getPerunNotifTemplateMessages().remove(templateMessage); StringTemplateLoader stringTemplateLoader = (StringTemplateLoader) configuration.getTemplateLoader(); String templateName = createTemplateName(templateMessage); stringTemplateLoader.removeTemplate(templateName); configuration.clearTemplateCache(); }
public static void renderTemplateFromString(String templateName, String templateString, Map<String, Object> context, Appendable outWriter, long lastModificationTime, boolean useCache) throws TemplateException, IOException { Template template = null; if (useCache) { template = cachedTemplates.get(templateName); } if (template == null) { StringTemplateLoader stringTemplateLoader = (StringTemplateLoader)((MultiTemplateLoader)defaultOfbizConfig.getTemplateLoader()).getTemplateLoader(1); Object templateSource = stringTemplateLoader.findTemplateSource(templateName); if (templateSource == null || stringTemplateLoader.getLastModified(templateSource) < lastModificationTime) { stringTemplateLoader.putTemplate(templateName, templateString, lastModificationTime); } } template = getTemplate(templateName); renderTemplate(template, context, outWriter); }