ZK RSS API Part II: Publish your RSS feed in chosen type

From Documentation
DocumentationSmall Talks2007JuneZK RSS API Part II: Publish your RSS feed in chosen type
ZK RSS API Part II: Publish your RSS feed in chosen type

Author
Ian Tsai, Engineer, Potix Corporation
Date
June 5, 2007
Version
  1. Apache-Tomcat-5.5.X
  2. ZK 2.3.1
  3. zrss-0.8.jar
  4. Rome-0.9.jar


Introduction

Last time in ZK RSS API Part I: Build your own Rss Reader, I introduced how to build a simple RSS reader.

But in server side, publisher implementation is more needed then a reader, So this time I'll demonstrate how to use ZK Rss API to build a simple Rss publishing service through a zul page in various RSS type, And I'll show some usefull functions that can help you to build more powerfull Rss service on ZK.

Quickly build

Before real code to do publishing work we need to setup ZK environment. following those steps and checkout your web.xml:


1.Enable your ZK DSP engine, Uncomment or add this segment in your web.xml:

<servlet>
<servlet-name>dspLoader</servlet-name>
<servlet-class>org.zkoss.web.servlet.dsp.InterpreterServlet</servlet-class>
<init-param>
	<param-name>class-resource</param-name>
	<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dspLoader</servlet-name>
<url-pattern>*.dsp</url-pattern>
</servlet-mapping>


2.Make sure WEB-INF/tld/zk/rss.dsp.tld is out there. Like JSP Tag Lib Definition, Rss Publisher need a DSP Tag Lib Definition to attache function call in DSP page.

3.Make sure WEB-INF/rss/rssPublish.dsp is out there. This is the page that will write out your content.

4. Make sure your zrss.jar is version 0.8, Ver 0.7 dosen't support publish.

After environment setting, to "Publish" a content that you prepared as a rss feed is very simple. Just embedded codes bellow in your zul page:

<zk>
<zscript>
    import org.zkoss.zrss.*;
    import org.zkoss.zrss.samples.*;
    
    RssPublisher publisher = new RssPublisher( Dummy.getDemoFeed());
    publisher.publish();
    
</zscript>
</zk>


If you don't like zscript code polluting the page, move it to a initiator like org.zkoss.zrss.samples.RssPublishDemoInit:

public void doInit(Page page, Object[] args) throws Exception
{
	String[] urlList = null;
	if(args.length>0)
	{
		if(args[0] instanceof String)
			urlList = ((String)args[0]).split(",");
		else if(args[0] instanceof String[]) 
			urlList = (String[]) args[0];
	}
	
	// create the feed you want...
	RssFeed feed = Dummy.getDemoFeed();
	
	
	// publish feed...
	RssPublisher publisher = new RssPublisher( feed);
	if(urlList!=null)publisher.aggregateFeeds(urlList);
	publisher.publish();
}


Then your page will like this:

<?init class="org.zkoss.zrss.samples.RssPublishDemoInit" ?>
<zk>
</zk>


The most dirty code in this example is RssFeed's POJO Setter works in Dummy.getDemoFeed():

//create feed and its content... 
RssFeed feed = new ZFeedImpl();

//must filled...
feed.setFeedType(Const.atom_10);//setup the output feed type...
feed.setTitle("ZK - #1 Ajax project in SourceForge.net");
feed.setLink("http://www.zkoss.org");
feed.setDescription("ZK is an open-source Ajax Web framework"+
" that enables rich user interface for Web applications with little programming.");

//option filled...
feed.setCopyright("POTIX Corp.");
feed.setAuthor("ZK Team.");

// make RssFeedEntry List... 
List entries = new ArrayList();
RssFeedEntry entry;
for(int i=0;i<4;i++)
{
	entry = new ZFeedEntryImpl();
	entry.setTitle(DEMO[0][i]);
	entry.setLink(DEMO[1][i]);
	entry.setAuthor("ZK ");
	entry.setUpdatedDate(new Date());
	try 
	{
		entry.setPublishedDate(dateParser.parse(DEMO[2][i]));
	}
	catch (ParseException ex) {}
	entry.setDescription(DEMO[3][i], DEMO[4][i]);
	entries.add(entry);
}
feed.setEntries(entries);

After all those works use browser to query you page. your get your feed.


Architecture Overview

The following graph shows how the ZK RSS API works:

RssPublishing.gif


While Publisher was declared in a page, and its publish() Method is fired befor write anything into response, the request will be forwarded to rssPublish.dsp. thats because zul page can only output HTML to browser side, but DSP can output any text file that you want.


More Applications implemented by ZK RSS API

Use RssBinder and RssPublisher we can aggregate feeds and bridge feeds to another type during publication. Lets take a look at org.zkoss.zrss.sample.RssPublishInit in demo page rsspublish2.zul:

<?init class="org.zkoss.zrss.samples.RssPublishTypeInit"
  arg0="atom_1.0" 
  arg1="http://www.mozillazine.org/atom.xml,
  http://www.osnews.com/files/recent.rdf" ?>
<zk> </zk> 
//set output rssType=atom_1.0
//set output FeedSources=http://www.mozillazine.org/atom.xml + 
//http://www.osnews.com/files/recent.rdf


Download Link




Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.