/* * Copyright (c) 2006 Mark Woodman * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.xml.feed; import com.sun.syndication.feed.synd.*; import com.sun.syndication.io.*; import com.totsp.xml.syndication.content.ContentModule; import java.io.*; import java.net.URL; import java.util.*; /** * FeedWarmer demonstrates how to use ROME to read, manipulate * and publish an RSS or Atom feed. This example adds a footer * of "immediate action" links to each feed item. * * @author Mark Woodman * */ public class FeedWarmer { /** Namespace URI for content:encoded elements */ private static String CONTENT_NS = "http://purl.org/rss/1.0/modules/content/"; /** Parses RSS or Atom to instantiate a SyndFeed. */ private SyndFeedInput input; /** Transforms SyndFeed to RSS or Atom XML. */ private SyndFeedOutput output; /** * Default constructor. */ public FeedWarmer() { input = new SyndFeedInput(); output = new SyndFeedOutput(); } /** * Add FeedWarmer footer to all items of any feed, * then republish as format specified in OUTPUT. * @param url The feed URL to input * @param outFormat The feed type to output. Can be: * rss_0.9, rss_0.91, rss_0.92, rss_0.93, * rss_0.94, rss_1.0, rss_2.0, atom_0.3, * or atom_1.0 * @throws IOException * @throws FeedException */ public String warmFeed(URL url, String outFormat) throws IOException, FeedException { // Load the feed, regardless of RSS or Atom type SyndFeed feed = input.build(new XmlReader(url)); // Set the output format of the feed feed.setFeedType(outFormat); // Modify the feed title String newTitle = feed.getTitle() + " (Warmed)"; feed.setTitle(newTitle); // Iterate through feed items, adding a footer each item Iterator entryIter = feed.getEntries().iterator(); while (entryIter.hasNext()) { SyndEntry entry = (SyndEntry) entryIter.next(); addFooter(entry); } // Generate XML in output format, regardless of original StringWriter writer = new StringWriter(); output.output(feed, writer); return writer.toString(); } /** * Add FeedWarmer footer to an entry. * @param entry */ private void addFooter(SyndEntry entry) { // Prep variables used in loops String title = entry.getTitle(); String link = entry.getLink(); // Use the add-on ContentModule to handle // elments within the feed ContentModule module = ((ContentModule) entry.getModule(CONTENT_NS)); // If content:encoded is found, use that. if(module!=null) { // Container for footer-appended HTML strings List newStringList = new ArrayList(); // Iterate through encoded HTML, creating footers Iterator oldStringIter = module.getEncodeds().iterator(); while (oldStringIter.hasNext()) { String original = (String) oldStringIter.next(); newStringList.add(createFooter(original, link, title)); } // Set new encoded HTML strings on entry module.setEncodeds(newStringList); } else { // Fall back to adding footer in // This results in escaped HTML. Ugly, but common. Iterator contentIter = entry.getContents().iterator(); while (contentIter.hasNext()) { // Target the description node SyndContent content = (SyndContent) contentIter.next(); // Create and set a footer-appended description String original = content.getValue(); content.setValue(createFooter(original, link, title)); } } } /** * 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

"); sb.append("Getting Warmer: "); // Add email link using title and item link sb.append("Email this | "); // Add delicious link using item title link sb.append("Add to delicious | "); // Add Google Blogs Search link using item title sb.append("Blog Search this"); // Finish and return the sb sb.append("
\n"); return sb.toString(); } /** * 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); } }