Tuesday, October 13, 2009

a case for scala

For quite some time now I have been putting off a rather simple implementation task for exposing HTML representations of entities based on an RDF/XML model in my JAX-RS application. Mainly because it either requires an XSL transformation or parsing the whole graph into an object model and using templates to create the HTML. Both cases are usually time consuming and somewhat unexciting. I have learned to like the idea of being able to use XML as a first-class citizen in the progamming language when I did some work with ActionScript 3 recently. So I decided to finally give Scala a chance and it turned out to integrate really well. The code snippet below actually shows a Scala class extending a Java class and overriding a Java method, both tagged with JAX-RS annotations. Path and resource names have been changed.
@Path("/Resource")
class HtmlResourceController extends AbstractResourceController {
   @GET
   @Path("/{id}")
   @Produces(Array(MediaType.TEXT_HTML))
   override def findResourceById(@PathParam("id") id:String) : Response = {
      val rdfXmlRes = super.findResourceById(id);    
      ...
   }
}
The Array declaration within @Produces is only needed for the scala compiler. Within this method I now get the full and yet easy to use XML processing power of Scala, as described here: Scala and XML.

The drawbacks are the additional dependency to Scala and having to maintain two programming languages within the same project but I can live with that for now, especially as it gives me access to a whole set of interesting language features like mixins, closures, currying, and so forth.