Pages

Wednesday, December 30, 2009

A Couple Of GroovyConsole Jiras

I find myself relying more and more on the GroovyConsole for testing out Java and Groovy stuff (especially regular expressions and xml processing), for writing scripts it basically is my IDE. There's a few irritations for which I opened a couple of Jiras for today, as I didn't see comments on them. All pretty minor, the first two deal with the open/save dialogs, the third deals with process management and output. We'll see what happens. If they're open to the changes, but don't have the time, a couple might be something even I could do. Anyway, here they are:

http://jira.codehaus.org/browse/GROOVY-3961
http://jira.codehaus.org/browse/GROOVY-3962
http://jira.codehaus.org/browse/GROOVY-3963

Monday, December 21, 2009

Gettin' Groovy with xml validation

So, I wanted to be able to tell learn some things about the validity of a particular xml document I was working with a few days ago. If you use something like XMLSpy or the xml plugin for Notepad++, they will display only the first validation error. So, I wrote a Groovy script to let me control the amount of errors reported, and collect some statistics about the document. I call it ValidateMe. And now, I'm sharing it with you. You can get the script off its Google Code page. As usual, it's MIT licensed, so you can do pretty much whatever you want with it. It's usage is described by running 'groovy validateMe.groovy help'. It's pretty straightforward, about 130 lines or so, but I think it's pretty slick.

As a result of doing this, I also learned that you can have multiple classes in the same .groovy script file, as long as the class with the main method is first.

Wednesday, December 16, 2009

Launching a GroovyConsole Without a cmd window

I run Groovy 1.7 from the .zip files (which doesn't yet have the native launchers built for it), and I love the line numbering, and many other things about it. The one thing that was irritating is that a new cmd window would have to be opened every time I launched the groovyConsole.bat. I now have a workaround. Create a new .vbs file in the bin folder of your groovy with the following contents:
Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("groovyconsole.bat", 0)
set WshShell = Nothing
You can then put a shortcut to this wherever, and even make it pretty by setting the icon to this. Oh, and make sure you have your GROOVY_HOME set up.

Actually, this can be used to run any batch script in the background, as long as you don't need to be able to let the user pass in arguments. My thanks to Koushik Biswas from Yahoo Answers for the tip.

Friday, December 11, 2009

Which Child Am I?

I don't know if this is worth blogging home about, but I've needed this solution a couple times, and I found myself referencing this blog entry draft, so I'll put it out there and maybe someone else will find it useful. The problem is I have an xml node, and I want to know the order it is (as an index integer) in relation to its siblings. You may have to modify this of course, if the children you are comparing are deeper down than 1.

def CAR_RECORDS = '''
<records>
  <car name='HSV Maloo' make='Holden' year='2006'>
    <country>Australia</country>
    <record type='speed'>Production Pickup Truck with speed of 271kph</record>
  </car>
  <car name='P50' make='Peel' year='1962'>
    <country>Isle of Man</country>
    <record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
  </car>
  <car name='Royale' make='Bugatti' year='1931'>
    <country>France</country>
    <record type='price'>Most Valuable Car at $15 million</record>
  </car>
</records>'''

def records = new XmlSlurper().parseText(CAR_RECORDS)
def record = records.car[2].country

int index = 0
Boolean found = false
record.parent().parent().children().each {
  if (it == record.parent()) {
    found = true
  } else if (!found) {
    index++
  }
}
assert index == 3
return index

Monday, December 7, 2009

Using Groovy from the .zip file

I had always had trouble launching groovysh or groovyConsole from the .zip releases of groovy, and always waited until they released the installer for Windows, never knowing why. There is a simple fix, but one that didn't occur to me right away. The cause is that the GROOVY_HOME variable needs set before startGroovy.bat tries to add it to the classpath, so just add

set GROOVY_HOME=..\
to startGroovy.bat (anywhere before the classpath gets set), and let the good times roll...
This also, of course, overrides whatever you have set as system or user variables, so you can safely play with other versions from the .zips without needing to change anything (or needing admin rights).

Thursday, December 3, 2009

My Second Wave Bot

Inspired by Piratify, I decided to make a bot that makes everyone talk like Yoda (Yodaspeak as I call it). The bot lives at yodaspeakify.appspot.com, and can be added by adding yodaspeak@appspot.com to your wave. The sourcecode is available here. It works, but needs more work to improve its results.