]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.core/src/org/argeo/fm/TestFreeMarker.java
4 import java
.io
.FileWriter
;
5 import java
.io
.IOException
;
7 import java
.util
.HashMap
;
10 import freemarker
.template
.Configuration
;
11 import freemarker
.template
.Template
;
12 import freemarker
.template
.TemplateExceptionHandler
;
14 public class TestFreeMarker
{
15 static String base
= System
.getProperty("user.home") + File
.separator
+ "dev" + File
.separator
+ "work"
16 + File
.separator
+ "ftl";
17 static Configuration cfg
;
20 cfg
= new Configuration(Configuration
.VERSION_2_3_28
);
21 cfg
.setDirectoryForTemplateLoading(new File(base
));
22 cfg
.setDefaultEncoding("UTF-8");
23 cfg
.setTemplateExceptionHandler(TemplateExceptionHandler
.HTML_DEBUG_HANDLER
);
24 cfg
.setLogTemplateExceptions(false);
25 cfg
.setWrapUncheckedExceptions(true);
26 } catch (IOException e
) {
27 // TODO Auto-generated catch block
32 public static void main(String
[] args
) {
33 if (args
.length
== 0) {
34 System
.err
.println("Usage: <template name> (in " + base
+ ")");
36 String template
= args
[0];
38 /* Create a data-model */
39 Map
<String
, Object
> root
= new HashMap
<>();
40 root
.put("user", "Big Joe");
41 Product latest
= new Product();
42 latest
.setUrl("products/greenmouse.html");
43 latest
.setName("green mouse");
44 root
.put("latestProduct", latest
);
46 /* Get the template (uses cache internally) */
47 Template temp
= cfg
.getTemplate(template
);
49 /* Merge data-model with template */
50 String target
= base
+ File
.separator
+ template
+ ".html";
51 Writer out
= new FileWriter(target
);
52 temp
.process(root
, out
);
54 System
.out
.println("Wrote " + target
);
55 } catch (Exception e
) {