Saturday 4 July 2020

FreeMarker: String slicing

FreeMarker supports string slicing.

 

For example,

s = "ABCDEFGHIJ"

 

${s[2..5]} =  CDEF

${s[2..<5]} =  CDE

${s[2..*3]} =  CDE

${s[2..*100]} =  CDEFGHIJ

${s[2..]} =  CDEFGHIJ

 

 

Below table specifies different ways to specify ranges.

 

Range Expression

Description

Example

start..end

Range from star number to end (inclusive)

For a string s = "ABCDEFGHIJ", s[2..5] return CDEF.

start..<end or start..!end:

Range from start number to end (exclusive)

For a string s = "ABCDEFGHIJ", s[2..<5] return CDE.

start..*length

Range from start to length number of items.

For a string s = "ABCDEFGHIJ", s[2..*3] return CDE.

start..

Unbounded range.

For a string s = "ABCDEFGHIJ", s[2..] return CDEFGHIJ.

 

Find the below working application.

 

Step 1: Define stringSlice.ftl file under src/main/resources/templates folder.

 

stringSlice.ftl
<#assign s = "ABCDEFGHIJ">

s = ${s}
${r"${s[2..5]} = "} ${s[2..5]}
${r"${s[2..<5]} = "} ${s[2..<5]}
${r"${s[2..*5]} = "} ${s[2..*5]}
${r"${s[2..*100]} = "} ${s[2..*100]}
${r"${s[2..]} = "} ${s[2..]}

Step 2: Define ‘FreeMarkerUtil’ class that takes model object and template file as input and merge them.

 

FreeMarkerUtil.java
package com.sample.app.util;

import java.io.StringWriter;
import java.util.Locale;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class FreeMarkerUtil {

	private static final Configuration FREE_MARKER_CONFIGURATION = new Configuration(Configuration.VERSION_2_3_30);

	static {
		FREE_MARKER_CONFIGURATION.setClassForTemplateLoading(FreeMarkerUtil.class, "/templates/");
		FREE_MARKER_CONFIGURATION.setDefaultEncoding("UTF-8");
		FREE_MARKER_CONFIGURATION.setLocale(Locale.US);
		FREE_MARKER_CONFIGURATION.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		FREE_MARKER_CONFIGURATION.setFallbackOnNullLoopVariable(false);
	}

	public static StringWriter mergeModelAndTemplate(Object modelObject, String ftlFile) throws Exception {
		StringWriter stringWriter = new StringWriter();

		Template template = FREE_MARKER_CONFIGURATION.getTemplate(ftlFile);

		template.process(modelObject, stringWriter);

		return stringWriter;
	}

}

Step 3: Define StringSlicePopulator.

 

StringSlicePopulator.java
package com.sample.app;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import com.sample.app.util.FreeMarkerUtil;

public class StringSlicePopulator {
	public static void main(String args[]) throws Exception {

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

		StringWriter stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "stringSlice.ftl");
		System.out.println(stringWriter.toString().trim());

	}
}

Output

s = ABCDEFGHIJ
${s[2..5]} =  CDEF
${s[2..<5]} =  CDE
${s[2..*5]} =  CDEFG
${s[2..*100]} =  CDEFGHIJ
${s[2..]} =  CDEFGHIJ



Previous                                                    Next                                                    Home

No comments:

Post a Comment