More on microformats
Having arrived early to the DC area for 2GX and in need of a little time to acclimate to the temperature change, I chilled out in the hotel. As a Marylander, I can deal with cold but swings of more than 20 - 30 degrees throws my body off if I don't take it easy the first day. It's ironic that while I write this, a VisitFlorida ad appeared on TV. But I digress...
I recently blogged about Google's Social Graph API and microformats. While perusing the microformats.org site one day, I came across a Java hCard builder/parser. hCard is a semantic (X)HTML representation of vCard information. It is used as an element in other types(hResume, hReview, hAtom) often to indicate ownership. I wanted to be able to make hCards but in a more Groovy way. Reinier Zwitserloot wrote classes for most of the compound components of hCard such as name, telephone, geolocation, etc so wiring them into a builder wasn't that hard. I did have to make a custom version of the HCard container class(named hCard) to allow incremental building of the hCard. All I did was making the lists not be immutable and rewriting the for loops in the toString() and toHTML() functions. The linked version has all the basics defined (name, tel, geolocation, organization, address, and email). The more optional fields such as notes, nicknames, photos, logos, and the like will be in the next update.
With the builder, I can take the following code for a hypothetical worker at the 2GX hotel:
def hCard = new hCardBuilder()
def card = hCard.hcard {
n(givenName: 'James', familyName:'Williams')
tel(number:'703-555-9000 ext 555', types:['WORK'])
org(name:'Sheraton Resorts', unit:'IT')
email(address:'name@email.com',types:['pref'])
adr(streetAddress:'11810 Sunrise Valley Drive', locality:'Reston', region:'VA', postalCode:'20191', countryName:'USA')
}
and create the following hCard:
<div class="vcard">Contact information for <span class="fn">James Williams </span>
<br/>Proper Name:
<div class="n"><span class="given-name">James</span> <span
class="family-name">Williams</span></div>
<div class="tel"> Tel [<span class="type">WORK</span>]:
<span class="value">703-555-9000 ext 555</span></div>
<div class="email"> Email [<span class="type">pref</span>]:
<a href="mailto:name@email.com" class="value">name@email.com</a>
</div>
<div class="adr"> Address:<br/><span class="street-address">11810 Sunrise ValleyDrive</span><br/>
<span class="locality">Reston</span>, <span class="region">VA</span>, <span class="postal-code">20191</span>
<br/><span class="country-name">USA</span></div>
<div class="org">Organization: <span class="organization-name">Sheraton Resorts</span> [<span class="organization-unit">IT</span>]
</div>
</div>
Simplistic, yes, but cool for an afternoon project and a needed component for more complex types like hAtom.
org.microformats.hCard - Java
The Firefox Operator plugin will identify microformats in web pages and allow you to export that information to an address book or scheduler, find it with Google or Yahoo maps, and other cool stuff.