KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > RSSConsumer


1 package dinamica;
2
3 import electric.xml.*;
4
5 /**
6  * Retrieves an XML document (RSS feed) and parse the elements
7  * to produce a Recordset.<br>
8  * This class can read RSS feeds given a URL passed to it as a configuration element named "url" in config.xml
9  * or as a request parameter named "url", and then publishes a Recordset named "rss_feed"
10  * that can be printed into the template body as any other recordset. The request parameter
11  * takes precedence over the config.xml element.<br>
12  * This module is suitable to build Portals that aggregate news from
13  * multiple sources, it can be used by Actions that represent "parts" or portlets
14  * to be INCLUDED by a main coordinator Action (the portal page).<br>
15  * The published recordset will contain the following columns:<br>
16  * <ul>
17  * <li>title
18  * <li>description
19  * <li>link
20  * <li>pubDate
21  * </ul>
22  * <br><br>
23  * Creation date: 10/03/2004<br>
24  * Last Update: 15/02/2005<br>
25  * (c) 2004 Martin Cordova<br>
26  * This code is released under the LGPL license<br>
27  * @author Martin Cordova (dinamica@martincordova.com)
28  * @throws Throwable If it can retrieve the RSS document or if the "url" was not defined
29  * */

30 public class RSSConsumer extends GenericTransaction
31 {
32
33     /* (non-Javadoc)
34      * @see dinamica.GenericTransaction#service(dinamica.Recordset)
35      */

36     public int service(Recordset inputParams) throws Throwable JavaDoc
37     {
38         
39         //reuse superclass code
40
int rc = super.service(inputParams);
41         
42         //define recordset structure (according to RSS standard)
43
Recordset rs = new Recordset();
44         rs.append("title", java.sql.Types.VARCHAR);
45         rs.append("description", java.sql.Types.VARCHAR);
46         rs.append("link", java.sql.Types.VARCHAR);
47         rs.append("pubDate", java.sql.Types.VARCHAR);
48         
49         //get url to load
50
String JavaDoc url = getRequest().getParameter("url");
51         if (url==null || url.trim().equals(""))
52             url = getConfig().getConfigValue("url");
53         
54         //retrieve XML document via HTTP
55
String JavaDoc data = StringUtil.httpGet(url, false);
56         
57         //parse and navigate XML document
58
Document doc = new Document(data);
59         Element root = doc.getRoot();
60         Elements items = root.getElements(new XPath("//item"));
61         while (items.hasMoreElements())
62         {
63             //add new entry
64
rs.addNew();
65             Element item = items.next();
66             Elements fields = item.getElements();
67             //get item field values
68
while (fields.hasMoreElements())
69             {
70                 Element field = fields.next();
71                 String JavaDoc name = field.getName();
72                 if (rs.containsField(name))
73                     rs.setValue(name, field.getString());
74             }
75         }
76         
77         //publish recordset
78
publish("rss_feed", rs);
79         
80         return rc;
81         
82     }
83
84 }
85
Popular Tags