Thursday, 2 July 2020

FreeMarker: Working with Ranges

Ranges are used to define sequences. Ranges can be defined by specifying what ranges of whole numbers it contains.

 

Ranges are specified using .. operator.

 

Example.

2..5 gives [2, 3, 4, 5]

 

Below table specifies different ways to specify ranges.

 

Range Expression

Description

Example

start..end

Range from star number to end (inclusive)

1..4 gives [1, 2, 3, 4]

4..1 gives [4, 3, 2, 1].

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

Range from start number to end (exclusive)

1..<4 gives [1, 2, 3]

4..<1 gives [4, 3, 2]

start..*length

Range from start to length number of items.

10..*4 gives [10, 11, 12, 13],

10..*-4 gives [10, 9, 8, 7]

start..

Unbounded range.

1.. gives [1, 2, 3,... ], up to infinity.

 

Find the below working application.

 

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

 

range.ftl
${"Printing 1..4"}

<#list 1..4 as item>
Value of element is : ${item}
</#list>

${"Printing 4..1"}

<#list 4..1 as item>
Value of element is : ${item}
</#list>

${"Printing 1..<4"}

<#list 1..<4 as item>
Value of element is : ${item}
</#list>

${"Printing 4..<1"}

<#list 4..<1 as item>
Value of element is : ${item}
</#list>

${"Printing 10..*4"}

<#list 10..*4 as item>
Value of element is : ${item}
</#list>

${"Printing 10..*-4"}

<#list 10..*-4 as item>
Value of element is : ${item}
</#list>

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 RangePopulator.

 

RangePopulator.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 RangePopulator {
	public static void main(String args[]) throws Exception {

		Map<String, Object> modelObject = new HashMap<String, Object>();
		
		StringWriter stringWriter = FreeMarkerUtil.mergeModelAndTemplate(modelObject, "range.ftl");
		System.out.println(stringWriter.toString().trim());

	}
}

Output

Printing 1..4

Value of element is : 1
Value of element is : 2
Value of element is : 3
Value of element is : 4

Printing 4..1

Value of element is : 4
Value of element is : 3
Value of element is : 2
Value of element is : 1

Printing 1..<4

Value of element is : 1
Value of element is : 2
Value of element is : 3

Printing 4..<1

Value of element is : 4
Value of element is : 3
Value of element is : 2

Printing 10..*4

Value of element is : 10
Value of element is : 11
Value of element is : 12
Value of element is : 13

Printing 10..*4

Value of element is : 10
Value of element is : 9
Value of element is : 8
Value of element is : 7






Previous                                                    Next                                                    Home

No comments:

Post a Comment