Andrey Hihlovskiy

Professional blog on groovy, gradle, Java, Javascript and other stuff.

Tag Archives: groovy

Power of switch statement in groovy

Very impressive (and expressive):

def x = 'test'

switch(x) {
  case null:
    println 'null!'
  case ~/(?i)Test/:
    println 'got it!'
    println 'something else'

here second ‘case’ does case-insensitive regex comparison. In general, case may contain any regex, collection, range or class.


Change in JDK 1.7.0_25 breaks all grails applications

Change in JDK 1.7.0_25 breaks all grails applications:
I just tried grails 2.2.3 – the problem is still not fixed, the application fails to start with an error message “Could not determine Hibernate dialect for database name [H2]”
Any workarounds suggesting to replace hibernate libraries don’t work.
The following additional parameter allows to start grails:

grails -noreloading run-app

But hey, it’s no fun to start in no-reloading mode! Oracle, but things back!

A happier, groovier way to parse RTF: apache_tika + XmlSlurper

I discovered a new, easier way to parse RTF in java/groovy programs. Consider the following sequence:

1. Instantiate XmlSlurper

2. Instantiate RTFParser (of Apache Tika)

3. Parse RTF (either file or string), passing XmlSlurper to RTFParser (such passing is possible, because RTFParser expects ContentHandler interface, which is implemented by XmlSlurper).

4. Traverse RTF content groovy-style: each, find, findAll, etc.

The example:

Disappointment with groovy/XmlSlurper

Greatest disappointment with groovy/XmlSlurper: it does not read/interpret XML comments. Quite critical for massive XML processing/transformations, when it is necessary to keep change delta to minimum.
In the last project I had to recede to JDOM2 – it reads, interprets and writes XML comments without problems. Sad, volume of code doubles compared to XmlSlurper.

groovy XmlParser and XmlSlurper

I am absolutely astonished by functionality of groovy classes XmlParser and XmlSlurper. The both are similar to each other, with one important difference: XmlParser is more DOM-like (all data in memory), while XmlSlurper is more SAX-like (data parsing delayed until needed).
The most charming thing is how both work together with closures, regexps and collection methods. I am seriously thinking about shifting all XML-specific code to these facilities.

gradle snippet: sources and javadoc jar for every subproject of given project

gradle snippet: sources and javadoc jar for every subproject of given project

Add this to the parent “build.gradle” to enable xyz-sources.jar and xyz-javadoc.jar generation for every java and groovy subproject of the given parent project. The script tolerates non-java subprojects.