All roads lead to Rome…

February 14, 2011 by Michael

…or: Adding Atom links to an RSS feed generated by ROME.

I’m using ROME to create RSS and Atom feeds for a project.

While ROME has excellent support for creating either RSS or Atom feeds, there is no build-in immediate support for Atom elements inside an RSS feed like

<atom:link href="http://example.com/example.rss" rel="self"/>

The first thing nowadays is to Google some keywords and finding someone who had the same problem. I found jettro and started with his solution but to me, the solution might work but is wrong.

Some points:

  • The copyFrom method means exactly the opposite
  • The generator doesn’t need to add the namespace
  • I want to add more than a link some time

I’ve implemented an AtomContent class that holds a list of com.sun.syndication.feed.atom.Link but is easy extensible.

This content is managed by an AtomModule like so

public interface AtomModule extends Module {
	/** The public namespace URI */
	public final static String ATOM_10_URI = "http://www.w3.org/2005/Atom";
	/** as used in the namespaced prefixed with "atom" */
	public final static Namespace ATOM_NS = Namespace.getNamespace("atom", ATOM_10_URI);
 
	/** Gets the included content */
	public AtomContent getContent();
 
	/** Sets the included content */
	public void setContent(final AtomContent content);
}

This interface is accompanied by an implementation AtomModuleImpl that is used to provide instances of AtomContent and especially an AtomModuleGenerator that is used to generate XML elements via JDOM.

The generator implements ModuleGenerator and if added through a rome.properties files automatically adds the appropriate namespace to the feeds. The rome.properties looks like this:

rss_2.0.feed.ModuleGenerator.classes=ac.simons.syndication.modules.atom.AtomModuleGenerator
rss_2.0.item.ModuleGenerator.classes=ac.simons.syndication.modules.atom.AtomModuleGenerator

As you see, i’ll only added the generator to RSS 2.0. Also a parser is not available at the moment as i didn’t need one.

The following snippet demonstrates the usage of the AtomModule:

final AtomContent atomContent = new AtomContent();
atomContent.addLink(new SyndicationLink().withRel("self").withHref("http://example.com/example.rss").getLink());
atomContent.addLink(new SyndicationLink().withRel("alternate").withType("text/html").withHref("http://example.com/example.html").getLink());
feed.getModules().add(new AtomModuleImpl(atomContent));

I published the code as java-syndication along with some other helper classes. If i need more elements, i’ll add them, but in the meantime, the code is usable and maybe of some value for people building RSS feeds with Java and ROME.

No comments yet

Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *