package com.venustech.generate;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
public class GenerateRule {
public void generateMain() throws IOException, TemplateException {
/* 在整个应用的生命周期中,这个工作你应该只做一次。 */
/* 创建和调整配置。 */
Configuration cfg = new Configuration();
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("myTemplate", "rule " +
"<#if action??> " +
" ${action} " +
"</#if> " +
"<#if protocol??> " +
" ${protocol} " +
"</#if> " +
"<#if source_ip_address??> " +
" <#if source_wildcard??> " +
" source ${source_ip_address} ${source_wildcard} " +
" </#if> " +
"</#if> " +
"<#if destination_ip_address??> " +
" <#if destination_wildcard??> " +
" destination ${destination_ip_address} ${destination_wildcard} " +
" </#if> " +
"</#if> " +
"<#if operator??> " +
" <#if destination_port??> " +
" destination_port ${operator} ${destination_port} " +
" </#if> " +
"</#if>");
cfg.setTemplateLoader(stringLoader);
Template temp = cfg.getTemplate("myTemplate","utf-8");
/* 创建数据模型 */
Map root = new HashMap();
root.put("action" ,"deny" );
root.put("protocol" ,"udp" );
root.put("source_ip_address" ,"192.168.56.123" );
root.put("source_wildcard" ,"0.0.0.255" );
root.put("destination_ip_address" ,"192.168.56.123" );
root.put("destination_wildcard" ,"0.0.0.255" );
root.put("operator" ,"gt" );
root.put("destination_port" ,"128" );
Writer out = new StringWriter(2048);
temp.process(root, out);
System.out.println(out.toString().replaceAll("[nr]", ""));
out.flush();
}
public static void main(String[] args) throws Exception {
GenerateRule html = new GenerateRule();
html.generateMain();
}
}
|