Migrate to Freemarker from Velocity template engine in your NetBeans Module


In Portal Pack Plug-ins, earlier Velocity template engine was used for all type of code generations and templating. But after going through some NetBeans doc I found that NetBeans 6 (Currently under development) bundles FreeMarker template engine with it. But FreeMarker apis are not exposed directly rather those can be used through JSR 223 Library Integration (JSR223: Scripting for Java Platform) in NetBeans.


You can follow the below steps for a easy migration from Velocity to Freemarker template engine in your module

  • Convert all your velocity templates to equivalent Freemarker templates. There is a command line utility to convert velocity templates to Freemarker templates automatically. For more details click here.
  • Change your module's layer.xml as below to mention the "javax.script.ScriptEngine" attributes as "freemarker" for your template.
                            <folder name="mytemplatefolder">
                                  <folder name="templates">
                                         <file name="mytemplate.java" url="nbresloc:templates/mytemplate.template">
                                            <attr name="position" intvalue="0"/>
                                            <attr name="javax.script.ScriptEngine" stringvalue="freemarker"/>
                                        </file>
                                  </folder>
                            </folder>
  • Add JSR 223 Integration to Module Dependencies
  •  To get the template FileObject  add/use following two methods in your module's code
                      public static FileObject getTemplateFile(String name) {
                            FileObject fo = getFolder() != null ? getFolder().getFileObject(name) : null;
                            return fo;
                      }

                      public static FileObject getFolder() {
                            if (folder == null) {
                                folder = Repository.getDefault().getDefaultFileSystem().findResource(templateFolder);
                            }
                            return folder;
                    }

         In the above example templateFolder value should be "mytemplatefolder/templates"
  •  Call following method on template FileObject to merge and create the new file

            public final DataObject createFromTemplate(DataFolder f, String name, Map  parameters)

  • You can also directly use the JSR 223 API for template processing and merging

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

ranjansatya

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today