Xzajo's Weblog

All | Java | nbxdoclet | NetBeans | Personal | Project Extensions | Sun | Testing
« Test Dependencies of... | Main | Run only tests which... »
20060907 Thursday September 07, 2006

Using velocity templates in NBM projects

Many people asked me how to use velocity template in their NetBeans module projects. So there is short example. Velocity libraries are wrapped to org-apache-velocity.nbm NetBeans module.

First time you need to install the module to NetBeans platform. Now we can start to write Velocity module. Add org-apache-velocity on classpath of module. Example class with generating code is below:

public class SimpleTemplate {  
    /** Write generated code to target document
     */
    public static void apply(Writer t,String vmTemplate,Map beans) {
        try {
            VelocityContext context = VTResourceLoader.getContext();
            for (Iterator bIt = beans.keySet().iterator() ; bIt.hasNext() ;) {
                String name = (String)bIt.next();
                Object bean = beans.get(name);
                context.put(name, bean);
            }
            
            Template template = Velocity.getTemplate(vmTemplate);
            if (t == null) {
                throw new IllegalStateException(" no target defined ");
            }
            template.merge(context, t);
            
        } catch (Exception e) {
            Logger.getAnonymousLogger().log(Level.SEVERE,"Exception during generating templates",e);
        }
    }  
}  
The velocity template is present in velocity/templates folder of layer.xml :
      <folder name="velocity"> 
        <folder name="templates">
             <file name="template.vm" url="Simple.vm"/>
        </folder>
      </folder>

Map of java objects is parameter for templates. Only one bean is used in our example:
public class SimpleBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }   
}

A template.vm example :
simple.name = $simple.name
We can write action with body:
    public void performAction() {
        // TODO implement action body
        InputOutput io = IOProvider.getDefault().getIO("template test",true);
        
        Map map = new HashMap();
        SimpleBean sb = new SimpleBean();
        sb.setName("Text");
        map.put("simple",sb);
        SimpleTemplate.apply(io.getOut(),"template.vm",map);
    }
It prints to output window transformed template:
simple.name = Text
Posted by xzajo ( Sep 07 2006, 03:56:47 PM CEST ) Permalink Comments [6]

Comments:

I have to questions about your way of integrating velocity:
  • Why do I have to register the velocity template in the nb filesystem? Why not load the template via classloader?
  • Why do you prefer to use singleton Velocity instance , and not the VelocityEngine class?
regards bernhard

Posted by Bernhard on September 08, 2006 at 01:49 PM CEST #

It is better to store templates to system filesystem because user can override them. Look at this page for example.
I choosed the variant with singleton without any intension.

Posted by Petr Zajac on September 09, 2006 at 09:03 PM CEST #

wow power leveling wow power leveling wow powerleveling wow powerleveling 搬家公司

Posted by 水无痕 on July 21, 2007 at 07:06 AM CEST #

http://www.wowgolds.co.uk wow gold http://www.wowgolds.co.uk cheap wow gold http://www.wowgolds.co.uk buy wow gold http://www.wowgolds.co.uk world of warcraft gold http://www.wowgolds.co.uk wow http://www.wowgolds.co.uk world of warcraft http://www.wowgolds.co.uk/wow-gold.html wow gold http://www.wowgolds.co.uk/warrior.html WoW Warrior http://www.wowgolds.co.uk/hunter.html WoW Hunter http://www.wowgolds.co.uk/rogue.html WoW Rogue http://www.wowgolds.co.uk/paladin.html WoW Paladin http://www.wowgolds.co.uk/shaman.html WoW Shaman http://www.wowgolds.co.uk/priest.html WoW Priest http://www.wowgolds.co.uk/mage.html WoW Mage http://www.wowgolds.co.uk/druid.html WoW Druid http://www.wowgolds.co.uk/warlock.html WoW Warlock http://www.powerleveling2.cn power leveling http://www.powerleveling2.cn powerleveling http://www.wowpowerleveling2.cn wow power leveling http://www.wowpowerleveling2.cn wow powerleveling http://www.wowpro.cn wow guides http://www.wowmmo.cn wow tips http://ggtop.nbatop.com google排名 http://ggtop.nbatop.com google左侧排名 http://ggtop.nbatop.com google排名服务 http://www.baidupaiming.net 百度推广 http://www.baidupaiming.net 百度排名 http://www.fzqsnet.cn 网站推广 http://www.thebiz8.com 商业吧 http://www.thebiz8.com/jichuang.html 机床 http://www.thebiz8.com/leddeng.html LED灯 http://www.thebiz8.com/dianchi.html 电池 http://www.thebiz8.com/suliao.html 塑料 http://www.thebiz8.com/shexiangji.html 摄像机 http://www.thebiz8.com/yimin.html 移民 http://www.thebiz8.com/jiachun.html 甲醇 http://www.thebiz8.com/ranliao.html 染料 http://www.0591qs.cn 福州热线 http://www.sportblog.org.cn 体育博客 http://www.stockblog.org.cn 股票博客 http://www.gameblog.org.cn 游戏博客 http://www.cheap-wow-gold.org.cn 魔兽博客 http://www.excamtest.cn 考试博客 http://www.carblog.org.cn 汽车博客 http://www.houseblog.org.cn 房产博客 http://www.pcblog.org.cn 电脑博客 http://powerlin518.iblog.com powerlin518 http://www.xcelwebdesign.com logo design http://www.xcelwebdesign.com website design http://www.xcelwebdesign.com web design http://www.shangbiaodesign.cn 商标设计

Posted by power leveling on March 20, 2008 at 04:23 PM CET #

http://sunnyren.weblog.com/
this blog introduces jewelry products and fashion jewelry

Posted by fashion jewelry on October 31, 2008 at 06:34 AM CET #

Post a Comment:

Comments are closed for this entry.

Calendar

RSS Feeds

Search

Links

Navigation

Referers