Desktop RSS News Reader - Texient - Learn 'n Share

Texient - Learn 'n Share

News & Articles on Technology, Finance & Marketing

Breaking

Home Top Ad

Friday, 13 June 2008

Desktop RSS News Reader

Desktop News Reader is a fully configurable desktop tool developed in Java Swing to read news feeds and display as alerts.

Pre-requisites
Good knowledge in Java Swing and basic knowledge in XML.

RSS Feeds
RSS is a family of Web feed formats used to publish frequently updated content such as blog entries, news headlines, and podcasts in a standardized format. If you want to learn more about RSS, click here.

Desktop News Reader contains a property file, feedreader.properties which stores all the feeds in the below given format:
feedUrl<feed url number>=<url>
Example:
feedUrl1=http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/world/rss.xml
feedUrl2=http://www.ndtv.com/convergence/ndtv/rssnat.asp

Settings Reader
The class, SettingsManager.java is responsible for reading the settings from the property file and storing it in local variables. The code given below:

InputStream fileInputStream = this.getClass()
   .getClassLoader()
   .getResourceAsStream("feedreader.properties");
BufferedReader reader = new BufferedReader(
   new InputStreamReader(fileInputStream));
String line;
HashMap settingsMap = new HashMap();
while((line = reader.readLine())!= null) {
   if(line.length()>0 && line.indexOf("=")!= -1 
      && !line.startsWith("#"))
   settingsMap.put(line.substring(0, line.indexOf("="))
      .toLowerCase(), line.substring(line.indexOf("=")+1, 
      line.length()));
}
...
Iterator keyIterator = settingsMap.keySet()
   .iterator();
String key;
while(keyIterator.hasNext()) {
   key = keyIterator.next();
   if(key.startsWith("feedurl")) {
      feedList.add(settingsMap.get(key));
   }
}
Now, we have the feed urls and other settings in the SettingsManager class.

The Ticket Window
The class FeedReader.java, which extends JWindow and implements HyperlinkListener and Runnable interfaces, is used for displaying the ticker window. The frameless window is achieved by extending the JWindow class. The news feeds appears as hyperlinks which is implemented using JEditorPane. The getJEditorPane() method contents is given below:
feedPane = new JEditorPane();
feedPane.setSize(new Dimension(0, 0));
feedPane.setContentType("text/html");
feedPane.setEditable(false);
feedPane.setBackground(new Color(255, 255, 204));
feedPane.add(getCloseButton());//Creates a close botton

Positioning
The ticker window needs to be placed in the right bottom corner of the taskbar. This is achieved by using the method getMaximumWindowBounds() of GraphicsEnvironment, the code given below:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle r = ge.getMaximumWindowBounds();
Parsing the feed url
Now, we need to parse the urls read from the properties file and store it in a HashMap:
url = new URL(settingsManager.getFeedList().get(k));
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestProperty("User-Agent", "Mozilla/4.0");

Document doc = db.parse(urlConn.getInputStream());
NodeList feedList = doc.getElementsByTagName("item");

for(int i=0; i<feedList.getLength(); i++) {
   childList = feedList.item(i).getChildNodes();
   for(int j=0;j<childList.getLength(); j++){
      if(childList.item(j).getNodeName().equals("title"))
         title = childList.item(j).getTextContent();
      else if(childList.item(j).getNodeName()
            .equals("description"))
         description = childList.item(j).getTextContent();
      else if(childList.item(j).getNodeName().equals("link"))
         link = childList.item(j).getTextContent();
      else if(childList.item(j).getNodeName().equals("pubDate"))
         pubDate = childList.item(j).getTextContent();
   }
   feedBean = new FeedBean(title, description, link, pubDate);
   totalFeedCount++;
   feedMap.put(totalFeedCount, feedBean);
}
Displaying the ticker
The run() method reads one feed item at a time from the HashMap, updates the content of the JEditorPane and animates the JWindow. The duration, random selection and the position can be configured through the property file.
if(settingsManager.isMessageShuffle()) {
   feedCounter = new Random(System.currentTimeMillis())
      .nextInt(totalFeedCount);
} else {
feedCounter++;
if(feedCounter>=totalFeedCount)
   feedCounter = 0;
}
FeedBean feedBean = (FeedBean)feedMap.get(feedCounter);
feed = generateFeedHTML(feedBean);
feedPane.setText(feed);
setVisible(true);
//Animate
for(int i=1;i<=settingsManager.getFeedPanelHeight(); i++) {
   setBounds(feedPanelLeft, r.height - i - 2, 
      settingsManager.getFeedPanelWidth() - 2, i);
   Thread.sleep(30);
}
Thread.sleep(settingsManager.getMessageDuration());
if(isRunning) {
   setVisible(false);
   Thread.sleep(settingsManager.getMessageFrequency());
}
Downloads
The working Desktop News Reader can be downloaded here.

Don't forget to update the feedreader.properties file to include your proxy settings. Open the above jar file using WinZip, feedreader.properties can be found in the base folder, open this file using any text editor and make necessary changes. Save and close WinZip. Open the Jar file using javaw.exe or run
java -jar DesktopNewsReader.jar
from the command prompt.

No comments:

Post a Comment

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to our feed and get articles like this delivered automatically to your feed reader? Like our Facebook Page.