File manipulation in programming using template

Recently i am working on one desktop application. The requirement is to develop ui which can generate configuration file which is used in to run one external application which is written in some other language. Config file which i have to generate is not a plain coma separated file but has complicated structure with configuration parameter. Also the application must run on Linux as well as on windows. So i decided to built with java swing. Based on my previous experience with Java swing i did easily developed user interface with java swing controls.

Now user interface is developed and i have to move on next task which is config file generation based on parameters values filled by the user. I have decided that i will use File class and generate expected file line by line with proper parameter values. In between my mentor back from his vacation and we are discussing about this task. He told me that i should not use File operation manually instead i have to use some template engine which will use source file with place-holders and i have to pass that place-holders values from the program where i am handling ui events. I have started looking for such implementation and try to implement such structure. He has also suggested some way using regular expressions.

But i have some new idea out of the box. I have started looking for libraries which is used as a template engine in Java and i found velocity template engine from Apache. The good part of Java like mature languages is they have good rich libraries. In web application if we are using MVC pattern we have to deal with templates. In Web templates are view which contains variable place holders which are passed from controller. And finally converted HTML is passed to client. But in my case it is not web application it is desktop app which i have to built. So i have started looking at velocity integration in desktop app. It is pretty simple they provide two jar files which you have to add in your project as a library. After adding velocity i have started playing with it and i find that it is not limited to html but we can write the content the way we want and put place-holder using $ sign. in face we can call member method for a object pass to it.


Consider below is the file which i have to generate.

#Database Configuration
Username: root
Password: root
Host: localhost
Database: test_app

Now i will first create velocity template for this file named with database.vm

#Database Configuration
Username: $username
Password: $password
Host: $host
Database: $database

Now from Java Code i will pass that place-holders which will be replaced when file is generated.



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s