@Override public FreemarkerTemplateEngine init(ConfigBuilder configBuilder) { super.init(configBuilder); configuration = new Configuration(); configuration.setDefaultEncoding(ConstVal.UTF8); configuration.setClassForTemplateLoading(FreemarkerTemplateEngine.class, StringPool.SLASH); return this; }
private Configuration newFreeMarkerConfig() { Configuration freeMarkerConfig = new Configuration(Configuration.VERSION_2_3_24); freeMarkerConfig.setDefaultEncoding("UTF-8"); freeMarkerConfig.setClassForTemplateLoading(this.getClass(), "/"); freeMarkerConfig .setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER); return freeMarkerConfig; }
private Configuration getConfiguration(String probingTemplate) throws IOException { Configuration config = new Configuration(Configuration.VERSION_2_3_23); config.setClassForTemplateLoading(getClass(), "/"); try { config.getTemplate(probingTemplate); } catch (TemplateNotFoundException e) { // When running from an IDE like IntelliJ, class loading resources may fail for some reason (Gradle is OK) // Working dir is module dir File dir = new File("src/main/resources/"); if (!dir.exists()) { // Working dir is base module dir dir = new File("DaoGenerator/src/main/resources/"); } if (dir.exists() && new File(dir, probingTemplate).exists()) { config.setDirectoryForTemplateLoading(dir); config.getTemplate(probingTemplate); } else { throw e; } } return config; }
private MailTemplateProvider() { configuration = new Configuration(Configuration.getVersion()); configuration.setClassForTemplateLoading(MailTemplateProvider.class, "/mail_templates"); configuration.setDefaultEncoding("UTF-8"); }
public void renderError(HttpServletRequest request, HttpServletResponse response, String error) { try { logger.error("Error :" + error); Map<String, Object> templateParam = new HashMap<>(); response.setContentType("text/html;charset=UTF-8"); Template template = null; Configuration config = new Configuration(); config.setClassForTemplateLoading(request.getServletContext().getClass(), "/"); template = config.getTemplate("/webapp/template/error.ftl.html"); templateParam.put("error", error); template.process(templateParam, response.getWriter()); } catch (TemplateException | IOException ex) { logger.error(Arrays.toString(ex.getStackTrace())); } }
@Override public Configuration load(@Nonnull Class<?> key) throws Exception { final Configuration configuration = new Configuration(FREEMARKER_VERSION); configuration.setObjectWrapper(new DefaultObjectWrapperBuilder(FREEMARKER_VERSION).build()); configuration.loadBuiltInEncodingMap(); configuration.setDefaultEncoding(StandardCharsets.UTF_8.name()); configuration.setClassForTemplateLoading(key, "/"); // setting the outputformat implicitly enables auto escaping configuration.setOutputFormat(HTMLOutputFormat.INSTANCE); for (Map.Entry<String, String> entry : baseConfig.entrySet()) { configuration.setSetting(entry.getKey(), entry.getValue()); } return configuration; }
public void renderHtml(HttpServletRequest request, HttpServletResponse response, Map<String, Object> templateParam, String view) throws IOException, TemplateException { if (request != null && response != null && templateParam != null) { //There is no way to access session info in freemarker template. //Hence all view rendering happens via this function which adds session info to attribute for each request. HttpSession session = request.getSession(); if (session != null) { //Flash messages are always set at session level and need to be propgrated to attributes. //They are reset after being displayed once. if (session.getAttribute("flashMsg") != null) { templateParam.put("flashMsg", session.getAttribute("flashMsg")); session.setAttribute("flashMsg", null); } templateParam.put("authName", session.getAttribute("authName")); templateParam.put("authRole", session.getAttribute("authRole")); response.setContentType("text/html;charset=UTF-8"); Template template = null; long startTime = System.currentTimeMillis(); Configuration config = new Configuration(); config.setClassForTemplateLoading(request.getServletContext().getClass(), "/"); template = config.getTemplate("/webapp/template/" + view); template.process(templateParam, response.getWriter()); long estimatedTime = System.currentTimeMillis() - startTime; logger.trace("Elapsed Time in Secs for Rendering: " + estimatedTime / 1000); } } }
private void writeHtmlOverviewFile() { File benchmarkReportDirectory = plannerBenchmarkResult.getBenchmarkReportDirectory(); WebsiteResourceUtils.copyResourcesTo(benchmarkReportDirectory); htmlOverviewFile = new File(benchmarkReportDirectory, "index.html"); Configuration freemarkerCfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); freemarkerCfg.setDefaultEncoding("UTF-8"); freemarkerCfg.setLocale(locale); freemarkerCfg.setClassForTemplateLoading(BenchmarkReport.class, ""); String templateFilename = "benchmarkReport.html.ftl"; Map<String, Object> model = new HashMap<>(); model.put("benchmarkReport", this); model.put("reportHelper", new ReportHelper()); try (Writer writer = new OutputStreamWriter(new FileOutputStream(htmlOverviewFile), "UTF-8")) { Template template = freemarkerCfg.getTemplate(templateFilename); template.process(model, writer); } catch (IOException e) { throw new IllegalArgumentException("Can not read templateFilename (" + templateFilename + ") or write htmlOverviewFile (" + htmlOverviewFile + ").", e); } catch (TemplateException e) { throw new IllegalArgumentException("Can not process Freemarker templateFilename (" + templateFilename + ") to htmlOverviewFile (" + htmlOverviewFile + ").", e); } }
public ScriptResource(Context ctx, Request req, Response res) throws ResourceException { super(ctx, req, res); setModifiable(true); getVariants().add(new Variant(MediaType.TEXT_HTML)); getVariants().add(new Variant(MediaType.APPLICATION_XML)); Configuration tmpltCfg = new Configuration(); tmpltCfg.setClassForTemplateLoading(this.getClass(),""); tmpltCfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); setTemplateConfiguration(tmpltCfg); scriptingConsole = new ScriptingConsole(cj); } public void setTemplateConfiguration(Configuration tmpltCfg) {
public EngineResource(Context ctx, Request req, Response res) { super(ctx, req, res); setModifiable(true); getVariants().add(new Variant(MediaType.TEXT_HTML)); getVariants().add(new Variant(MediaType.APPLICATION_XML)); Configuration tmpltCfg = new Configuration(); tmpltCfg.setClassForTemplateLoading(this.getClass(),""); tmpltCfg.setObjectWrapper(new DefaultObjectWrapper()); setTemplateConfiguration(tmpltCfg); }
public BeanBrowseResource(Context ctx, Request req, Response res) throws ResourceException { super(ctx, req, res); getVariants().add(new Variant(MediaType.TEXT_HTML)); getVariants().add(new Variant(MediaType.APPLICATION_XML)); setModifiable(true); // accept POSTs appCtx = cj.getJobContext(); beanPath = (String)req.getAttributes().get("beanPath"); if (beanPath!=null) { try { beanPath = URLDecoder.decode(beanPath,"UTF-8"); } catch (UnsupportedEncodingException e) { // inconceivable! UTF-8 required all Java impls } } else { beanPath = ""; } Configuration tmpltCfg = new Configuration(); tmpltCfg.setClassForTemplateLoading(this.getClass(),""); tmpltCfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); setTemplateConfiguration(tmpltCfg); } public void setTemplateConfiguration(Configuration tmpltCfg) {
public JobResource(Context ctx, Request req, Response res) throws ResourceException { super(ctx, req, res); setModifiable(true); getVariants().add(new Variant(MediaType.TEXT_HTML)); getVariants().add(new Variant(MediaType.APPLICATION_XML)); cj = getEngine().getJob( TextUtils.urlUnescape((String) req.getAttributes().get("job"))); Configuration tmpltCfg = new Configuration(); tmpltCfg.setClassForTemplateLoading(this.getClass(),""); tmpltCfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); setTemplateConfiguration(tmpltCfg); } public void setTemplateConfiguration(Configuration tmpltCfg) {
freemarkerMailConfiguration.setClassForTemplateLoading(HtmlEmailSenderImpl.class, "/"); Template textTemplate = freemarkerMailConfiguration.getTemplate(new StringBuilder(TEMPLATE_PATH).append("").append("/").append(tmpl).toString()); final StringWriter textWriter = new StringWriter(); freemarkerMailConfiguration.setClassForTemplateLoading(HtmlEmailSenderImpl.class, "/"); Template htmlTemplate = freemarkerMailConfiguration.getTemplate(new StringBuilder(TEMPLATE_PATH).append("").append("/").append(tmpl).toString()); final StringWriter htmlWriter = new StringWriter();
cfg.setClassForTemplateLoading(this.getClass(), "/");
"fid.3")); cfg = TemplateUtils.getSafeConfiguration(); cfg.setClassForTemplateLoading(getClass(), ""); cfg.setObjectWrapper(createWrapper());
/** * @param config * @param startStep * @param fromVersion */ public InstallServlet(Properties config, String startStep, String fromVersion) { this.startStep = startStep; this.fromVersion = fromVersion; this.cmsUpgrader = new CmsUpgrader(config); this.freemarkerConfiguration = new Configuration(Configuration.getVersion()); freemarkerConfiguration.setClassForTemplateLoading(getClass(), "/initialization/template/"); freemarkerConfiguration.setDefaultEncoding("utf-8"); freemarkerConfiguration.setNumberFormat("#"); }
/** * @param config * @param startStep * @param fromVersion */ public InstallServlet(Properties config, String startStep, String fromVersion) { this.startStep = startStep; this.fromVersion = fromVersion; this.cmsUpgrader = new CmsUpgrader(config); this.freemarkerConfiguration = new Configuration(Configuration.getVersion()); freemarkerConfiguration.setClassForTemplateLoading(getClass(), "/initialization/template/"); freemarkerConfiguration.setDefaultEncoding("utf-8"); freemarkerConfiguration.setNumberFormat("#"); }
} else { cfg.setClassForTemplateLoading(getResourceLoaderClass(), "templates");
private Configuration getConfig() { Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); cfg.setClassForTemplateLoading(ExtentReports.class, TEMPLATE_LOCATION); cfg.setDefaultEncoding(ENCODING); return cfg; }
configuration.setOutputEncoding(PippoConstants.UTF8); configuration.setLocalizedLookup(true); configuration.setClassForTemplateLoading(FreemarkerTemplateEngine.class, getTemplatePathPrefix());