Combine power of Groovy closures and Java.

Groovy closure is a great tool for development. There is no smoother integration than that of groovy and POJO's. Groovy closures can be put to used for simple tasks and can reduce a developer's time drastically. The best part of this integration, one does not have to do understand a schema or DTD or learn a new XML language. This post will focus on getting a groovy DSL working for a file manipulation.

Problem statement Develop a system that will have to get an input directory from a configuration file. Iterate the directory and identify folders that have project documents. After identifying the project folders and documents, rename all information underneath folder to html format.


Solution for problem Identify best possible uses of Groovy script to reduce programmer's overload. Develop a Java solution when applicable, invoke the Java class from script. Integrate both solution and pass final information to any web app for display processing.



Assumption User had installed Groovy and is good to go with an IDE that can run groovy scripts.




Tasks for Solution





  • Define a closure

  • Define and externalize the input directory that has to be processed.

  • Integrate properties file information into the closure

  • Delete all pre-processed html files

  • Create a Java class that will use Java's regex to identify directories that satisfy business condition

  • return a collection of directories that will have to processed.

  • Install office-groovy-com integration plugin

  • Open word documents

  • Convert word documents to html

  • Exit gracefully


Define a closure:


According to Groovy website closure is defined as "A Groovy Closure is like a "code block" or a method pointer. It is a piece of code that is defined and then executed at a later point. It has some special properties like implicit variables, support for currying and support for free variables (which we'll see later on)."  It is code block that is bit quirky to learn, but some  serious effort is worth every bit of it. Here in the example we will define an external directory that has some *.doc files that will be opened and converted to an html file.


Define and externalize the input directory that has to be processed:


Based on the OS you are using define a directory and copy some *.doc files.


We will define a directory /usr/sats/Sathish Documents, we will store a document first.doc. We will define a properties file that will have directory information and starting writing a closure.


Make an  entry "PATH=/usr/sats/documents" in a properties file call it "config.properties"


Define a closure now:




As one can see to define the closure just type def <closure name>


The first task performed in code is open a properties file and read the entry that we put earlier. Done now have externalized and created a groovy closure. How tough was that :)


Now we have accomplished some milestones, let us concentrate on some house cleaning. We will delete all html files from the directory if some exists. The code for this is




delhtmlclos = {


it.eachDir(delhtmlclos)


def htmlpattern = ~/.*.html/


it.eachFileMatch(htmlpattern) {


println("Going to delete" + it.toString())


it.delete()


}


}




eachDir is a shortcut closure method that instructs compiler to loop all the working directories that was mentioned. We will see later how to call this closure. Next we are defining the pattern of files that has to be deleted and then we start deleting the files that matches the condition. Imagine the amount of lines that you have saved from a similar Java code base.



Next block of code, one can see how easy to invoke native Java api's




def props = new Properties();


props.load(this.getClass().getClassLoader().getResourceAsStream("config.properties"))


def fileUtil = new com.company.foldersutility.FilterDirectoryUtil().retFilteredDirectory(props.get("PATH"))




FilterDirectoryUtil is a native java class that we will look soon, but developer does not have to do anything to invoke this class from a groovy script. One cannot stop appreciating the power from Groovy!!!


Finally continuing what we have been doing,




fileUtil.each {filename ->


println(filename)


def dirName = new File(filename)


delhtmlclos(dirName)


def p = ~/.*.doc/


dirName.eachFileMatch(p) {


/**


* Groovy has great support from a MS office plugin. This will convert word document


* to a html file.


*/


f ->


def word = new ActiveXObject("Word.Application")


word.Documents.Open(f.toString())


word.ActiveDocument.SaveAs(new File("${f.toString() - ".doc"}.html").canonicalPath, 8)


word.Quit()


}


}




there is a ready-made plugin for manipulating ms-office files. There is a lot of information on this plug-in (click).



The above code invokes the delhtmlcos that we defined earlier to delete all existing html files and continues with the main duty of opening ms-office document file and saving those files as a html. Remember we are not returning anything from this script. This is an isolated task for the script. This script can be easily invoked from a java class. Before we close this discussion here is the FilterDirectoryUtil class.



This class uses Java regex to parse files. The files parsed using this regex will end with an integer value.


That's all there to be for this discussion. If something is missed out, send me a note.