Lastly, we look at building an ATOM document using the same principles. ATOM provides a means for site owners to provide updates to site users. It works by providing an XML document showing recent changes to the site. Browsers that support RSS and RSS readers can then highlight these changes to users.
An atom formatted document contains two main sections, the header which describes the feed, and then a list of elements, that describe the content. This is not intended as a full introduction to ATOM, but rather as an example of how to implement it. For the sake of this example we have a class Entry that represents a page on the site, for we want to provide a list of changed entries via ATOM. We use the class AtomGenerator to generate the xml, and at the bottom of the script is the test harness that will fire it up.
import groovy.xml.MarkupBuilder
import java.text.SimpleDateFormat
// this class represents each page that we want to expose
// via atom.
class Entry
{
int id;
String title;
String summary;
Date lastModified = new Date();
String htmlLocation;
}
// this is the atom generator thats going to do the hard
// work, of converting entries into atom.
class AtomGenerator
{
static String generateForEntries(def entries)
{
// we need a simple date formatter that formats as XML.
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
// create the builder as before.
StringWriter writer = new StringWriter();
MarkupBuilder xml = new MarkupBuilder(writer);
// feed is the root level. In a namespace
xml.feed(xmlns:'http://www.w3.org/2005/Atom') {
// add the top level information about this feed.
title("My super atom feed")
subtitle("Serving up my content")
id("uri:uuid:xxx-xxx-xxx-xxx")
link(href:"https://www.thecoderscorner.com")
author {
name("MyName")
}
updated sdf.format(new Date());
// for each entry we need to create an entry element
entries.each { item ->
entry {
title item.title
link(href:item.htmlLocation);
id "url:mysite,2007,entry,${item.id}"
updated sdf.format(item.lastModified)
summary item.summary;
}
}
}
// lastly give back a string representation of the xml.
return writer.toString();
}
}
// make up some entries for the test
def entries = [
new Entry(id: 1, title: "Entry 1", summary:
"summary 1", htmlLocation: "https://www.thecoderscorner.com"),
new Entry(id: 2, title: "Entry 2", summary:
"summary 2", htmlLocation: "https://www.thecoderscorner.com"),
new Entry(id: 3, title: "Entry 3", summary:
"summary 3", htmlLocation: "https://www.thecoderscorner.com")
]
// now call the generator and print the xml
def xmlAtom = AtomGenerator.generateForEntries (entries)
println(xmlAtom)
Firstly we built the root element, and ensured that the namespace property was provided. Then we populated element feed with the required fields as described in the code. After this we used each to traverse the list of Entry classes passed in, and outputted an entry element for each one. Again, this contained all the required fields as given in the code.
<feed xmlns='http://www.w3.org/2005/Atom'>
<title>My super atom feed</title>
<subtitle>Serving up my content</subtitle>
<id>uri:uuid:xxx-xxx-xxx-xxx</id>
<link href='https://www.thecoderscorner.com' />
<author>
<name>MyName</name>
</author>
<updated>2008-08-27T10:44:16.938Z</updated>
<entry>
<title>Entry 1</title>
<link href='https://www.thecoderscorner.com' />
<id>url:mysite,2007,entry,1</id>
<updated>2008-08-27T10:44:16.884Z</updated>
<summary>summary 1</summary>
</entry>
<entry>
<title>Entry 2</title>
<link href='https://www.thecoderscorner.com' />
<id>url:mysite,2007,entry,2</id>
<updated>2008-08-27T10:44:16.891Z</updated>
<summary>summary 2</summary>
</entry>
<entry>
<title>Entry 3</title>
<link href='https://www.thecoderscorner.com' />
<id>url:mysite,2007,entry,3</id>
<updated>2008-08-27T10:44:16.891Z</updated>
<summary>summary 3</summary>
</entry>
</feed>
Its always advisable to check that the feed validates, I usually use RSS Feed Validator site for this purpose.