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

Taglib For Rss V 0.1

11.03.2006
| 3157 views |
  • submit to reddit
        // jsp taglibrary for rss

// the java code
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>" ;
                  
                                   for( Iterator iter = feed.getEntries().iterator(); iter.hasNext(); ){
                                   html +=  "<tr>" +
                                                "<td><a href = " + ((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.
    }


}
//////////////////////////////////////////////////

//  the tld file 
//////////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
  <tlib-version>1.0</tlib-version>
  <short-name>rss</short-name>
  <uri>/WEB-INF/tlds/rss</uri>
  
  <tag>
       <name>rss</name>
       <tag-class>com.ranjan.feedreader.FeedReader</tag-class>
       <body-content>empty</body-content>
          <attribute>
            <name>url</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
          </attribute>

       
  </tag>    
  <!-- A validator verifies that the tags are used correctly at JSP
         translation time. Validator entries look like this: 
      <validator>
          <validator-class>com.mycompany.TagLibValidator</validator-class>
          <init-param>
             <param-name>parameter</param-name>
             <param-value>value</param-value>
	  </init-param>
      </validator>
   -->
  <!-- A tag library can register Servlet Context event listeners in
        case it needs to react to such events. Listener entries look
        like this: 
     <listener>
         <listener-class>com.mycompany.TagLibListener</listener-class> 
     </listener>
   -->
   
</taglib>