205 lines
11 KiB
HTML
205 lines
11 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<!-- Generated by Apache Maven Doxia Site Renderer 1.4 at 2013-09-27 -->
|
||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||
|
<title>ROME - Rome v0.1 Tutorial, Using Rome to aggregate many syndication feeds into a single one</title>
|
||
|
<style type="text/css" media="all">
|
||
|
@import url("../../../css/maven-base.css");
|
||
|
@import url("../../../css/maven-theme.css");
|
||
|
@import url("../../../css/site.css");
|
||
|
</style>
|
||
|
<link rel="stylesheet" href="../../../css/print.css" type="text/css" media="print" />
|
||
|
<meta name="author" content="mkurz" />
|
||
|
<meta name="Date-Creation-yyyymmdd" content="20110815" />
|
||
|
<meta name="Date-Revision-yyyymmdd" content="20130927" />
|
||
|
<meta http-equiv="Content-Language" content="en" />
|
||
|
|
||
|
</head>
|
||
|
<body class="composite">
|
||
|
<div id="banner">
|
||
|
<a href="http://github.com/" id="bannerLeft">
|
||
|
<img src="../../../images/romelogo.png" alt="ROME" />
|
||
|
</a>
|
||
|
<div class="clear">
|
||
|
<hr/>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="breadcrumbs">
|
||
|
|
||
|
|
||
|
<div class="xright">
|
||
|
|
||
|
<span id="publishDate">Last Published: 2013-09-27</span>
|
||
|
| <span id="projectVersion">Version: 1.1-SNAPSHOT</span>
|
||
|
</div>
|
||
|
<div class="clear">
|
||
|
<hr/>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="leftColumn">
|
||
|
<div id="navcolumn">
|
||
|
|
||
|
|
||
|
<h5>Rome</h5>
|
||
|
<ul>
|
||
|
<li class="none">
|
||
|
<a href="../../../index.html" title="Overview">Overview</a>
|
||
|
</li>
|
||
|
<li class="collapsed">
|
||
|
<a href="../../../HowRomeWorks/index.html" title="How Rome Works">How Rome Works</a>
|
||
|
</li>
|
||
|
<li class="none">
|
||
|
<a href="../../../RssAndAtOMUtilitiEsROMEV0.5AndAboveTutorialsAndArticles/index.html" title="Tutorials And Articles">Tutorials And Articles</a>
|
||
|
</li>
|
||
|
<li class="collapsed">
|
||
|
<a href="../../../ROMEReleases/index.html" title="Releases">Releases</a>
|
||
|
</li>
|
||
|
<li class="none">
|
||
|
<a href="../../../ROMEDevelopmentProposals/index.html" title="ROME Development Proposals">ROME Development Proposals</a>
|
||
|
</li>
|
||
|
</ul>
|
||
|
<h5>Project Documentation</h5>
|
||
|
<ul>
|
||
|
<li class="collapsed">
|
||
|
<a href="../../../project-info.html" title="Project Information">Project Information</a>
|
||
|
</li>
|
||
|
<li class="collapsed">
|
||
|
<a href="../../../project-reports.html" title="Project Reports">Project Reports</a>
|
||
|
</li>
|
||
|
</ul>
|
||
|
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
|
||
|
<img class="poweredBy" alt="Built by Maven" src="../../../images/logos/maven-feather.png" />
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="bodyColumn">
|
||
|
<div id="contentBox">
|
||
|
<div class="section">
|
||
|
<h2>Rome v0.1 Tutorial, Using Rome to aggregate many syndication feeds into a single one<a name="Rome_v0.1_Tutorial_Using_Rome_to_aggregate_many_syndication_feeds_into_a_single_one"></a></h2>
|
||
|
<p><b>Software requirements:</b> J2SE 1.4+, Xerces 2.4.0, JDOM B10, Commons Codec 1.2 and Rome 0.1.</p>
|
||
|
<p>Rome represents syndication feeds (RSS and Atom) as instances of the com.sun.syndication.synd.SyndFeedI interface. The SyndFeedI interfaces and its properties follow the Java Bean patterns. The default implementations provided with Rome are all lightweight classes.</p>
|
||
|
<p>Rome includes parsers to process syndication feeds into SyndFeedI instances. The SyndInput class handles the parsers using the correct one based on the syndication feed being processed. The developer does not need to worry about selecting the right parser for a syndication feed, the SyndInput will take care of it by peeking at the syndication feed structure. All it takes to read a syndication feed using Rome are the following 2 lines of code:</p>
|
||
|
<div class="source">
|
||
|
<pre>
|
||
|
SyndInput input = new SyndInput();
|
||
|
SyndFeedI feed = input.build(feedUrl.openStream());
|
||
|
</pre></div>
|
||
|
<p>The first line creates a SyndInput instance that will work with any syndication feed type (RSS and Atom versions). The second line instructs the SyndInput to read the syndication feed from the InputStream of a URL pointing to the feed. The SyndInput.build() method returns a SyndFeedI instance that can be easily processed.</p>
|
||
|
<p>Rome also includes generators to create syndication feeds out of SyndFeedI instances. The SyndOutput class does this generation. When creating a syndication feed output, the developer has to indicate the syndication feed type for the ouput, and the SyndOutput will use the right generator for it. The following two lines of code show how to create a syndication feed output from a SyndFeedI instance:</p>
|
||
|
<div class="source">
|
||
|
<pre>
|
||
|
SyndOutput output = new SyndOutput(outputType);
|
||
|
output.output(feed,System.out);
|
||
|
</pre></div>
|
||
|
<p>The first line creates a SyndOutput instance that will produce syndication feeds of the type indicated by the outputType parameter. The outputType parameter can be any of the following values: rss_0.9, rss_0.91, rss_0.92, rss_0.93, rss_0.94, rss_1.0, rss_2.0 & atom_0.3. The second line writes the SyndFeedI as a syndication feed into the application's output.</p>
|
||
|
<p>SyndFeedI can also be created and populated within the code. For example:</p>
|
||
|
<div class="source">
|
||
|
<pre>
|
||
|
SyndFeedI aggrFeed = new SyndFeed();
|
||
|
aggrFeed.setTitle("Aggregated Feed");
|
||
|
aggrFeed.setDescription("Anonymous Aggregated Feed");
|
||
|
aggrFeed.setAuthor("anonymous");
|
||
|
aggrFeed.setLink("http://www.anonymous.com");
|
||
|
</pre></div>
|
||
|
<p>The snipped of code above creates a SyndFeedI instance using the default implementation provided by Rome and sets the title, description, author and link properties of the feed.</p>
|
||
|
<p>SyndFeedI properties can be modified, assigned to other SyndFeedI instances, removed, etc. It's important to remember that the getters/setters semantics defined for all SyndFeedI properties (and properties of its properties) is a copy by reference, not by value. In other words if you alter the property of a SyndFeedI property you are altering the SyndFeedI. Or, another example, if you assign the list of entries of one SyndFeedI instance to another SyndFeedI isntance and then you modify the list or any of the entries in the list, you are modifying the entries of both SyndFeedI instances.</p>
|
||
|
<p>The following lines of code show how to copy the entries of a SyndFeedI instance being read (inFeed) into a SyndFeedI instance being created within the application (feed):</p>
|
||
|
<div class="source">
|
||
|
<pre>
|
||
|
SyndFeedI feed = new SyndFeed();
|
||
|
...
|
||
|
List entries = new ArrayList();
|
||
|
feed.setEntries(entries);
|
||
|
...
|
||
|
SyndInput input = new SyndInput(false);
|
||
|
SyndFeedI inFeed = input.build(inputUrl.openStream());
|
||
|
|
||
|
entries.addAll(inFeed.getEntries());
|
||
|
</pre></div>
|
||
|
<p>Note that it's OK to assign the entries list to the feed and later add entries to it as all SyndFeedI getters/setters do copy by reference.</p>
|
||
|
<p>Following is the full code for a Java application that reads a list of syndication feed, aggregates their items into a single feed, and writes the aggregated feed to the application's output.</p>
|
||
|
<div class="source">
|
||
|
<pre>
|
||
|
package com.sun.syndication.samples;
|
||
|
|
||
|
import java.net.URL;
|
||
|
import java.util.List;
|
||
|
import java.util.ArrayList;
|
||
|
|
||
|
import com.sun.syndication.feed.synd.SyndFeedI;
|
||
|
import com.sun.syndication.feed.synd.SyndFeed;
|
||
|
import com.sun.syndication.feed.synd.SyndEntryI;
|
||
|
import com.sun.syndication.io.SyndInput;
|
||
|
import com.sun.syndication.io.SyndOutput;
|
||
|
|
||
|
public class FeedAggregator {
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
boolean ok = false;
|
||
|
if (args.length>=2) {
|
||
|
try {
|
||
|
String outputType = args[0];
|
||
|
|
||
|
SyndFeedI feed = new SyndFeed();
|
||
|
feed.setTitle("Aggregated Feed");
|
||
|
feed.setDescription("Anonymous Aggregated Feed");
|
||
|
feed.setAuthor("anonymous");
|
||
|
feed.setLink("http://www.anonymous.com");
|
||
|
|
||
|
List entries = new ArrayList();
|
||
|
feed.setEntries(entries);
|
||
|
|
||
|
for (int i=1;i<args.length;i++) {
|
||
|
URL inputUrl = new URL(args[i]);
|
||
|
|
||
|
SyndInput input = new SyndInput(false);
|
||
|
SyndFeedI inFeed = input.build(inputUrl.openStream());
|
||
|
|
||
|
entries.addAll(inFeed.getEntries());
|
||
|
|
||
|
}
|
||
|
|
||
|
SyndOutput output = new SyndOutput(outputType);
|
||
|
output.output(feed,System.out);
|
||
|
|
||
|
ok = true;
|
||
|
}
|
||
|
catch (Exception ex) {
|
||
|
System.out.println("ERROR: "+ex.getMessage());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!ok) {
|
||
|
System.out.println();
|
||
|
System.out.println("FeedAggregator aggregates different feeds into a single one.");
|
||
|
System.out.println("The first parameter must be the feed type for the aggregated feed.");
|
||
|
System.out.println(" [valid values are: rss_0.9, rss_0.91, rss_0.92, rss_0.93, ]");
|
||
|
System.out.println(" [ rss_0.94, rss_1.0, rss_2.0 & atom_0.3 ]");
|
||
|
System.out.println("The second to last parameters are the URLs of feeds to aggregate.");
|
||
|
System.out.println();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
</pre></div></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="clear">
|
||
|
<hr/>
|
||
|
</div>
|
||
|
<div id="footer">
|
||
|
<div class="xright">
|
||
|
Copyright © 2004-2013
|
||
|
<a href="http://www.rometools.org">ROME Project</a>.
|
||
|
All Rights Reserved.
|
||
|
|
||
|
</div>
|
||
|
<div class="clear">
|
||
|
<hr/>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|