Develop RSS Reader in Java

Use the following classes for developing RSS reader

 ---------------------------------------------
import java.util.ArrayList;
import java.util.List;

/*
 * Stores an RSS feed
 */
public class Feed {
   
    final String title;
    final String link;
    final String description;
    final String language;
    final String copyright;
    final String pubDate;
    final List<FeedMessage> entries = new ArrayList<FeedMessage>();

    public Feed(String title, String link, String description,
            String language, String copyright, String pubDate) {
                this.title = title;
                this.link = link;
                this.description = description;
                this.language = language;
                this.copyright = copyright;
                this.pubDate = pubDate;
    }

    public List<FeedMessage> getMessages() {
        return entries;
    }

    public String getTitle() {
        return title;
    }

    public String getLink() {
        return link;
    }

    public String getDescription() {
        return description;
    }

    public String getLanguage() {
        return language;
    }

    public String getCopyright() {
        return copyright;
    }


    public String getPubDate() {
        return pubDate;
    }

    @Override
    public String toString() {
        return "Feed [copyright=" + copyright + ", description=" + description
                + ", language=" + language + ", link=" + link + ", pubDate="
                + pubDate + ", title=" + title + "]";
    }
   
   

}
-------------------------------------------------------
/*
 * Represents one RSS message
 */
public class FeedMessage {

    String title;
    String description;
    String link;
    String author;
    String guid;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getGuid() {
        return guid;
    }
    public void setGuid(String guid) {
        this.guid = guid;
    }
}



__________________________________________________

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;


public class RSSFeedParser {
    static final String TITLE = "title";
    static final String DESCRIPTION = "description";
    static final String CHANNEL = "channel";
    static final String LANGUAGE = "language";
    static final String COPYRIGHT = "copyright";
    static final String LINK = "link";
    static final String AUTHOR = "author";
    static final String ITEM = "item";
    static final String PUB_DATE = "pubDate";
    static final String GUID = "guid";

    final URL url;

    public RSSFeedParser(String feedUrl) {
        try {
            this.url = new URL(feedUrl);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("null")
   
    public Feed readFeed() {
        Feed feed=null;
        try {
       
            boolean isFeedHeader = true;
            // Set header values intial to the empty string
            String description="";
            String title="";
            String link="";
            String language="";
            String copyright ="";
            String author = "";
            String pubdate ="";
            String guid ="";

            // First create a new XMLInputFactory
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            // Setup a new eventReader
            InputStream in = read();
            XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
            // Read the XML document
            while (eventReader.hasNext()) {

                XMLEvent event = eventReader.nextEvent();

                if (event.isStartElement()) {
                    if (event.asStartElement().getName().getLocalPart() == (ITEM)) {
                        if (isFeedHeader) {
                            isFeedHeader = false;
                            feed = new Feed(title, link, description,
                                    language, copyright, pubdate);
                        }
                        event = eventReader.nextEvent();
                        continue;
                    }

                    if (event.asStartElement().getName().getLocalPart() == (TITLE)) {
                        event = eventReader.nextEvent();
                        title = event.asCharacters().getData();
                        continue;
                    }
                        if (event.asStartElement().getName().getLocalPart() == (DESCRIPTION)) {
                        event = eventReader.nextEvent();
                        //description= event.asCharacters().getData();
                        description= event.toString();
                        continue;
                    }

                    if (event.asStartElement().getName().getLocalPart() == (LINK)) {
                        event = eventReader.nextEvent();
                        //link= event.asCharacters().getData();
                        link= event.toString();
                        continue;
                    }

                    if (event.asStartElement().getName().getLocalPart() == (GUID)) {
                        event = eventReader.nextEvent();
                        guid= event.asCharacters().getData();
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart() == (LANGUAGE)) {
                        event = eventReader.nextEvent();
                        language= event.asCharacters().getData();
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart() == (AUTHOR)) {
                        event = eventReader.nextEvent();
                        author= event.asCharacters().getData();
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart() == (PUB_DATE)) {
                        event = eventReader.nextEvent();
                        pubdate= event.asCharacters().getData();
                        continue;
                    }
                    if (event.asStartElement().getName().getLocalPart() == (COPYRIGHT)) {
                        event = eventReader.nextEvent();
                        copyright= event.asCharacters().getData();
                        continue;
                    }
                } else if (event.isEndElement()) {
                    if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
                        FeedMessage message = new FeedMessage();
                        message.setAuthor(author);
                        message.setDescription(description);
                        message.setGuid(guid);
                        message.setLink(link);
                        message.setTitle(title);
                        feed.getMessages().add(message);
                        event = eventReader.nextEvent();
                        continue;
                    }
                }
            }
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        }
        return feed;

    }

    private InputStream read() {
        try {
            return url.openStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

____________________________________________


import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class RSSFrame {

    public RSSFrame() {
        // TODO Auto-generated constructor stub
        JFrame frame = new JFrame();
        frame.setSize(600,600);
        frame.setVisible(true);
       
        JMenuBar menuBar = new JMenuBar();
        final JPopupMenu menu = new JPopupMenu();
        /////
        JButton button = new JButton("Options");
        button.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        });
       
        /////
        //frame.setJMenuBar(menuBar);
        //menuBar.add(menu);
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        frame.add(panel);
        panel.add(button);
        MenuScroller.setScrollerFor(menu, 8, 125, 3, 1);
        //http://www.vogella.de/article.rss
        //http://www.parkcitygroup.com/feed/
        RSSFeedParser parser = new RSSFeedParser("http://www.vogella.de/article.rss");
        Feed feed = parser.readFeed();
        System.out.println(feed);
        for (final FeedMessage message : feed.getMessages()) {
            JMenuItem menuItem = new JMenuItem(message.getTitle());
            menu.add(menuItem);
            menuItem.setToolTipText(message.getLink());
            menuItem.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    BrowserControl.displayURL(message.getLink());
                }
               
               
            });
           
        }
       
       
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        new RSSFrame();
        /*RSSFeedParser parser = new RSSFeedParser("http://www.parkcitygroup.com/feed/");
        Feed feed = parser.readFeed();
        System.out.println(feed);
        for (FeedMessage message : feed.getMessages()) {
            System.out.println(message.getTitle());
           
        }*/
       
    }

}

Comments

Popular posts from this blog

Using Oracle JRockit Mission Control (JRMC)