-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
v1ll4n
committed
Jul 5, 2024
1 parent
830e44b
commit 757b222
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
desc( | ||
title: 'checking [freemaker.Template.Process directly]', | ||
type: audit | ||
) | ||
|
||
// write your SyntaxFlow Rule, like: | ||
// DocumentBuilderFactory.newInstance()...parse(* #-> * as $source) as $sink; // find some call chain for parse | ||
// check $sink then 'find sink point' else 'No Found' // if not found sink, the rule will stop here and report error | ||
// alert $source // record $source | ||
|
||
getTemplate().process() as $sink; | ||
|
||
check $sink then "Found Freemaker Process Using" else "No Freemaker Process Simple"; | ||
|
||
// the template is generate by yak.ssa.syntaxflow command line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
desc( | ||
title: 'checking [freemaker.Template.CreateProcessingEnvironment]', | ||
type: audit | ||
) | ||
|
||
// write your SyntaxFlow Rule, like: | ||
// DocumentBuilderFactory.newInstance()...parse(* #-> * as $source) as $sink; // find some call chain for parse | ||
// check $sink then 'find sink point' else 'No Found' // if not found sink, the rule will stop here and report error | ||
// alert $source // record $source | ||
|
||
getTemplate().createProcessingEnvironment() as $env; | ||
$env.process() as $sink; | ||
$env.invoke() as $sink; | ||
|
||
check $sink then "Found Freemaker CreateProcessingEnvironment invoke or process"; | ||
alert $env; | ||
|
||
// the template is generate by yak.ssa.syntaxflow command line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import freemarker.template.*; | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class FreemarkerExample { | ||
public static void main(String[] args) { | ||
// 配置 Freemarker | ||
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); | ||
try { | ||
cfg.setDirectoryForTemplateLoading(new File("src/main/resources/templates")); | ||
cfg.setDefaultEncoding("UTF-8"); | ||
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); | ||
cfg.setLogTemplateExceptions(false); | ||
cfg.setWrapUncheckedExceptions(true); | ||
|
||
// 加载模板 | ||
Template template = cfg.getTemplate("welcome.ftl"); | ||
|
||
// 数据模型 | ||
Map<String, Object> templateData = new HashMap<>(); | ||
templateData.put("user", "John Doe"); | ||
|
||
// 渲染模板 | ||
Writer out = new StringWriter(); | ||
template.process(templateData, out); | ||
|
||
// 输出渲染后的文本 | ||
System.out.println(out.toString()); | ||
|
||
} catch (IOException | TemplateException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import freemarker.template.*; | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class FreemarkerDemo { | ||
public static void main(String[] args) { | ||
// 创建和配置 Freemarker | ||
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); | ||
try { | ||
cfg.setDirectoryForTemplateLoading(new File("src/main/resources/templates")); | ||
cfg.setDefaultEncoding("UTF-8"); | ||
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); | ||
cfg.setLogTemplateExceptions(false); | ||
cfg.setWrapUncheckedExceptions(true); | ||
|
||
// 加载模板 | ||
Template template = cfg.getTemplate("helloTemplate.ftl"); | ||
|
||
// 准备数据模型 | ||
Map<String, Object> templateData = new HashMap<>(); | ||
templateData.put("name", "Freemarker User"); | ||
|
||
// 准备输出流 | ||
Writer out = new StringWriter(); | ||
|
||
// 创建 Environment 对象并处理模板 | ||
Environment env = template.createProcessingEnvironment(templateData, out); | ||
env.process(); | ||
|
||
// 输出结果 | ||
System.out.println(out.toString()); | ||
|
||
} catch (IOException | TemplateException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters