Java Code Examples for freemarker.template.Configuration#DEFAULT_INCOMPATIBLE_IMPROVEMENTS

The following examples show how to use freemarker.template.Configuration#DEFAULT_INCOMPATIBLE_IMPROVEMENTS . 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: TemplateHelper.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected static String __processTemplate(TemplateLoader templateLoader, String templateName,
                                          Map<String, ?> parameterValues) {
    Map<String, Object> params = prepareParams(parameterValues);

    StringWriter writer = new StringWriter();

    try {
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        configuration.setTemplateLoader(templateLoader);
        Template template = configuration.getTemplate(templateName);
        template.process(params, writer);

        return writer.toString();
    } catch (Throwable e) {
        throw new RuntimeException("Unable to process template", e);
    }
}
 
Example 2
Source File: CodeRenderer.java    From jpa-entity-generator with MIT License 6 votes vote down vote up
/**
 * Renders source code by using Freemarker template engine.
 */
public static String render(String templatePath, RenderingData data) throws IOException, TemplateException {
    Configuration config = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
    StringTemplateLoader templateLoader = new StringTemplateLoader();
    String source;
    try (InputStream is = ResourceReader.getResourceAsStream(templatePath);
         BufferedReader buffer = new BufferedReader(new InputStreamReader(is))) {
        source = buffer.lines().collect(Collectors.joining("\n"));
    }
    templateLoader.putTemplate("template", source);
    config.setTemplateLoader(templateLoader);
    config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    config.setObjectWrapper(new BeansWrapper(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS));
    config.setWhitespaceStripping(true);

    try (Writer writer = new java.io.StringWriter()) {
        Template template = config.getTemplate("template");
        template.process(data, writer);
        return writer.toString();
    }
}
 
Example 3
Source File: FreeMarkerUtils.java    From seezoon-framework-all with Apache License 2.0 5 votes vote down vote up
public static Configuration buildConfiguration(String directory) {
	// 1.创建配置实例Cofiguration
	Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
	cfg.setDefaultEncoding("utf-8");
	ClassPathResource cps = new ClassPathResource(directory);
	try {
		cfg.setDirectoryForTemplateLoading(cps.getFile());
		return cfg;
	} catch (IOException e) {
		throw new ServiceException(e);
	}
}
 
Example 4
Source File: KurentoJsBase.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  try {
    final String outputFolder =
        new ClassPathResource("static").getFile().getAbsolutePath() + File.separator;

    Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
    cfg.setClassForTemplateLoading(KurentoJsBase.class, "/templates/");
    Template template = cfg.getTemplate("kurento-client.html.ftl");

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("kurentoUrl", kurentoUrl);

    for (String lib : kurentoLibs) {
      Writer writer = new FileWriter(new File(outputFolder + lib + ".html"));
      data.put("kurentoLib", lib);

      if (lib.contains("utils")) {
        data.put("kurentoObject", "kurentoUtils");
      } else {
        data.put("kurentoObject", "kurentoClient");
      }

      template.process(data, writer);
      writer.flush();
      writer.close();
    }
  } catch (Exception e) {
    Assert.fail("Exception creating templates: " + e.getMessage());
  }

}
 
Example 5
Source File: LogViewEndpoint.java    From spring-boot-actuator-logview with MIT License 5 votes vote down vote up
public LogViewEndpoint(String loggingPath, List<String> stylesheets) {
    this.loggingPath = loggingPath;
    this.stylesheets = stylesheets;
    fileProviders = asList(new FileSystemFileProvider(),
            new ZipArchiveFileProvider(),
            new TarGzArchiveFileProvider());
    freemarkerConfig = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
    freemarkerConfig.setClassForTemplateLoading(this.getClass(), "/templates");
}
 
Example 6
Source File: KmsService.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
private void createKurentoConf() {
  Map<String, Object> data = new HashMap<String, Object>();
  try {
    URI wsAsUri = new URI(wsUri);
    int port = wsAsUri.getPort();
    String path = wsAsUri.getPath();
    data.put("wsPort", String.valueOf(port));
    data.put("wsPath", path.substring(1));
    data.put("registrar", registrarUri);
    data.put("registrarLocalAddress", registrarLocalAddress);

  } catch (URISyntaxException e) {
    throw new KurentoException("Invalid ws uri: " + wsUri);
  }
  data.put("gstPlugins", getGstPlugins());
  data.put("debugOptions", getDebugOptions());
  data.put("serverCommand", getServerCommand());
  data.put("workspace", getKmsLogPath());

  Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
  cfg.setClassForTemplateLoading(this.getClass(), "/templates/");

  createFileFromTemplate(cfg, data, "kurento.conf.json");
  createFileFromTemplate(cfg, data, "kurento.sh");

  Shell.runAndWait("chmod", "+x", workspace + File.separator + "kurento.sh");
}
 
Example 7
Source File: FreemarkerTemplateEngine.java    From jbake with MIT License 5 votes vote down vote up
private void createTemplateConfiguration() {
    templateCfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
    templateCfg.setDefaultEncoding(config.getRenderEncoding());
    try {
        templateCfg.setDirectoryForTemplateLoading(config.getTemplateFolder());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: FreeMarkers.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static String renderString(String templateString, Map<String, ?> model) {
	try {
		StringWriter result = new StringWriter();
		Template t = new Template("name", new StringReader(templateString), new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS));
		t.process(model, result);
		return result.toString();
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 9
Source File: FreeMarkerViewResolver.java    From ask-sdk-frameworks-java with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a default config for FreeMarker which reads templates from the classpath under the path specified in the prefix
 * a prefix is specified.
 *
 * @param configuration free marker config
 * @param resourceClass class to load resources relative to
 * @return supplied configuration if not nul, otherwise a default one
 */
protected Configuration buildDefaultConfig(Configuration configuration, Class<?> resourceClass) {
    if (configuration != null) {
        return configuration;
    }
    Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
    cfg.setClassForTemplateLoading(resourceClass == null ? getClass() : resourceClass, "/");
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    cfg.setLogTemplateExceptions(true);
    cfg.setCacheStorage(NullCacheStorage.INSTANCE);
    return cfg;
}
 
Example 10
Source File: GridHandler.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
private void createRemoteScript(GridNode node, String remotePort, String remoteScript,
    String remoteFolder, String remoteChromeDriver, String classpath, BrowserType browser,
    int maxInstances) throws IOException {

  Map<String, Object> data = new HashMap<String, Object>();
  data.put("remotePort", String.valueOf(remotePort));
  data.put("maxInstances", String.valueOf(maxInstances));
  data.put("hubIp", hubAddress);
  data.put("hubPort", String.valueOf(hubPort));
  data.put("tmpFolder", node.getTmpFolder());
  data.put("remoteChromeDriver", remoteChromeDriver);
  data.put("classpath", classpath);
  data.put("pidFile", REMOTE_PID_FILE);
  data.put("browser", browser);

  // Create script for Node
  Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
  cfg.setClassForTemplateLoading(PerformanceTest.class, "/templates/");

  String tmpScript = node.getTmpFolder() + LAUNCH_SH;
  try {
    Template template = cfg.getTemplate(LAUNCH_SH + ".ftl");
    Writer writer = new FileWriter(new File(tmpScript));
    template.process(data, writer);
    writer.flush();
    writer.close();

  } catch (Exception e) {
    throw new RuntimeException("Exception while creating file from template", e);
  }

  // Copy script to remote node
  node.getSshConnection().scp(tmpScript, remoteScript);
  node.getSshConnection().execAndWaitCommand("chmod", "+x", remoteScript);
  Shell.runAndWait("rm", tmpScript);
}
 
Example 11
Source File: CrafterFreeMarkerConfigurer.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Configuration newConfiguration() {
    return new CrafterCacheAwareConfiguration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS, cacheTemplates);
}
 
Example 12
Source File: FreemarkerConfiguration.java    From crud-intellij-plugin with Apache License 2.0 4 votes vote down vote up
public FreemarkerConfiguration(String basePackagePath) {
	super(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
	setDefaultEncoding("UTF-8");
	setClassForTemplateLoading(getClass(), basePackagePath);
}
 
Example 13
Source File: FreeMarkers.java    From albedo with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static Configuration buildConfiguration(String directory) throws IOException {
	Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
	Resource path = new DefaultResourceLoader().getResource(directory);
	cfg.setDirectoryForTemplateLoading(path.getFile());
	return cfg;
}
 
Example 14
Source File: FreeMarkerViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
TestConfiguration() {
	super(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
}
 
Example 15
Source File: FreeMarkerView.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Return the configured FreeMarker {@link ObjectWrapper}, or the
 * {@link ObjectWrapper#DEFAULT_WRAPPER default wrapper} if none specified.
 * @see freemarker.template.Configuration#getObjectWrapper()
 */
protected ObjectWrapper getObjectWrapper() {
	ObjectWrapper ow = obtainConfiguration().getObjectWrapper();
	Version version = Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS;
	return (ow != null ? ow : new DefaultObjectWrapperBuilder(version).build());
}
 
Example 16
Source File: FreeMarkerViewTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
TestConfiguration() {
	super(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
}
 
Example 17
Source File: TemplateManagerTest.java    From robe with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void templateManagerWithTemplate() throws IOException {

    String content = "<p>${item}</p>";

    Template template = new Template("name", content, new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS));
    TemplateManager manager = new TemplateManager(template);


    Map<String, Object> parameters = new HashMap<>();

    String uuid = UUID.randomUUID().toString();
    parameters.put("item", uuid);

    manager.setParameter(parameters);
    Assert.assertTrue(manager.getParameter().get("item").equals(uuid));

    Writer out = new StringWriter();
    manager.process(out);

    String result = out.toString();

    Assert.assertTrue(result.equals("<p>" + uuid + "</p>"));

}
 
Example 18
Source File: FreeMarkerView.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the configured FreeMarker {@link ObjectWrapper}, or the
 * {@link ObjectWrapper#DEFAULT_WRAPPER default wrapper} if none specified.
 * @see freemarker.template.Configuration#getObjectWrapper()
 */
protected ObjectWrapper getObjectWrapper() {
	ObjectWrapper ow = obtainConfiguration().getObjectWrapper();
	Version version = Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS;
	return (ow != null ? ow : new DefaultObjectWrapperBuilder(version).build());
}
 
Example 19
Source File: FreeMarkerConfigurationFactory.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: FreeMarkerConfigurationFactory.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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);
}