01: package dinamica;
02:
03: import electric.xml.*;
04:
05: /**
06: * Retrieves an XML document (RSS feed) and parse the elements
07: * to produce a Recordset.<br>
08: * This class can read RSS feeds given a URL passed to it as a configuration element named "url" in config.xml
09: * 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: /* (non-Javadoc)
33: * @see dinamica.GenericTransaction#service(dinamica.Recordset)
34: */
35: public int service(Recordset inputParams) throws Throwable {
36:
37: //reuse superclass code
38: int rc = super .service(inputParams);
39:
40: //define recordset structure (according to RSS standard)
41: Recordset rs = new Recordset();
42: rs.append("title", java.sql.Types.VARCHAR);
43: rs.append("description", java.sql.Types.VARCHAR);
44: rs.append("link", java.sql.Types.VARCHAR);
45: rs.append("pubDate", java.sql.Types.VARCHAR);
46:
47: //get url to load
48: String url = getRequest().getParameter("url");
49: if (url == null || url.trim().equals(""))
50: url = getConfig().getConfigValue("url");
51:
52: //retrieve XML document via HTTP
53: String data = StringUtil.httpGet(url, false);
54:
55: //parse and navigate XML document
56: Document doc = new Document(data);
57: Element root = doc.getRoot();
58: Elements items = root.getElements(new XPath("//item"));
59: while (items.hasMoreElements()) {
60: //add new entry
61: rs.addNew();
62: Element item = items.next();
63: Elements fields = item.getElements();
64: //get item field values
65: while (fields.hasMoreElements()) {
66: Element field = fields.next();
67: String name = field.getName();
68: if (rs.containsField(name))
69: rs.setValue(name, field.getString());
70: }
71: }
72:
73: //publish recordset
74: publish("rss_feed", rs);
75:
76: return rc;
77:
78: }
79:
80: }
|