Over a million developers have joined DZone.

RSS Jsp Taglib V 0.2

·
// 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 =

                        "" +
                        "" +
                            "" +
                        "" +
                            "" +
                            "" +
                        "" +
                         "" +
                            "" +
                        "" +


                        "" +
                            "
" + feed.getTitle() + "
" + feed.getDescription() + "
" + feed.getCopyright() + "
" ; //"" for( Iterator iter = feed.getEntries().iterator(); iter.hasNext(); ){ html += "" + createHREF(((SyndEntry)iter.next()).getLink() , ((SyndEntry)iter.next()).getTitle() ) + // "" + ""; //((SyndEntry)iter.next()).getLink().toString() } html += "
" + /**/ + "
" + ((SyndEntry)iter.next()).getTitle() + "
" + "" + "" ; 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 " " + value + ""; } } ////////////////////////////////////////// tld ///////////////////////////////// //////////////////////////////////////////
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}