/** * Return a new Configuration object. Subclasses can override this for custom * initialization (e.g. specifying a FreeMarker compatibility level which is a * new feature in FreeMarker 2.3.21), or for using a mock object for testing. * <p>Called by {@code createConfiguration()}. * @return the Configuration object * @throws IOException if a config file wasn't found * @throws TemplateException on FreeMarker initialization failure * @see #createConfiguration() */ protected Configuration newConfiguration() throws IOException, TemplateException { return new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); }
/** * Return a new Configuration object. Subclasses can override this for custom * initialization (e.g. specifying a FreeMarker compatibility level which is a * new feature in FreeMarker 2.3.21), or for using a mock object for testing. * <p>Called by {@code createConfiguration()}. * @return the Configuration object * @throws IOException if a config file wasn't found * @throws TemplateException on FreeMarker initialization failure * @see #createConfiguration() */ protected Configuration newConfiguration() throws IOException, TemplateException { return new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); }
/** * Creates the FreeMarker {@link Configuration} singleton and (when overidden) maybe sets its defaults. Servlet * init-params will be applied later, and thus can overwrite the settings specified here. * * <p> * By overriding this method you can set your preferred {@link Configuration} setting defaults, as only the settings * for which an init-param was specified will be overwritten later. (Note that {@link FreemarkerServlet} also has * its own defaults for a few settings, but since 2.3.22, the servlet detects if those settings were already set * here and then it won't overwrite them.) * * <p> * The default implementation simply creates a new instance with {@link Configuration#Configuration()} and returns * it. */ protected Configuration createConfiguration() { // We can only set incompatible_improvements later, so ignore the deprecation warning here. return new Configuration(); }
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; }
public AlertTemplateProcessor(AlertingPlugin alertingPlugin) { this.alertingPlugin = alertingPlugin; cfg = new Configuration(Configuration.VERSION_2_3_22); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); }
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; }
@Override public FreemarkerTemplateEngine init(ConfigBuilder configBuilder) { super.init(configBuilder); configuration = new Configuration(); configuration.setDefaultEncoding(ConstVal.UTF8); configuration.setClassForTemplateLoading(FreemarkerTemplateEngine.class, StringPool.SLASH); return this; }
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())); } }
public String render() { Configuration freemarkerConfiguration = new Configuration(); Map<String, Object> model = new HashMap<>(); model.put("pageSpec", this); try { Template template = new Template("report-main", new InputStreamReader(getClass().getResourceAsStream("/generator/page-spec.gspec.ftl")), freemarkerConfiguration); StringWriter sw = new StringWriter(); template.process(model, sw); sw.flush(); sw.close(); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error rendering template", ex); } } }
private MailTemplateProvider() { configuration = new Configuration(Configuration.getVersion()); configuration.setClassForTemplateLoading(MailTemplateProvider.class, "/mail_templates"); configuration.setDefaultEncoding("UTF-8"); }
private Configuration createConfiguration(final TemplateLoader templateLoader, final Charset charset) { Configuration cfg = new Configuration(CURRENT_VERSION); cfg.setObjectWrapper(new DefaultObjectWrapperBuilder(CURRENT_VERSION).build()); cfg.setDefaultEncoding(charset.name()); cfg.setTemplateLoader(templateLoader); return cfg; }
public FreemarkerDefaultConfigurationFactory(ServletContext servletContext) { super(); // Create different loaders. final List<TemplateLoader> loaders = new ArrayList<>(); if (servletContext != null) { loaders.add(new WebappTemplateLoader(servletContext)); } loaders.add(new ClassTemplateLoader(FreemarkerDefaultConfigurationFactory.class, "/")); try { loaders.add(new FileTemplateLoader(new File("/"))); } catch (IOException e) { // NOOP } // Create Base configuration. configuration = new Configuration(); configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]))); }
@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 FreemarkerContext() { this.configuration = new Configuration(Configuration.VERSION_2_3_23); this.configuration.setLocalizedLookup(false); this.configuration.setTemplateUpdateDelayMilliseconds(0); this.configuration.setClassLoaderForTemplateLoading(getClass().getClassLoader(), BASE_PACKAGE_PATH); }
private String processTemplate(String templateName, String templateString, TemplateModel templateModel) throws IOException, TemplateException { Template template = new Template(templateName, templateString, new Configuration(Configuration.VERSION_2_3_22)); StringWriter out = new StringWriter(templateString.length()); template.process(Collections.singletonMap("templateModel", templateModel), out); return out.toString(); }
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 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 FreemarkerXmlPlannerBenchmarkFactory configure(Reader templateReader, Object model) { Configuration freemarkerCfg = new Configuration(); freemarkerCfg.setDefaultEncoding("UTF-8"); // Write each number according to Java language spec (as expected by XStream), so not formatted by locale freemarkerCfg.setNumberFormat("computer"); // Write each date according to OSI standard (as expected by XStream) freemarkerCfg.setDateFormat("yyyy-mm-dd"); // Write each datetime in format expected by XStream freemarkerCfg.setDateTimeFormat("yyyy-mm-dd HH:mm:ss.SSS z"); // Write each time in format expected by XStream freemarkerCfg.setTimeFormat("HH:mm:ss.SSS"); String templateFilename = "benchmarkTemplate.ftl"; Template template; try { template = new Template(templateFilename, templateReader, freemarkerCfg, "UTF-8"); } catch (IOException e) { throw new IllegalStateException("Can not read template (" + templateFilename + ") from templateReader.", e); } return configure(template, model); }
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) {