rome/ROMEReleases/ROME0.3Beta/RomeV0.3Tutorials/RomeV0.3TutorialDefiningACustomModuleBeanParserAndGenerator.html

349 lines
No EOL
18 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.3 Tutorial, Defining a Custom Module (bean, parser and generator)</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>
&nbsp;| <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.3 Tutorial, Defining a Custom Module (bean, parser and generator)<a name="Rome_v0.3_Tutorial_Defining_a_Custom_Module_bean_parser_and_generator"></a></h2>
<p>This tutorial walks through the steps of creating a custom module for syndication feeds that support additional namespaces (such as RSS 1.0, RSS 2.0 and Atom 0.3).</p>
<p>To understand this tutorial you should be familiar the with Rome API and with the use of modules in syndication feeds.</p>
<p>Out of the box Rome parsers and generators support plug-ability of modules for RSS 1.0, RSS 2.0 and Atom 0.3 at both feed and item/entry levels. Also support for the Dublin Core and Syndication modules is provided.</p>
<p>The complete source for this tutorial is in the Rome samples bundle as well as in CVS.</p>
<div class="section">
<h3>What is the intended outcome of the tutorial?<a name="What_is_the_intended_outcome_of_the_tutorial"></a></h3>
<p>The goal is to add support for a hypothetical Sample Module by defining a module bean, module parser and module generator and the necessary configuration to wire the parser and generator to an RSS 1.0 parser and RSS 1.0 generator.</p>
<p>The sample module defines 3 elements, 'bar', 'foo' and 'date', where 'bar' and 'date' may occur at most once and the 'foo' element may occur several times. For example, a feed with the Sample Module data would look like:</p>
<div class="source">
<pre>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;rdf:RDF xmlns:rdf=&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;
xmlns=&quot;http://purl.org/rss/1.0/&quot;
xmlns:sample=&quot;http://rome.dev.java.net/module/sample/1.0&quot;&gt;
&lt;channel&gt;
&lt;title&gt;RSS 1.0 Feed with Sample Module&lt;/title&gt;
&lt;link&gt;http://rome.dev.java.net&lt;/link&gt;
&lt;description&gt;This is a feed showing how to use a custom module with Rome&lt;/description&gt;
&lt;items&gt;
&lt;rdf:Seq&gt;
&lt;rdf:li resource=&quot;item01&quot; /&gt;
&lt;rdf:li resource=&quot;item02&quot; /&gt;
&lt;/rdf:Seq&gt;
&lt;/items&gt;
&lt;sample:bar&gt;Channel bar&lt;/sample:bar&gt;
&lt;sample:foo&gt;Channel first foo&lt;/sample:foo&gt;
&lt;sample:foo&gt;Channel second foo&lt;/sample:foo&gt;
&lt;/channel&gt;
&lt;item rdf:about=&quot;item01&quot;&gt;
&lt;title&gt;Title of Item 01&lt;/title&gt;
&lt;link&gt;http://rome.dev.java.net/item01&lt;/link&gt;
&lt;description&gt;Item 01 does not have Sample module data&lt;/description&gt;
&lt;/item&gt;
&lt;item rdf:about=&quot;item02&quot;&gt;
&lt;title&gt;Title of Item 02&lt;/title&gt;
&lt;link&gt;http://rome.dev.java.net/item02&lt;/link&gt;
&lt;description&gt;Item 02 has Sample module data&lt;/description&gt;
&lt;sample:bar&gt;Item 02 bar&lt;/sample:bar&gt;
&lt;sample:foo&gt;Item 02 only foo&lt;/sample:foo&gt;
&lt;sample:date&gt;2004-07-27T00:00+00:00&lt;/sample:date&gt;
&lt;/item&gt;
&lt;/rdf:RDF&gt;
</pre></div></div>
<div class="section">
<h3>Sample Module Bean<a name="Sample_Module_Bean"></a></h3>
<p>First we must start with the bean interface, SampleModuleI. SampleModuleI must extend ModuleI. The ModuleI interface defines the getUri() method, which will return a URI identifying the module. The ModuleI interface also extends the Cloneable, ToString and CopyFrom interfaces to ensure that the beans provide support for basic features as cloning, equality, toString, etc. (for more details on this check the Understanding the Rome common classes and interfaces document).</p>
<p>The SampleModule's URI is defined as a constant, accessible via the getURI() instance method. This is for convenience and good practice only.</p>
<p>Then the module defines 3 properties: bar (String), foos (List) and date (Date). The elements of the foos property list must be strings (wishing for Java 5 generics already?).</p>
<div class="source">
<pre>
public interface SampleModuleI extends ModuleI,CopyFrom {
public static final String URI = &quot;http://rome.dev.java.net/module/sample/1.0&quot;;
public String getBar();
public void setBar(String bar);
public List getFoos();
public void setFoos(List foos);
public Date getDate();
public void setDate(Date date);
}
</pre></div>
<p>Next we have to write the bean implementation, SampleModule.</p>
<p>SampleModule extends Module (the implementation counterpart of ModuleI). Module extends ObjectBean which provides equals, hashCode, toString and clone support for the properties of the class given in the constructor (SampleModuleI). Also the URI of the Sample module is indicated; it will be used by the Module.getUri() method.</p>
<div class="source">
<pre>
public class SampleModule extends Module implements SampleModuleI {
...
public SampleModule() {
super(SampleModuleI.class,SampleModuleI.URI);
}
public String getBar() {
return _bar;
}
...
</pre></div>
<p>The module properties are just Java Bean properties. The only catch is to follow Rome semantics for Collection properties: in the case of a null value for the collection, an empty collection must be returned.. Also, following Rome semantics, all properties are by reference, including mutable ones.</p>
<div class="source">
<pre>
public class SampleModule extends Module implements SampleModuleI {
private String _bar;
private List _foos;
private Date _date;
...
public void setBar(String bar) {
_bar = bar;
}
public List getFoos() {
return (_foos==null) ? (_foos=new ArrayList()) : _foos;
}
public void setFoos(List foos) {
_foos = foos;
}
public Date getDate() {
return _date;
}
public void setDate(Date date) {
_date = date;
}
</pre></div>
<p>Now the weird part: the bits for the CopyFrom logic. The Understanding the Rome common classes and interfaces document fully explains the CopyFrom logic in detail. In short, the CopyFrom interface is to support copying properties from one implementation of a bean (defined through an interface) to another implementation of the same bean.</p>
<p>The getInterface() method returns the bean interface of the implementation (this is necessary for collections containing sub-classes such as a list of modules).</p>
<p>The copyFrom() method copies all the properties from the parameter object (which must be a SampleModuleI implementation) into the caller bean properties. Note that the copyFrom must do a deep copy.</p>
<div class="source">
<pre>
public class SampleModule extends Module implements SampleModuleI {
...
public Class getInterface() {
return SampleModuleI.class;
}
public void copyFrom(Object obj) {
SampleModuleI sm = (SampleModuleI) obj;
setBar(sm.getBar());
List foos = new ArrayList(sm.getFoos()); // this is enough for the copy because the list elements are inmutable (Strings)
setFoos(foos);
setDate((Date)sm.getDate().clone()); // because Date is not inmutable.
}
}
</pre></div></div>
<div class="section">
<h3>Sample Module Parser<a name="Sample_Module_Parser"></a></h3>
<p>The sample module parser must implement the ModuleParser interface. This interface defines 2 methods, getNamespaceUri() that returns the URI of the module and parse(Element) which extracts the module elements from the given Element.</p>
<p>The feed parsers will invoke the module parser with a feed element or with an item element. The module parser must look for module elements in the children of the given element. That is was the Sample parser is doing when looking for 'bar', 'foo' and 'date' children elements in the received element.</p>
<p>In the case of the 'foo' element it looks for all occurrences as the Sample module schema allows for more than one occurrence of the 'foo' element. A SampleModule bean is created and the found values are set into it. For the 'date' element it assumes its a date value in W3C datetime format.</p>
<p>If no Sample Module elements are found in the feed, the parse(Element) method returns null. This is to avoid having an empty instance of a module -not present in the feed- in the feed bean or in the item bean.</p>
<div class="source">
<pre>
public class SampleModuleParser implements ModuleParser {
private static final Namespace SAMPLE_NS = Namespace.getNamespace(&quot;sample&quot;, SampleModuleI.URI);
public String getNamespaceUri() {
return SampleModuleI.URI;
}
public ModuleI parse(Element dcRoot) {
boolean foundSomething = false;
SampleModuleI fm = new SampleModule();
Element e = dcRoot.getChild(&quot;bar&quot;, SAMPLE_NS);
if (e != null) {
foundSomething = true;
fm.setBar(e.getText());
}
List eList = dcRoot.getChildren(&quot;foo&quot;, SAMPLE_NS);
if (eList.size() &gt; 0) {
foundSomething = true;
fm.setFoos(parseFoos(eList));
}
e = dcRoot.getChild(&quot;date&quot;, SAMPLE_NS);
if (e != null) {
foundSomething = true;
fm.setDate(DateParser.parseW3CDateTime(e.getText()));
}
return (foundSomething) ? fm : null;
}
private List parseFoos(List eList) {
List foos = new ArrayList();
for (int i = 0; i &lt; eList.size(); i++) {
Element e = (Element) eList.get(i);
foos.add(e.getText());
}
return foos;
}
}
</pre></div></div>
<div class="section">
<h3>Sample Module Generator<a name="Sample_Module_Generator"></a></h3>
<p>The sample module generator must implement the ModuleGenerator interface. This interface defines 2 methods, getNamespaceUri() that returns the URI of the module and generate(ModuleI,Element) which injects the module data into the given Element.</p>
<p>The feed generator will invoke the module generator with a feed element or with an item element. The module generator must inject the module properties into the given element (which is a feed or an item). This injection has to be done using the right namespace.</p>
<p>If no Sample Module bean is in the feed bean the module generator is not invoked at all.</p>
<div class="source">
<pre>
public class SampleModuleGenerator implements ModuleGenerator {
private static final Namespace SAMPLE_NS = Namespace.getNamespace(&quot;sample&quot;, SampleModuleI.URI);
public String getNamespaceUri() {
return SampleModuleI.URI;
}
public void generate(ModuleI module, Element element) {
// this is not necessary, it is done to avoid the namespace definition in every item.
Element root = element;
while (root.getParent()!=null &amp;&amp; root.getParent() instanceof Element) {
root = (Element) element.getParent();
}
root.addNamespaceDeclaration(SAMPLE_NS);
SampleModuleI fm = (SampleModuleI)module;
if (fm.getBar() != null) {
element.addContent(generateSimpleElement(&quot;bar&quot;, fm.getBar()));
}
List foos = fm.getFoos();
for (int i = 0; i &lt; foos.size(); i++) {
element.addContent(generateSimpleElement(&quot;foo&quot;,foos.get(i).toString()));
}
if (fm.getDate() != null) {
element.addContent(generateSimpleElement(&quot;date&quot;, DateParser.parseW3CDateTime(fm.getDate())));
}
}
protected Element generateSimpleElement(String name, String value) {
Element element = new Element(name, SAMPLE_NS);
element.addContent(value);
return element;
}
}
</pre></div></div>
<div class="section">
<h3>Configuration to make Rome process Sample Module in feeds<a name="Configuration_to_make_Rome_process_Sample_Module_in_feeds"></a></h3>
<p>The last step is to setup the configuration file to indicate to Rome how and when to use the Sample Module parser and generator.</p>
<p>The configuration is stored in a Properties file, 'rome.properties', that has to be in the root of the classpath or JAR where the Sample Module bean, parser and generator classes are.</p>
<p>You must indicate the syndication feed formats (ie RSS 1.0) that must be aware of the Sample Module. You must indicate if the Sample Module is available for feed or item elements, or for both. You must indicate both the parser and the generator classes.</p>
<p>Following is the 'rome.properties' file for the Sample Module, it's defined for RSS 1.0 only, for both feed and item elements.</p>
<div class="source">
<pre>
# Parsers for RSS 1.0 feed modules
#
rss_1.0.feed.ModuleParser.classes=com.sun.syndication.samples.module.SampleModuleParser
# Parsers for RSS 1.0 item modules
#
rss_1.0.item.ModuleParser.classes=com.sun.syndication.samples.module.SampleModuleParser
# Generators for RSS 1.0 feed modules
#
rss_1.0.feed.ModuleGenerator.classes=com.sun.syndication.samples.module.SampleModuleGenerator
# Generators for RSS_1.0 entry modules
#
rss_1.0.item.ModuleGenerator.classes=com.sun.syndication.samples.module.SampleModuleGenerator
</pre></div>
<p>If you are defining more than one module, indicate the parser and generator implementation classes separated by commas or spaces.</p></div>
<div class="section">
<h3>Using the Sample Module from the SyndFeedI beans<a name="Using_the_Sample_Module_from_the_SyndFeedI_beans"></a></h3>
<p>They will be there, just use them. You may get the SampleModuleI bean using the getModule(String Uri) method of the SyndFeedI bean and the SyndEntryI bean. </p>
<p>Adding or replacing a syndication feed parser, generator or converter is along the same lines of what it has been explained in the tutorial for modules. Eventually we'll have a doc on that too </p></div></div>
</div>
</div>
<div class="clear">
<hr/>
</div>
<div id="footer">
<div class="xright">
Copyright &#169; 2004-2013
<a href="http://www.rometools.org">ROME Project</a>.
All Rights Reserved.
</div>
<div class="clear">
<hr/>
</div>
</div>
</body>
</html>