Skip to Content
In the previous blog (An introduction to Scala, part 1), you saw how Scala could do similar things to Java (even compiling to the same bytecode), but in a more compact and readable form. In this blog you’ll learn more about some of the things that make Scala unique as we write an application to read XML feeds from Twitter and SDN, using them to produce a web page with a list of blog entries written by Twitter friends. We’ll step through the code a few lines at a time so you can see how the whole thing is built up. At the start of our application, we declare the package name and the list of imports, just like Java. | image | You can see that Scala does +not+ require a semicolon at the end of each line. The underscore character “_” is Scala’s wildcard character, so the first import gives access to all the classes in package +scala.xml+. The second import shows a handy bit of Scala syntax to allow access to several named classes in the +java.net+ package without requiring a separate line for each one. Next, we declare our class along with the main() method which will be executed when we run our program from the command line. | image | In this case, we are declaring a singleton class, which in Scala is declared as an object. Next, we declare the main() method with the command-line arguments being passed in as an array of strings. So, that’s the language-level housekeeping out of the way, and it’s time to write some useful code. Let’s load up the XML feed for SCN blogs. | image | There are a few interesting things happening in this few lines of code. First of all, we’re declaring a new value, +sdnBlogs+, to hold the result of the following code block. “val” declares an entity which, once assigned, cannot be reassigned. This is just like using the “final” qualifier in Java. The code block (in braces after the = sign) is basically an in-line anonymous function, where the result of the last line is returned as the function value. So this is what the whole snippet of code does: defines the URL of the SCN blogs’ RSS feed; opens a connection to that URL; reads the data from that connection into a Scala XML object (class +scala.xml.Elem+); and assigns that object to the identifier +sdnBlogs+. Incidentally, if this XML was stored in a local file instead of a remote web server, the whole three-line block would be replaced with just one line of code:This is basically the same code as we used to get the SCN blog feed, except for a few lines of {code:html}boilerplate code converted from Java{code} which we use to set our Twitter login credentials.This one line of code does quite a lot of work, so let’s break it down into chunks. +val friendNames+ you should be familiar with by now – that declares an entity to hold the result of what comes next.First we define a for() loop. +twitterFriends”user”+ is the syntax to get a sequence of nodes from the +twitterFriends+ XML object. +val user <-+ then binds the value +user+ to each node in the sequence as the for() loop runs. The yield keyword after the loop definition tells Scala, for each iteration of the loop, to add the result of the following expression to a list. This expression is +(user”name”).text+, which gets us the inner text of the element in our XML, and returns the result as a list of String objects into the value +friendNames+. Our next chunk of code loops through the items in the +sdnBlogs+ XML object which have a This bit of code starts off in a similar way to the previous chunk – declaring a result object and kicking off a for() loop. There’s a slight difference to the for() syntax here – instead of +for(elem <- collection)+ we have +for (elem <- collection; condition)+. This formulation only iterates over the members of the collection that satisfy the condition (rather like a WHERE clause in SQL). In this case, the condition is +friendNames.contains((item”creator”).text)+ – in other words, only perform this iteration if the blog item’s creator is in our list of friends.This is pretty straightforward now that we know we can use raw XML in our Scala – in fact we could just copy and paste the code from the HTML editor of our choice and insert +{friendBlogs}+ at the point where we want our links to appear. Finally, just for polish, we’ll format and indent the HTML using a pretty printer object and print it out to the console: | image | When compiled and run: | image | Our program produces similar HTML to this (depending on who your Twitter friends are, and how many blogs they’ve written recently): | image
To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. Richard Hirsch
    Hi,

    Looks cool and it appears to be very compact. Have you had any luck embedding this in NetWeaver? Is there any Ajax-UI packages for Scala as well?

    Dick

    (0) 
  2. Richard Hirsch
    Hi,

    Looks cool and it appears to be very compact. Have you had any luck embedding this in NetWeaver? Is there any Ajax-UI packages for Scala as well?

    Dick

    (0) 
    1. Darren Hague Post author
      Hi Dick,

      Using Scala with NetWeaver is coming in a future blog, when I’ll discuss the web development framework for Scala: /lift/.  Right now though, Scala code can be compiled to Java 1.4 bytecode, and will therefore run on NetWeaver Java. I’ve not tried it yet, but the code above could just as easily extend javax.servlet.http.HttpServlet and implement doGet() instead of implementing main() – that should give a functioning web application which can be deployed to NetWeaver Java. I leave that as an exercise for the reader. 🙂

      (0) 

Leave a Reply