Sunday 14 June 2020

FreeMarker: Escaping Sequences

An escape sequence is a sequence of characters that doesn't represent itself when used inside string literal or character. It is composed of two or more characters starting with backslash \.

 

For example:

\n represents new line.

\t represents tab

 

Example 1: Below statement use \n

Hello ${"How are you? \nI am fine, Thank you."}

 

Example 2: Below statement prints the string 'quoted' in double-quotes.

${"It's \"quoted\" and this is a backslash: \\"}

 

Below table summarizes the escape sequences supported by FreeMarker.

 

Escape Sequence

Description

\"     

Quotation mark (u0022)

\'

Apostrophe (a.k.a. apostrophe-quote) (u0027)

\{     

Opening curly brace: {

\=    

Equals character: =

\\     

Back slash (u005C)

\n

Line feed (u000A)

\r     

Carriage return (u000D)

\t      

Horizontal tabulation (a.k.a. tab) (u0009)

\b     

Backspace (u0008)

\f      

Form feed (u000C)

\l      

Less-than sign: <

\g     

Greater-than sign: >

\a     

Ampersand: &

\xCode      

 

Character given with its hexadecimal Unicode code (UCS code). The Code after x represents 1 to 4 hexadecimal digits.

 

Example

"\xA9 1999-2001",

"\x0A9 1999-2001",

"\x00A9 1999-2001"

 

Find the below working application.

 

Step 1: Define ‘escapeSequence.ftl’ file under src/main/resources/templates folder.

 

escapeSequence.ftl
Hello ${"How are you? \nI am fine, Thank you."}

${"It's \"quoted\" and this is a backslash: \\"}

Step 2: Define ‘FreeMarkerUtil’ class that take model class 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 EscapeSequencePopulator.

 

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

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

	}
}

Run the application, you will see below messages in console.

Hello How are you? 
I am fine, Thank you.

It's "quoted" and this is a backslash: \



Previous                                                    Next                                                    Home

No comments:

Post a Comment