Saturday, October 4, 2008

Groovy as extension for Netbeans editor

As you know I work a lot with xml files (see my Xml/Xsl posts). Xml files are perfect for storing informations in structured way. Sometimes there is need for refactoring those informations. By refactoring I mean nodes/attributes renaming, changing value or place of those. If file is too big, that task may be very-very boring doing it by hand. Of course you can use your favorite text editor's search-replace feature but for complex tasks you will end writing utility for processing xml.
I played a little bit with Groovy and GroovyShell and noticed that you can easily pass your (java) variables to it. It means that you can pass your editor's document and process it with Groovy.
I created simple module for processing xml files. It is based on XPathEvaluator module. It adds a new window to Netbeans where you can write Groovy scripts.

Code for "Run Script" button looks like this:
Binding binding = new Binding();
binding.setVariable("doc", xmlDoc);
binding.setVariable("output", jTextArea1);
GroovyShell shell = new GroovyShell(binding);

It binds xmlDoc, which is EditorCookie.getDocument(), to doc variable and evaluates the script which is in jEditorArea.

Maybe a Groovy could be a new way for extending Netbeans? Tell me what you think.