DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

RSS Jsp Taglib V 0.2

11.03.2006
| 5280 views |
  • submit to reddit
        // taglib to display rss in jsp page 

package com.ranjan.feedreader;

/**
 * Created by IntelliJ IDEA.
 * User: Rapid
 * Date: Oct 9, 2006
 * Time: 3:18:23 PM
 * To change this template use File | Settings | File Templates.
 */
import java.net.URL;
import java.util.Iterator;
import java.util.HashMap;

import com.sun.syndication.feed.module.Module;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import javax.servlet.jsp.JspContext;

import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;

/* this tag should return the RSS feed as it is
 *it's purpose is to retrieve rss feeds
 *
 */

public class FeedReader extends TagSupport{

    private String url = null;





    SyndFeedInput input ;
    URL feedUrl;
    SyndFeed feed ;
    String html ;


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int doStartTag() throws JspException {

        return EVAL_BODY_INCLUDE;    //To change body of overridden methods use File | Settings | File Templates.
    }

    public int doEndTag() throws JspException {




            try  {



                feedUrl  = new URL(url);

                input  = new SyndFeedInput();

                feed = input.build(new XmlReader(feedUrl));
/*
                String metaRSS = "Title: " + feed.getTitle() + "\n" +
                "Author: " + feed.getAuthor()  + "\n" +
                 "Description: " + feed.getDescription()  + "\n" +
                 "Pub date: " + feed.getPublishedDate()  + "\n" +
                 "Copyright: " + feed.getCopyright() ;

                 pageContext.getOut().write( metaRSS );
*/

                  html =

                        "<table border='1'>" +
                        "<tr>" +
                            "<td>" + feed.getTitle() +
                            "</td>" +
                        "</tr>" +
                            "<tr>" +
                            "<td>" + feed.getDescription() +
                            "</td>" +
                        "</tr>" +
                         "<tr>" +
                            "<td>" + feed.getCopyright() +
                            "</td>" +
                        "</tr>" +


                        "<tr>" +
                            "<table>" ;

                                    //"<td> <a href = '#' onclick = " + "\"" + "popup_rss_article('" + /**/ + "') \"" + ">" + /**/  + "</a></td>"                      
                                   for( Iterator iter = feed.getEntries().iterator(); iter.hasNext(); ){
                                   
                                   html +=  "<tr>" +
                                                   createHREF(((SyndEntry)iter.next()).getLink() , ((SyndEntry)iter.next()).getTitle() )  +
                                   //             "<td> <a href = '#' onclick = " + "\"" + "popup_rss_article('" +((SyndEntry)iter.next()).getLink() + "') \"" + ">" + ((SyndEntry)iter.next()).getTitle()  + "</a></td>" +
                                            "</tr>"; 
                                   //((SyndEntry)iter.next()).getLink().toString()
                                    }

                        html +=
                                "</table>" +
                         "</tr>" +
                        "</table>" ;


                pageContext.getOut().write( html );


            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("ERROR: " + ex.getMessage());
            }






        return EVAL_PAGE;    //To change body of overridden methods use File | Settings | File Templates.
    }
    public String createHREF( String link, String value )
    {
        return  "<td> <a href = '#' onclick = " + "\"" + "popup_rss_article('" + link + "') \"" + ">" + value  + "</a></td>";        
    }

}

//////////////////////////////////////////
tld     /////////////////////////////////
//////////////////////////////////////////