Thursday, June 13, 2013

Recover eclipse code templates using workspace metadata.

Have you ever lost your eclipse installation and feeling bad that you didn't backup code templates? 

Since code templates are integral part of rapid development a developer should keep his code templates under check. 
In any case if he loses his code templates and have his workspace handy then one can follow below steps to get it back. 

One may wonder where eclipse stores its code templates. Well here is the location. 
C:\Users\<user>\workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.php.ui.prefs
Note: The location can change for java and other perspectives. It may be like org.eclipse.java.ui.prefs. I leave the rest to your search within that above folder.

Open that file in your favourite text editor (mine is Notepad++. So screenshots will be in that)
Consider the line(9) where it starts with 

org.eclipse.php.ui.editor.templates


Start copying the part where it looks like above snippet and ends with  

</templates>


Copy the data into another file. 
Personally I don't prefer importing that data directly into eclipse because 
  1. Its eclipse settings file and our required format of xml may differ from raw data of eclipse 
  2. We need to take care of xml safe characters while creating a new xml for code templates 
  3. Eclipse may add other node elements, attributes etc., while generating code templates xml file so please don't try to mimic the functionality of eclipse
Since the raw data we got is not exactly we want so we plan on improving the raw data we got. So lets get started with some simple text manipulations using Regex Expressions. 
First we reorder the data which is earlier in single line.
Note: Don't forget to select Regular expression as your search mode

Match : >< 
Replace : >\r\n< 


After the above operation we can see all templates ordered nicely. (sorry for the dull image)
But there is still a problem with extra backslashes. 
So we remove them

Match : \\=
Replace : =



Add below line at the top of file to make it  as xml with specific encoding
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
Now save the file as your code templates file and try to import it into eclipse. 


I personally never tried it because I have my eclipse set and I will try it when situation demands.

Please do reply me with your experiences.

No comments:

Post a Comment