Adding Extension Elements

With the extension utilities, you can easily add some of the popular RSS extension elements to your feed. For example, to add the itunes elements:

   
    ItunesExtension ext = new ItunesExtension();
    ext.setAuthor("itune author");
    ext.setBlock("itunes block");
    ext.setDuration("itune duration");
    ItunesCategoryType cat = new ItunesCategoryType();
    cat.setText("cat");
    ext.getCategory().add(cat);
    ext.getCategory().add(cat);
    //<duration> is not allowed under channel, read the specs
    //http://www.apple.com/itunes/store/podcaststechspecs.html
    ext.setDuration("10:10:20");
    ext.setExplicit("yes");
    ext.setKeywords("some,key, words");
    ItunesImageType image = new ItunesImageType();
    image.setHref("http://someurl");
    image.setRel("ref");
    ext.setImage(image);
    ItunesOwnerType owner = new ItunesOwnerType();
    owner.setEmail("owner@email.com");
    owner.setName("pwner");
    ext.setOwner(owner);
    ext.setSubtitle("subtitle");
    ext.setSummary("summary");
    

    FeedWriter w = new FeedWriter("itunes.xml");
    ChannelFeed c = new ChannelFeed();
    c.setTitle("a title");
    c.getOtherElements().addAll(ExtensionUtils.toItunesElements(ext));
    ItemEntry i = new ItemEntry();
    i.getOtherElements().addAll(ExtensionUtils.toItunesElements(ext));
    c.addItem(i.setTitle("item title"));
    w.writeChannel(c);

The ExtensionUtils class currently support the following extensions. If you don't know what they are, take a look at the extension page for more information about them.

    AdminExtension extensionObject = new AdminExtension();
    ...
    ExtensionUtils.toAdminAtomElements(extensionObject);
    
    BloggerExtension extensionObject = new BloggerExtension();
    ...
    ExtensionUtils.toBloggerAtomElements(extensionObject);
    
    DublinCoreExtension extensionObject = new DublinCoreExtension();
    ...
    ExtensionUtils.toDublinCoreElements(extensionObject);
    
    FeedburnerExtension extensionObject = new FeedburnerExtension();
    ...
    ExtensionUtils.toFeedburnerElements(extensionObject);
    
    GeoRssExtension extensionObject = new GeoRssExtension();
    ...
    ExtensionUtils.toGeoRssElements(extensionObject);
    
    GoogleBaseExtension extensionObject = new GoogleBaseExtension();
    ...
    ExtensionUtils.toGoogleBaseElements(extensionObject);
    
    ItunesExtension extensionObject = new ItunesExtension();
    ...
    ExtensionUtils.toItunesElements(extensionObject);
    
    MrssExtension extensionObject = new MrssExtension();
    ...
    ExtensionUtils.toMrssElements(extensionObject);
    
    SlashExtension extensionObject = new SlashExtension();
    ...
    ExtensionUtils.toSlahsElements(extensionObject);
    
    SyndicationExtension extensionObject = new SyndicationExtension();
    ...
    ExtensionUtils.toSyndicationElements(extensionObject);
    
    WellFormedWebExtension extensionObject = new WellFormedWebExtension();
    ...
    ExtensionUtils.toWellFormedWebElements(extensionObject);    
    

Reading Extension Elements

Most of these extension elements are not in the RSS specs. Therefore, they are not mapped to the core data model of Yarfraw. However, in the real world, you will find these extension elements quite often in many feeds. If you are using Yarfraw to write a feed viewer, you should also take these extension elements into consideration. For example, many RSS 2.0 feed does not have a <pubDate> element, instead, they use the <dc:date> element to indicates the date of publishing.

All elements that are not mapped to the core model will be added to the 'otherElements' list of its corresponding parent. The extension utilities package has some methods that you can use to extract these common extension elements to an object for further processing.

To extract the itunes extension elements from an item, you write:

    ChannelFeed c = new FeedReader("itunes.xml").readChannel();
    ItunesExtension ext = ExtensionUtils.extractItunesExtension(c.getOtherElements());
    

One thing you should note is that when an element is 'extracted', the element is removed from the otherElement list. For example, if the otherElement list contains only itunes elements, then the size of the otherElement list will be 0 after the extraction:

    ChannelFeed c = new FeedReader("itunes.xml").readChannel();
    ItunesExtension ext = ExtensionUtils.extractItunesExtension(c.getOtherElements());
    assert(c.getOtherElements().size() == 0);
    

The ExtensionUtils class currently support the following extensions. If you don't know what they are, take a look at the extension page for more information about them.

  
    ExtensionUtils.extractAdminExtension(otherElements);
    ExtensionUtils.extractBloggerExtension(otherElements);
    ExtensionUtils.extractDublinCoreExtension(otherElements);
    ExtensionUtils.extractFeedburnerExtension(otherElements);
    ExtensionUtils.extractGeoRssExtension(otherElements);
    ExtensionUtils.extractGoogleBaseExtension(otherElements);
    ExtensionUtils.extractItunesExtension(otherElements);
    ExtensionUtils.extractMrssExtension(otherElements);
    ExtensionUtils.extractSlashExtension(otherElements);
    ExtensionUtils.extractSyndicationExtension(otherElements);
    ExtensionUtils.extractWellFormedWebExtension(otherElements);
    
    //some elements in Atom 0.3 are supported through an extension module
    Atom03Extension ext = ExtensionUtils.extractAtom03Extension(otherElements);
    

Atom 0.3 support

On version 0.9, I added support for Atom 0.3 just for completeness sake. At the time of writting (9/26/07), the only site that I know is still actually using Atom 0.3 is Google news. Since the feed is from Google, I have to support it :-).

Support for Atom 0.3 is achieved through some combination of core model mapping and extension mapping. The reason for this is because I do not want to modify the core model in order to add support for an obsolete format. So elements that can mapped to the core model are mapped, the rest will be added to the otherElements list and can be mapped using an extension module.

All elements in Atom 0.3 that are of type 'content construct' will be sent to the otherElements list. They include:

        <xs:element name="tagline" type="atom:contentType"/>
        <xs:element name="info" type="atom:contentType"/>
        <xs:element name="summary" type="atom:contentType"/>
        <xs:element name="content" type="atom:contentType"/>
        <xs:element name="title" type="atom:contentType"/>      

In addition, element <issued> will also be added as an extension element.

Here is a short example on how to use the core model and the extension module to access data in an Atom 0.3 feed:

        
    FeedReader r = new FeedReader("http://news.google.com/?output=atom");
    r.setFormat(FeedFormat.ATOM03);
    ChannelFeed c = r.readChannel();
    assertEquals(26, c.getItems().size());
    
    Atom03Extension ext = ExtensionUtils.extractAtom03Extension(c.getItems().get(0).getOtherElements());
    assertEquals("<b>Iranian president spars with academics in NY - Reuters Canada</b>", ext.getTitle().getContent().get(0));
    assertEquals("2007-09-24T19:39:29+00:00", ext.getIssued());
    assertEquals("Top Stories", ext.getSummary().getContent().get(0));
    assertEquals("text/html", ext.getContent().get(0).getType());
    assertEquals("escaped", ext.getContent().get(0).getMode());
        

For more details, I encourage to take a look at the Javadoc . Also check out the FAQ section for more insights about this API.