ZK RSS API Part I: Build your own Rss Reader"

From Documentation
Line 80: Line 80:
  
  
As you can see, use Annotated Databinding technic (If you are not familiar with this, see [http://docs.zkoss.org/wiki/Data_Binding_Collection_Data_with_ZUML_Annotations Data Binding Collection Data with ZUML Annotations]) there's '''ZERO''' code in the page. '''org.zkoss.zrss.RssInit''' is a very simple class that's override '''AnnotateDataBinderInit: doInit()''' Method. It requires '''arg0''' as its Argument for Feed's URL and provides page variable '''feeds''' which is an feed list.
+
As you can see, use Annotated Databinding technic (If you are not familiar with this, see [[Small_Talks/2007/March/Data_Binding_Collection_Data_with_ZUML_Annotations | Data Binding Collection Data with ZUML Annotations]]
 +
) there's '''ZERO''' code in the page. '''org.zkoss.zrss.RssInit''' is a very simple class that's override '''AnnotateDataBinderInit: doInit()''' Method. It requires '''arg0''' as its Argument for Feed's URL and provides page variable '''feeds''' which is an feed list.
  
 
Now, startup your Tomcat and take a look for the rss.zul:
 
Now, startup your Tomcat and take a look for the rss.zul:

Revision as of 08:38, 7 December 2010

DocumentationSmall Talks2007MayZK RSS API Part I: Build your own Rss Reader
ZK RSS API Part I: Build your own Rss Reader

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


Introduction

RSS (Really Simple Syndication, Rich Site Summary or RDF Site Summary anyway) is a very popular function for today's web design. But as it's abbreviations, there's too many standards to implement an API to read them all easily.

In this article I'll show how to use ZK RSS API to build an RSS reader easily.


Quickly build

To "look up" a feed from remote site is very simple, just type the codes as bellow:

	RssBinder binder = new RssBinder();
	try
        {
            RssFeed feed = binder.lookUpFeed( "http://www.mozillazine.org/atom.xml" );//Change URL link... 
            System.out.println("link: " + feed.getFeedLink());
            System.out.println("feedType: " + feed.getFeedType());
            System.out.println("Copyright: " + feed.getCopyright());
            System.out.println("Title: " + feed.getFeedTitle());
            //Do the things you want...
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }

Don't forget to put the jar file in WEB-INF/lib/. And now there's two way for you to use this code:


1. Just embadded these code with <zscript> in your ZUL page.
2. Use Annotated Databinding, put it in an "Init" class which extends AnnotateDataBinderInit. There is already an implementation: org.zkoss.zrss.RssInit.


In the second choice, ZUL page will be very clean like this:

<?init class="org.zkoss.zrss.RssInit" 
arg0="http://www.osnews.com/files/recent.rdf,
http://www.webattack.com/webattack.xml,
http://www.mozillazine.org/atom.xml" ?> 
<window xmlns:a="http://www.zkoss.org/2005/zk/annotation" 
	title="RSS Reader"  border="normal" >
<hbox width="1024px" spacing="2">
<vbox width="430px" spacing="0">
    <groupbox mold="3d" width="100%" >
	<caption label="RSS Feeds.">
			   
	</caption>
	  <a:bind model="feeds" rows="feeds.size" selectedItem="selected"/>
	  <listbox >
	    <a:bind _var="feed"/>
	    <listitem>	    
	      
	      <listcell>
	        <a:bind src="feed.iconLink"/>
	      	<image/>
	      </listcell>
	      
	      <a:bind label="feed.feedTitle"/>
	      <listcell/>
	    </listitem>
	  </listbox>
</groupbox>
...


As you can see, use Annotated Databinding technic (If you are not familiar with this, see Data Binding Collection Data with ZUML Annotations ) there's ZERO code in the page. org.zkoss.zrss.RssInit is a very simple class that's override AnnotateDataBinderInit: doInit() Method. It requires arg0 as its Argument for Feed's URL and provides page variable feeds which is an feed list.

Now, startup your Tomcat and take a look for the rss.zul:

Screenshot.gif

Summary of the setup step:

1. put zrss-0.7.jar, rome.jar and jdom.jar in the right place /WEB-INF/lib/.(all resources could be downloaded from Here)
2. Add <?init class="org.zkoss.zrss.RssInit" arg0="URL1,URL2,URL3,..." ?> in your page.
3. Use page variable "feeds" as your wish.

Encapsulate the RSS Standard's complexity: RssFeed & RssFeedEntry

Inorder to provide a more convenient API for simply usage and wont loss any detail while real hard work needed, The org.zkoss.zrss. RssFeed you get is a facade Interface of ROME's feed at the current vrsion. You can still get the native feed use getNativeFeed(). RssFeed only provides those popular properties an rss information should have:

package org.zkoss.zrss;
public interface RssFeed extends Serializable, Cloneable
{
    Image getIcon();//get web site's favorits icon.

    String getIconLink();//get web site's favorits icon url.

    Image getFeedImage();//get feed provided image.

    String getFeedTitle();//get title of this feed.

    String getFeedLink();//get Feed url Link.

    String getFeedDesc();//get the description of this feed.

    String getCopyright();//get publisher's copyright.

    List getFeedEntries(); // get News list from feed.

    String getFeedType();//get version and standard of this feed.

    Object getNativeFeed();//get native implementation from ROME project.

    Date getPublishedDate();//get publish date of this feed.
    
}//end of interface...


RssFeedEntry is a facade too, It provides these methods:

package org.zkoss.zrss;
public interface RssFeedEntry extends Serializable
{
    Object getNativeEntry();//get native Entry from Rome Project...
    
    String getTitle();//get this entry's title
    
    String getLink();//get detail info's page link.
    
    Date getPublishedDate();//get this message pulbish date.
    
    Date getUpdatedDate();//get this message pulbish date.
    
    String getDescValue();//get Description Content.
    
    String getDescType();//get Description MIME type.
}

And like RssFeed, you can get native Entry from ROME using getNativeEntry().


Know more about RssBinder

While previous quick show, we use binder.lookUpFeed(String url); to look up feed, but in many cases connecting to other plase will always get in trouble that you need to handle, So RssBinder provides two more overload methods for "lookup":


/**
 * @param urlStr the URL of feed
 * @param conn the Http connection used to connect remote site.
 * @return A simple facade interface for RSS.
 * @throws Exception
 */
public RssFeed lookUpFeed(String urlStr, HttpURLConnection conn) throws Exception
/**
 * @param urlStr
 * @return
 */	
public RssFeed lookUpFeed(String urlStr, HttpExceptionListener handler);

public interface HttpExceptionListener
{
    void handleException(HttpURLConnection conn, Exception ex);
}


First one you use your own HttpURLConnection to look up feeds, it will throw any bad things outside.

Second one you use defult created HttpURLConnection and add a handler (HttpExceptionListener) yourself.


Download Link




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