1:/* 2: * Created on Nov 8, 2004 3: * 4: * Project: xtom 5: * 6: */ 7:package xtom.parser.examples.rss; 8: 9:import java.io.BufferedReader; 10:import java.io.InputStreamReader; 11:import java.net.URL; 12:import java.net.URLConnection; 13:import java.util.LinkedList; 14: 15:import xtom.parser.Element; 16:import xtom.parser.Parser; 17:import xtom.parser.XMLTree; 18: 19:/** 20: * This is an RSS Reader. It support RSS version 2.0 Only. 21: * This Reader was tested with the JRoller RSS feed. 22: * @author taras 23: * @version $Revision: 1.1 $ 24: * @since 25: */ 26:public class RSSReader { 27: 28: String xml = ""; 29: 30: /** 31: * Reads the URL for the rss feed. 32: * @param url The url of the RSS Feed. 33: */ 34: public RSSReader(URL url){ 35: if (url == null) throw new IllegalArgumentException("URL cannot be NULL"); 36: 37: try { 38: URLConnection conn = url.openConnection(); 39: BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 40: String inputLine; 41: 42: while ((inputLine = in.readLine()) != null) 43: xml += inputLine ; 44: 45: in.close(); 46: } catch (Exception e){ 47: e.printStackTrace(); 48: throw new IllegalStateException(e.getMessage()); 49: } 50: } 51: 52: /** 53: * Parses the RSS Feed, and returns the RSS Feed document 54: * containing the RSS data. 55: * @return RSS Feed Document. 56: * @throws RuntimeException if the XML read from the URL is not RSS 2.0 57: */ 58: public Feed parse(){ 59: 60: // create new XTOM Parser 61: Parser p = new Parser(xml); 62: // parse the XML and create an XMLTree. 63: XMLTree tree = p.parse(); 64: 65: // now check if the root element is <rss> 66: Element root = tree.getRootElement(); 67: if (root.getName() != "rss") throw new RuntimeException("The root element was not <rss>. Can't parse this RSS Feed"); 68: 69: // now we are ready to parse the rss feed. 70: return parseFeed(root); 71: } 72: 73: /** 74: * Goes through all elements and maps them onto a RSS holder 75: * classes. 76: * @param root The root elment. 77: * @return an RSS feed. 78: */ 79: private Feed parseFeed(Element root){ 80: Feed feed = new Feed(); 81: 82: if (root.getAttribute("version") != null) 83: feed.setVersion(root.getAttribute("version").getValue()); 84: 85: // get the channel element 86: Element ch = root.getElementByPath("channel"); 87: 88: // get the title element of the Channel, get the value and set the title of the Channel. 89: Channel channel = new Channel(root.getElementByPath("channel/title").getValue()); 90: 91: // populate the RSS Channel 92: channel.setCopyright(ch.getElementByPath("copyright").getValue()); 93: channel.setDescription(ch.getElementByPath("description").getValue()); 94: channel.setGenerator(ch.getElementByPath("generator").getValue()); 95: channel.setLastBuildDate(ch.getElementByPath("lastBuildDate").getValue()); 96: channel.setLink(ch.getElementByPath("link").getValue()); 97: channel.setManagingEditor(ch.getElementByPath("managingEditor").getValue()); 98: channel.setWebMaster(ch.getElementByPath("webMaster").getValue()); 99: 100: // now we need to get the Items for the Channel and populate the Item objects 101: // while adding them to the Channel object. 102: Element[] items = ch.getElementsByPath("item"); 103: // a Collection of items; 104: LinkedList itemsList = new LinkedList(); 105: 106: for (int i = 0; i < items.length; i++){ 107: Item item = new Item(items[i].getElementByPath("title").getValue()); 108: item.setCategory(items[i].getElementByPath("category").getValue()); 109: item.setDescription(items[i].getElementByPath("description").getValue()); 110: item.setGuid(items[i].getElementByPath("guid").getValue()); 111: item.setPermaLink(items[i].getElementByPath("guid").getAttribute("isPermaLink").getValueAsBoolean()); 112: item.setPubDate(items[i].getElementByPath("pubDate").getValue()); 113: 114: // add this item to the list. 115: itemsList.add(item); 116: } 117: 118: // add the list of items to the Channel, and add the Channel to the feed. 119: channel.setItems(itemsList); 120: feed.setChannel(channel); 121: 122: // we are done, return the feed. 123: return feed; 124: } 125: 126: /** 127: * Test this feed 128: * @param args 129: */ 130: public static void main(String[] args) throws Exception { 131: URL url = new URL("http://jroller.com/rss"); 132: RSSReader reader = new RSSReader(url); 133: Feed feed = reader.parse(); 134: 135: // print out some of the Feed data 136: System.out.println("Feed version: " + feed.getVersion()); 137: System.out.println("Channel title: " + feed.getChannel().getTitle()); 138: System.out.println("Channel description: " + feed.getChannel().getDescription()); 139: 140: LinkedList list = (LinkedList) feed.getChannel().getItems(); 141: for (int i = 0; i < list.size(); i++){ 142: Item item = (Item) list.get(i); 143: System.out.println("Item title: " + item.getTitle()); 144: System.out.println("Item category: " + item.getCategory()); 145: System.out.println("Item description: " + item.getDescription()); 146: } 147: } 148:}