Archive for February, 2009

Groovy PowerPoint DSL

Posted on February 27, 2009. Filed under: Groovy | Tags: , , |

I’m really not shaving a yak here, but I created a PowerPoint DSL to give a presentation about DSLs! Let’s jump to the code immediately, because if you’re reading this, you must be interested in DSLs.

This is the (Groovy) DSL I wrote:

builder.slideshow(filename:'Test.ppt') {
    slide(title: 'Introduction') {
        bullet(text: 'Bullet 1')
        bullet(text: 'Bullet 2')
    }
    slide(title: 'Slide 2') {
        bullet(text: 'Bullet 3')
        bullet(text: 'Bullet 4')
    }
    slide(title: 'Example') {
        textbox("""This is a slide
With a lot of extra lines
Which make no sense
At all""")
    }
    imageslide(src:'background.png')
}

The DSL is pretty straightforward. I didn’t support all features of PowerPoint (nor would I want to, nor is it feasible), but the basics are here. The DSL currently supports all of the above:

  • Slides with titles
  • Bullets, which can be added to the slides
  • Textboxes, which are large text area’s
  • Imageslides, which are slides consisting of only images. This is to support all the ‘Beyond Bullet Points’ lovers 😉

While the DSL is far from complete (you could add individual images, position them using X,Y coordinates, add tables, add notes and more), the idea is clear: in a consistent and easy to read structure you can define all those elements you need in a powerpoint presentation. All the missing elements, like stated above, can easily be added by adding it to corresponding domain and exporter.

Domain and exporter you say? Yes! There are ofcourse multiple approaches in creating the DSL, but I’ve kept some parts apart from each other. Under the hood, the DSL is transformed into a very simple domain model, also consisting of slides, bullets, textboxes, etc. This extra layer is introduced to create a separation of concerns. Now, the only responsibility of the Builder is to build the domain model, instead of parsing the DSL and also creating the Powerpoint. The creation of the powerpoint is now delegated to a (also) simple Exporter, which understands the API of the POI library used to create the actual PowerPoint file. So, the model is built by the Groovy Builder, and, once the build phase is complete, the model is given to the exporter, which traverses the model. This makes the code cleaner and also allows easy testing of both the DSL and the Exporter. Ofcourse, I tested none, but instead relied on my excellent programming skills……(The next blog is about TDD, okay??)

This resulted in the following diagram:

Application diagram

Application diagram

I like the approach, but if you don’t, or know a better way, please let me know. I’d really appreciate feedback, since I will probably use a similar approach in the next DSL I create.

The code, and the resulting PowerPoint, can be found in the uploaded zipfile. Like I said: as always, let me know what you think!

Read Full Post | Make a Comment ( 5 so far )

Converting Groovy Objects to JSON using Grails

Posted on February 14, 2009. Filed under: Grails, Groovy, Uncategorized | Tags: , , , |

Disclaimer: this might not be the best way to do it. I’m open for improvements, but it’s a way I’m using now, and it works pretty nice!

Currenly, I’m creating an application which does some JSON communication. Since Grails already has nice support for JSON, I wanted to reuse their libraries, but, at the moment, I don’t need a complete web Grails application. So, the plan was to reuse the JSON (deep) converters to convert my Groovy classes to JSON, and work with that. I created the following small test to try to accomplish what I wanted:


import grails.converters.JSON

class Person {
String name = "erik"
int age = 10
}

println new JSON(new Person()).toString()

Well, I don’t think I’d be writing this blog entry if things were really this easy. Executing the above code fails with a nasty NullPointerException:

Caught: org.apache.commons.lang.UnhandledException: java.lang.NullPointerException
	at testjson.run(testjson.groovy:10)
	at testjson.main(testjson.groovy)

Why is this, you might ask. Well, it turns out the JSON converter is using a ConverterUtil class, which expects a GrailsApplication. When there’s no GrailsApplication, the code will fail with a NPE as a result. If you’d like to see this fixed: I’ve created a JIRA issue here.

However, I did came up with a workaround. It’s not perfect, but it works. Since the ConvertUtil is a sort of Singleton, you call it yourself, and set a DummyGrailsApplication instead. Well, instead of the dummy version, we can use a DefaultGrailsApplication instead. So, after our fix, the code looks like this:


import grails.converters.JSON
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
import org.codehaus.groovy.grails.web.converters.ConverterUtil

class Person {
String name = "erik"
int age = 10
}
ConverterUtil.getInstance().grailsApplication = new DefaultGrailsApplication()
println new JSON(new Person()).toString()

When running this, the following output is produced:

{"age":10,"class":"Person","name":"erik"}

Which is exactly what we want!

Read Full Post | Make a Comment ( 4 so far )

IntelliJ JSON Formatter Plugin 0.2

Posted on February 14, 2009. Filed under: IntelliJ JSON Formatter Plugin | Tags: , , , |

I’ve worked with my JSON Formatter plugin for a couple of days now to debug my Grails communication layer, and even though I created it myself, I want to say that I’m quite pleased with it. The only thing which annoyed me a bit is that it’s hard to see  if the JSON code is valid. Well, while it’s not 100% complete (and correct) yet, I’m happy to say that I’ve added simple validation to the Plugin, thanks to the JSON library of Bruno Ranschaert, who was quick to respond to a small bug in the ANTLR part of the JSON library. Since that’s fixed now, you can use the JSON plugin to further enjoy Grails and IntelliJ development! 🙂 (See the pictures for the feedback panel!)

picture-4

Read Full Post | Make a Comment ( None so far )

IntelliJ JSON Formatter Plugin

Posted on February 5, 2009. Filed under: Grails, IntelliJ JSON Formatter Plugin | Tags: , , , , , |

I’ve just created my first IntelliJ plugin, a JSON formatter. Since I work a lot with Groovy and Grails, and Grails supports JSON so nicely, it’s sometimes necessary to debug the JSON code going back and from the client to the Grails backend. However, since the JSON code is stripped from whitespaces, it’s a bit harder to read than a nested structure. To format the JSON, you can use various websites , but it requires a context switch (from IDE to Webbrowser), plus it requires an Internet connection. To solve this, I created an IntelliJ JSON Formatter plugin, which nicely hooks into IntelliJ. To give you an idea of what the plugin looks like, please take a look at the following image:

IntelliJ JSON Formatter plugin

This images gives an impression of a formatted piece of JSON code, and also includes brace matching and coloring thanks to the RSyntaxTextArea. The JSON code is parsed with the help of the JSON tools from Berlios.

You can install the plugin by using IntelliJ’s integrated plugin manager. If you’ve used it, please give me some feedback! I’d like it!

Read Full Post | Make a Comment ( 11 so far )

Liked it here?
Why not try sites on the blogroll...