Blog

Find As You Type in Italian

Thanks to the great work of Christian Liensberger, who previously did the German translation as well, Find As You Type is now available in Italian.

Enjoy!

Categories: Software
Posted on: 2007-11-23 11:51 UTC. Show comments (2)

Aikido

One of the things I always planned to do in Japan was Aikido. I have done Aikido for several years in the Netherlands, unfortunately a traffic accident almost three years ago permanently injured my foot which means that I've not been able to train properly since then. I'd done several attempts to start again, the latest actually rather successful as long as I didn't overexert my foot. And so I planned to also try to do it here.

But since I had enough new stuff coming at me the first period I was here, I didn't. And the longer you stay away, the harder it is to get started again. Uncertainty about my foot also kept me from actively pursuing it.

But recently I've been thinking about it a lot, and I really do want to try it again, even if I have to take it slow. So, it was time for some decisive action.

As of yesterday, I am a member of the Aikido Aikikai Hombu Dojo, the world headquarters of Aikido, located in Shinjuku. I plan to go training for the first time on Thursday (the earliest I can fit it in my schedule). Naturally, I'm quite nervous; I haven't trained for so long and there's no telling how my foot will hold out. Also it's the first time training in Japan, and at the Hombu Dojo no less. But on the other hand I'm also very much looking forward to standing on the tatami again.

Can't wait till Thursday!

Categories: Personal, Japan
Posted on: 2007-11-13 11:35 UTC. Show comments (1)

Blast from the past

For a joke on Channel9 I needed a screenshot of Channel9 in Internet Explorer on Windows 3.11, and for a laugh I also decided to make a screencast of setting up Windows 3.11 in Virtual PC. It's not very useful, but maybe a bit of nostalgia for people who used this OS. :)


Watch the video on YouTube

The cursor is a bit wonky at times. It seems Windows Media Encoder (which I used to take the screencast) shows Vista's own cursor even when the cursor is captured by Virtual PC and isn't actually visible. This is why you get an additional cursor when I'm using the mouse in the Virtual PC. Nothing I can do about that, though.

Categories: General computing, Random stuff
Posted on: 2007-11-07 04:30 UTC. Show comments (0)

Lectures

One notable difference between doing a PhD here and doing it in the Netherlands is that here, there's a requirement to get some credits from taking lectures. I am required to get 8 credits, which by whatever system they're using (I'm not sure what it is, it's definitely different from ECTS though) comes down to four courses.

This isn't a problem of course. I have registered for four courses this semester so if I get credits for them all I'll be done with it. One factor that makes this more interesting is that most lectures are given in Japanese. Only one out of the four lectures I'm taking is in English. So can I understand those lectures in Japanese? Not really. One of them is okay because he has slides that are in English, but the other two are not so easy. But it doesn't really matter, I'll just have to attend the lectures and submit a paper (which I can write in English) and I'll get the credits. Sure, there are more useful ways I could spend my time, but it's a requirement and there's nothing I can do about so there's no point in complaining. I've got my laptop and wireless Internet so I can just work on something else during the lectures if I can't understand it.

Besides those four courses I also have to participate in a special seminar for students from my department. Here Master and PhD students give a presentation, usually a survey of recent research in whatever field they're also working in. Of course most of those are also in Japanese. I'll have to give a presentation myself as well of course, that one will be in English.

So with those four lectures, the seminar and Japanese classes three times a week (I'm taking those again as well), my schedule is looking pretty full (and it's all at Hongo so I have to spend some time travelling too, about an hour in each direction). And wasn't I supposed to do research too? :P

At least I won't be bored. :)

Categories: University, Japan
Posted on: 2007-10-21 12:30 UTC. Show comments (0)

Creating an RSS feed with XML literals

As I promised, I will now give an example of how to use XML literals in Visual Basic 9 to create an RSS feed.

RSS feeds are an example where XML literals are ideally suited for the task. RSS feeds are commonly automatically generated, and instead of having to deal with XmlWriter or XSLT or something similar we can create it directly in VB with minimal effort. This is not a made-up example; the RSS feed for ookii.org is currently generated using the XmlWriter approach similar to the first example in the previous post. When .Net 3.5 is released and my host installs it on the server, I will replace that code with what you see in this post.

We will use a generic RssFeed class to generate the XML from. This also has the advantage that if you have multiple different data sources you want to generate an RSS feed for you can reuse this code. All you need to do is fill an RssFeed class with the appropriate data (for which LINQ is also ideally suited).

For brevity, I will not list the full source of the RssFeed class and its associated RssItem and RssCategory classes here. Suffice it to say they are classes that contain properties for things such as the title of a feed or the text of an item. RssFeed has a collection of RssItems and RssItem has a collection of RssCategories. If you want to see the definitions check out the full source of the example.

The first thing we need to take care of is XML namespaces. RSS itself doesn’t use a namespace, but we’ll be using some extensions that do. We’ll be using these namespaces in multiple places in the VB source and it’d be nice if we don’t have to repeat the namespace URI every time. Fortunately, VB allows us to import XML namespaces in much the same way as regular .Net namespace so we can use them in any XML literal in the file:

Imports <xmlns:dc="http://purl.org/dc/elements/1.1/">
Imports <xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
Imports <xmlns:wfw="http://wellformedweb.org/CommentAPI/">

Before we get started on the heavy work, we have one more thing to do. If we want this to be applicable generically we must realize that some items do not apply to all feeds. For instance I will be using the <slash:comments /> element which gives the number of comments for an item. Not all types of items can have comments so that element doesn’t always apply. Although we could put the code to omit these elements directly in the XML embedded expressions this doesn’t aid readability, so I’ve opted to create functions for them. Here we make use of the fact that if an embedded expression returns Nothing, it’s ignored.

Private Function CreateCommentCountElement(ByVal commentCount As Integer?) As XElement
    If commentCount Is Nothing Then
        Return Nothing
    Else
        Return <slash:comments><%= commentCount %></slash:comments>
    End If
End Function

Private Function CreateCommentsElement(ByVal commentLink As String) As XElement
    If commentLink Is Nothing Then
        Return Nothing
    Else
        Return <comments><%= commentLink %></comments>
    End If
End Function

Private Shared Function CreateCommentRssElement(ByVal commentRssUrl As String) As XElement
    If commentRssUrl Is Nothing Then
        Return Nothing
    Else
        Return <wfw:commentRss><%= commentRssUrl %></wfw:commentRss>
    End If
End Function

Private Function CreateCategories(ByVal categories As IEnumerable(Of RssCategory)) As IEnumerable(Of XElement)
    If categories Is Nothing Then
        Return Nothing
    Else
        Return From category In categories _
               Select <category domain=<%= category.Domain %>><%= category.Name %></category>
    End If
End Function

Now we can finally get to the meat of this sample, generating the RSS feed, which is exceedingly simple:

Public Function CreateXml() As XDocument
    Dim itemElements = From item In Items _
                       Select <item>
                                  <title><%= item.Title %></title>
                                  <link><%= item.Link %></link>
                                  <guid isPermaLink=<%= item.GuidIsPermalink.ToString().ToLowerInvariant() %>><%= item.Guid %></guid>
                                  <pubDate><%= item.PubDate.ToString("r") %></pubDate>
                                  <dc:creator><%= item.Creator %></dc:creator>
                                  <%= CreateCommentCountElement(item.CommentCount) %>
                                  <%= CreateCommentsElement(item.CommentsLink) %>
                                  <%= CreateCommentRssElement(item.CommentRssUrl) %>
                                  <description><%= New XCData(item.Description) %></description>
                                  <%= CreateCategories(item.Categories) %>
                              </item>

    Return <?xml version="1.0" encoding="utf-8"?>
           <rss version="2.0">
               <channel>
                   <title><%= Title %></title>
                   <link><%= Link %></link>
                   <dc:language><%= Language %></dc:language>
                   <%= itemElements %>
               </channel>
           </rss>
End Function

I do it in two steps, first the items and then the main feed, but it could easily be done in one, I just find this more readable. One thing to note is the way I create a CDATA section. I do it this way because you can't put embedded expresssions inside a CDATA section, as the embedded expression syntax is valid content for a CDATA section. Is that really all there is to it? Yes! It’s that simple.

But wait, there’s more. Remember last time I mentioned how you can also query existing XML documents. This means we can also easily load any existing RSS feed into the RssFeed class:

Public Shared Function FromXml(ByVal feed As XDocument) As RssFeed
    If feed Is Nothing Then
        Throw New ArgumentNullException("feed")
    End If

    Dim result = From channel In feed.<rss>.<channel> _
                 Select New RssFeed() With _
                     { _
                         .Title = channel.<title>.Value, _
                         .Link = channel.<link>.Value, _
                         .Language = channel.<dc:language>.Value, _
                         .Items = From item In channel.<item> _
                                  Select New RssItem() With _
                                     { _
                                          .Title = item.<title>.Value, _
                                          .Link = item.<link>.Value, _
                                          .Guid = item.<guid>.Value, _
                                          .GuidIsPermalink = (item.<guid>.@isPermaLink = "true"), _
                                          .PubDate = Date.Parse(item.<pubDate>.Value), _
                                          .CommentCount = CType(item.<slash:comments>.Value, Integer?), _
                                          .CommentsLink = item.<comments>.Value, _
                                          .CommentRssUrl = item.<wfw:commentRss>.Value, _
                                          .Description = item.<description>.Value, _
                                          .Categories = From category In item.<category> _
                                                        Select New RssCategory() With _
                                                           { _
                                                                .Name = category.Value, _
                                                                .Domain = category.@domain _
                                                           } _
                                      } _
                     }

    Return result.First()
End Function

Here we can also see the nice new object initializers at work. Imagine if you will how much work this would’ve been with XmlReader, and how much harder to read that code would’ve been. And in case you’re wondering if this won’t crash if a feed omits one of the optional elements, it won’t: if a feed omits e.g. <slash:comments>, in that case the item.<slash:comments> query will return an empty list, and the Value property will return Nothing, no exceptions will be thrown.

The full source of the example is available here.

This article was written for Visual Studio 2008 Beta 2. Some of it may not apply to other versions.

Categories: Programming
Posted on: 2007-10-21 10:15 UTC. Show comments (1)

Latest posts

Categories

Archive

Syndication

RSS Subscribe

;