/*
* 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
//