Total Pageviews

2020/02/04

[Java] [Freemarker] Generate Text file from FTL - example 2

Requirement
I would like generate text file as bellows:
FreeMarker Template example: Hello World!

        ===================(Page:0001)
        ======  County List   ======
        ========================
        1. 台灣
        2. Japan
        3. The Netherlands


        ===================(Page:0002)
        ======  County List   ======
        ========================
        4. United Kingdom
        5. France
        6. Canada


        ===================(Page:0003)
        ======  County List   ======
        ========================
        7. United States
        8. Italy

------------------------------
Number of Record(s): 0000000008
Print date: 19 Nov 19

FTL file
<#-- 設定 locale,讓日期印出來是英文
     https://freemarker.apache.org/docs/ref_directive_setting.html  -->
<#setting locale = "en_US">

<#-- 宣告變數用來儲存總筆數與頁數
     https://freemarker.apache.org/docs/ref_directive_assign.html -->
<#assign total = 0>
<#assign page = 1>

<#-- 取得現在日期 -->
<#assign dateTime = .now>
<#assign date = dateTime?date>

FreeMarker Template example: ${message}

<#list countries as country>
    <#-- 一頁三筆資料 -->
    <#if total % 3 == 0>
        <#-- 第二頁開始才在表頭增加兩行空白 -->
        <#if page gt 1>


        </#if>
        <#-- 將 page number 格式化 -->
        ===================(Page:${page?string["0000"]})
        ======  County List   ======
        ========================
        <#assign page = page + 1>
    </#if>
        ${country_index + 1}. ${country}
        <#assign total = total + 1>
</#list>

------------------------------
Number of Record(s): ${total?left_pad(10, "0")}
<#-- https://freemarker.apache.org/docs/ref_builtins_date.html -->
Print date: ${date?string["dd MMM yy"]}


Test Case
package com.test.batch;

import com.test.batch.exception.FtlException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class FtlTest {

    private Configuration cfg;

    @Before
    public void init() {
        cfg = new Configuration();
        // 設定到 classpath 讀取 ftl file
        cfg.setClassForTemplateLoading(this.getClass(), "/");
    }
    
    @Test
    public void testCountriesFtl() {
        try (Writer file = new FileWriter(new File("C:/ftl_countries.txt"));) {
            Template template = cfg.getTemplate("ftl/countries.ftl");

            Map<String, Object> data = new HashMap<>();
            data.put("message", "Hello World!");

            List<String> countries = new ArrayList<String>();
            countries.add("台灣");
            countries.add("Japan");
            countries.add("The Netherlands");
            countries.add("United Kingdom");
            countries.add("France");
            countries.add("Canada");
            countries.add("United States");
            countries.add("Italy");

            data.put("countries", countries);

            template.process(data, file);
        } catch (IOException | TemplateException e) {
            throw new FtlException("fail to generate file from ftl file : " + e.getMessage(), e);
        }
    }

}



No comments: