XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

ROME in a Day: Parse and Publish Feeds in Java
by Mark Woodman | Pages: 1, 2, 3, 4, 5, 6


The createFooter() Method

We have used ROME to do all the hard stuff, so all that remains is to create the HTML footer added to each SyndEntry. To mimic some features provided by the inspiration of FeedWarmer, we'll add links for email, del.icio.us, and a Google Blog Search. None of this code is ROME-specific, so it should be fairly self-explanatory.

A word of caution: don't cut and paste the following section into your Java editor. The code shown has escaped HTML so that it will display properly in your web browser. Instead, use the actual source code in FeedWarmer.java, which contains unescaped HTML.

/**
 * Create a feed item footer of immediate actions
 * by using information from the feed item itself
 * @param original  The original text of the feed item
 * @param link      The link for the feed item
 * @param title     The title of the feed item
 * @return
 */
private String createFooter(String original, String link,
                            String title)
{
    // Use StringBuffer to create a sb
    StringBuffer sb = new StringBuffer(original);
    sb.append("\n\n<div class='feedwarmer'><hr/>");
    sb.append("<i>Getting Warmer:</i> ");

    // Add email link using title and item link
    sb.append("<a href='mailto:?body=Check this out: ");
    sb.append(link).append("'>Email this</a> | ");

    // Add delicious link using item title link
    sb.append("<a href='http://del.icio.us/post/?url=");
    sb.append(link).append("&title=").append(title);
    sb.append("'>Add to delicious</a> | ");

    // Add Google Blogs Search link using item title
    sb.append("<a href='http://blogsearch.google.com/");
    sb.append("blogsearch?hl=en&q=").append(title);
    sb.append("'>Blog Search this</a>");

    // Finish and return the sb
    sb.append("</div>\n");
    return sb.toString();
}

These are just a few simple examples of what you can do with just the item's title and link. Once you realize how easy it is to get at a feed's data with ROME, a lot of opportunities open up to leverage that data when republishing the feed.

The main() Method

We're in the home stretch. Let's put together a simple main() method which allows us to test the code from the command line. The first argument passed in will be the URL of any feed you like. This version writes the results out as RSS 2.0 to the console, but you could just as easily write them out to a file or other output stream.

   /**
     * Main method to demo from command line.
     * @param args        args[0] must be the URL of a feed
     * @throws Exception
     */
    public static void main(String[] args) throws Exception
    {
        // Create instance
        FeedWarmer warmer = new FeedWarmer();

        // "Warm" a feed using URL passed in,
        // designating the feed output desired
        String warmedFeed = warmer.warmFeed(new URL(args[0]),
                                            "rss_2.0");
        // Print to console to demo results
        System.out.println(warmedFeed);
    }
}

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow