1 17 18 package com.sun.syndication.fetcher.samples; 19 import java.net.URL ; 20 21 import com.sun.syndication.feed.synd.SyndFeed; 22 import com.sun.syndication.fetcher.FeedFetcher; 23 import com.sun.syndication.fetcher.FetcherEvent; 24 import com.sun.syndication.fetcher.FetcherListener; 25 import com.sun.syndication.fetcher.impl.FeedFetcherCache; 26 import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache; 27 import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher; 28 36 public class FeedReader { 37 public static void main(String [] args) { 38 boolean ok = false; 39 if (args.length==1) { 40 try { 41 URL feedUrl = new URL (args[0]); 42 FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); 43 FeedFetcher fetcher = new HttpURLFeedFetcher(feedInfoCache); 44 45 FetcherEventListenerImpl listener = new FetcherEventListenerImpl(); 46 47 fetcher.addFetcherEventListener(listener); 48 49 System.err.println("Retrieving feed " + feedUrl); 50 SyndFeed feed = fetcher.retrieveFeed(feedUrl); 54 55 System.err.println(feedUrl + " retrieved"); 56 System.err.println(feedUrl + " has a title: " + feed.getTitle() + " and contains " + feed.getEntries().size() + " entries."); 57 System.err.println("Polling " + feedUrl + " again to test conditional get support."); 61 SyndFeed feed2 = fetcher.retrieveFeed(feedUrl); 62 System.err.println("If a \"Feed Unchanged\" event fired then the server supports conditional gets."); 63 64 ok = true; 65 } 66 catch (Exception ex) { 67 System.out.println("ERROR: "+ex.getMessage()); 68 ex.printStackTrace(); 69 } 70 } 71 72 if (!ok) { 73 System.out.println(); 74 System.out.println("FeedReader reads and prints any RSS/Atom feed type."); 75 System.out.println("The first parameter must be the URL of the feed to read."); 76 System.out.println(); 77 } 78 79 } 80 81 static class FetcherEventListenerImpl implements FetcherListener { 82 85 public void fetcherEvent(FetcherEvent event) { 86 String eventType = event.getEventType(); 87 if (FetcherEvent.EVENT_TYPE_FEED_POLLED.equals(eventType)) { 88 System.err.println("\tEVENT: Feed Polled. URL = " + event.getUrlString()); 89 } else if (FetcherEvent.EVENT_TYPE_FEED_RETRIEVED.equals(eventType)) { 90 System.err.println("\tEVENT: Feed Retrieved. URL = " + event.getUrlString()); 91 } else if (FetcherEvent.EVENT_TYPE_FEED_UNCHANGED.equals(eventType)) { 92 System.err.println("\tEVENT: Feed Unchanged. URL = " + event.getUrlString()); 93 } 94 } 95 } 96 } 97 | Popular Tags |