Java Code Examples for freemarker.template.Configuration#setClassLoaderForTemplateLoading()

The following examples show how to use freemarker.template.Configuration#setClassLoaderForTemplateLoading() . 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: FreeMarkerTemplateUtil.java    From springboot-learn with MIT License 6 votes vote down vote up
public String getEmailHtml(Map map, String templateName) {

        String htmlText = "";
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);
        try {
            //加载模板路径,此处对应在resource目录下
            configuration.setClassLoaderForTemplateLoading(ClassLoader.getSystemClassLoader(), "ftl");
            //或者
//            configuration.setClassForTemplateLoading(this.getClass(), "/com/mail/ftl");
            //获取对应名称的模板
            Template template = configuration.getTemplate(templateName);
            //渲染模板为html
            htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return htmlText;
    }
 
Example 2
Source File: FreemarkerRenderer.java    From act with GNU General Public License v3.0 6 votes vote down vote up
private void init(String dbHost, Integer dbPort, String dbName, String dnaCollection, String pathwayCollection) throws IOException {
  cfg = new Configuration(Configuration.VERSION_2_3_23);

  cfg.setClassLoaderForTemplateLoading(
      this.getClass().getClassLoader(), "/act/installer/reachablesexplorer/templates");
  cfg.setDefaultEncoding("UTF-8");

  cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  cfg.setLogTemplateExceptions(true);

  reachableTemplate = cfg.getTemplate(reachableTemplateName);
  pathwayTemplate = cfg.getTemplate(pathwayTemplateName);

  // TODO: move this elsewhere.
  MongoClient client = new MongoClient(new ServerAddress(dbHost, dbPort));
  DB db = client.getDB(dbName);

  dnaDesignCollection = JacksonDBCollection.wrap(db.getCollection(dnaCollection), DNADesign.class, String.class);
  Cascade.setCollectionName(pathwayCollection);
}
 
Example 3
Source File: ClinicalNoteExporter.java    From synthea with Apache License 2.0 6 votes vote down vote up
private static Configuration templateConfiguration() {
  Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
  configuration.setDefaultEncoding("UTF-8");
  configuration.setLogTemplateExceptions(false);
  try {
    configuration.setSetting("object_wrapper",
        "DefaultObjectWrapper(2.3.26, forceLegacyNonListCollections=false, "
            + "iterableSupport=true, exposeFields=true)");
  } catch (TemplateException e) {
    e.printStackTrace();
  }
  configuration.setAPIBuiltinEnabled(true);
  configuration.setClassLoaderForTemplateLoading(ClassLoader.getSystemClassLoader(),
      "templates/notes");
  return configuration;
}
 
Example 4
Source File: CCDAExporter.java    From synthea with Apache License 2.0 6 votes vote down vote up
private static Configuration templateConfiguration() {
  Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
  configuration.setDefaultEncoding("UTF-8");
  configuration.setLogTemplateExceptions(false);
  try {
    configuration.setSetting("object_wrapper",
        "DefaultObjectWrapper(2.3.26, forceLegacyNonListCollections=false, "
            + "iterableSupport=true, exposeFields=true)");
  } catch (TemplateException e) {
    e.printStackTrace();
  }
  configuration.setAPIBuiltinEnabled(true);
  configuration.setClassLoaderForTemplateLoading(ClassLoader.getSystemClassLoader(),
      "templates/ccda");
  return configuration;
}
 
Example 5
Source File: FreeMarkerTemplateUtil.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
 * 获取模板信息
 *
 * @param name 模板名
 * @return
 */
public Template getTemplate(String name) {
    //通过freemarkerd Configuration读取相应的ftl
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
    //设定去哪里读取相应的ftl模板文件,指定模板路径
    cfg.setClassLoaderForTemplateLoading(ClassLoader.getSystemClassLoader(), "ftl");
    try {
        //在模板文件目录中找到名称为name的文件
        Template template = cfg.getTemplate(name);
        return template;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 6
Source File: ConfigurationExporterTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
static String renderDocumentation(ConfigurationRegistry configurationRegistry) throws IOException, TemplateException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_27);
    cfg.setClassLoaderForTemplateLoading(ConfigurationExporterTest.class.getClassLoader(), "/");
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    cfg.setLogTemplateExceptions(false);

    Template temp = cfg.getTemplate("configuration.asciidoc.ftl");
    StringWriter tempRenderedFile = new StringWriter();
    tempRenderedFile.write("////\n" +
        "This file is auto generated\n" +
        "\n" +
        "Please only make changes in configuration.asciidoc.ftl\n" +
        "////\n");
    final List<ConfigurationOption<?>> nonInternalOptions = configurationRegistry.getConfigurationOptionsByCategory()
        .values()
        .stream()
        .flatMap(List::stream)
        .filter(option -> !option.getTags().contains("internal"))
        .collect(Collectors.toList());
    final Map<String, List<ConfigurationOption<?>>> optionsByCategory = nonInternalOptions.stream()
        .collect(Collectors.groupingBy(ConfigurationOption::getConfigurationCategory, TreeMap::new, Collectors.toList()));
    temp.process(Map.of(
        "config", optionsByCategory,
        "keys", nonInternalOptions.stream().map(ConfigurationOption::getKey).sorted().collect(Collectors.toList())
    ), tempRenderedFile);

    // re-process the rendered template to resolve the ${allInstrumentationGroupNames} placeholder
    StringWriter out = new StringWriter();
    new Template("", tempRenderedFile.toString(), cfg)
        .process(Map.of("allInstrumentationGroupNames", getAllInstrumentationGroupNames()), out);

    return out.toString();
}
 
Example 7
Source File: FreeMarkerEngine.java    From requirementsascode with Apache License 2.0 5 votes vote down vote up
private void createConfiguration(String basePackagePath) {
  cfg = new Configuration(Configuration.VERSION_2_3_26);
  cfg.setClassLoaderForTemplateLoading(getClass().getClassLoader(), basePackagePath);
  cfg.setLogTemplateExceptions(false);
  setDefaultEncoding("UTF-8");
  setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
}
 
Example 8
Source File: ViewFreemarker.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@PostConstruct
public void init()
{
  Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
  
  cfg.setDefaultEncoding("UTF-8");
  
  String templatePath = _config.get("view.freemarker.templates",
                                   "classpath:/templates");
  
  ClassLoader loader = Thread.currentThread().getContextClassLoader();
  
  if (templatePath.startsWith("classpath:")) {
    String path = templatePath.substring(("classpath:").length());
    
    cfg.setClassLoaderForTemplateLoading(loader, path);
  }
  else {
    throw new UnsupportedOperationException();
  }
  
  cfg.setAutoFlush(false);
  
  cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
  
  _cfg = cfg;
}
 
Example 9
Source File: OALRuntime.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public OALRuntime(OALDefine define) {
    oalDefine = define;
    classPool = ClassPool.getDefault();
    configuration = new Configuration(new Version("2.3.28"));
    configuration.setEncoding(Locale.ENGLISH, CLASS_FILE_CHARSET);
    configuration.setClassLoaderForTemplateLoading(OALRuntime.class.getClassLoader(), "/code-templates");
    allDispatcherContext = new AllDispatcherContext();
    metricsClasses = new ArrayList<>();
    dispatcherClasses = new ArrayList<>();
    openEngineDebug = StringUtil.isNotEmpty(System.getenv("SW_OAL_ENGINE_DEBUG"));
}
 
Example 10
Source File: AbstractRunningGenerator.java    From skywalking with Apache License 2.0 5 votes vote down vote up
protected AbstractRunningGenerator() {
    cfg = new Configuration(Configuration.VERSION_2_3_28);
    try {
        cfg.setClassLoaderForTemplateLoading(this.getClass().getClassLoader(), "/");
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        cfg.setLogTemplateExceptions(false);
        cfg.setWrapUncheckedExceptions(true);
    } catch (Exception e) {
        // never to do this
    }
}