KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > tiles > NewsFeedController


1 package org.springframework.samples.tiles;
2
3 import java.net.MalformedURLException JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 import org.apache.struts.tiles.ComponentContext;
13 import org.dom4j.Document;
14 import org.dom4j.DocumentException;
15 import org.dom4j.DocumentFactory;
16 import org.dom4j.Element;
17 import org.dom4j.Node;
18 import org.dom4j.io.SAXReader;
19
20 import org.springframework.web.servlet.view.tiles.ComponentControllerSupport;
21
22 /**
23  * Controller for the news tile that retrieves a feed
24  * mentioned in the definitions file.
25  *
26  * @author Alef Arendsen
27  */

28 public class NewsFeedController extends ComponentControllerSupport {
29
30     protected void doPerform(
31             ComponentContext componentContext, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
32             throws Exception JavaDoc {
33
34         NewsFeedConfigurer configurer = (NewsFeedConfigurer) getApplicationContext().getBean("feedConfigurer");
35         String JavaDoc uri = configurer.feedUri((String JavaDoc) componentContext.getAttribute("sourceName"));
36         NewsReader reader = new NewsReader(uri);
37
38         int size = Integer.parseInt((String JavaDoc) componentContext.getAttribute("size"));
39         List JavaDoc items = new ArrayList JavaDoc();
40         for (int i = 0; i < size && i < reader.size(); i++) {
41             NewsItem item = new NewsItem();
42             item.setLink(reader.getLinkAt(i));
43             item.setTitle(reader.getTitleAt(i));
44             item.setSourceName((String JavaDoc) componentContext.getAttribute("sourceName"));
45             items.add(item);
46         }
47         
48         componentContext.putAttribute("items", items);
49     }
50
51
52     private static class NewsReader {
53
54         private Document feed;
55
56         private String JavaDoc type;
57
58         public NewsReader(String JavaDoc uriString) throws MalformedURLException JavaDoc, DocumentException {
59             // get and parse the feed
60
DocumentFactory docFactory = DocumentFactory.getInstance();
61             SAXReader saxReader = new SAXReader(docFactory, false);
62             URL JavaDoc uri = new URL JavaDoc(uriString);
63             this.feed = saxReader.read(uri);
64             // determine the type
65
this.type = feed.getRootElement().getName();
66         }
67
68         private String JavaDoc getChannelPrefix() {
69             if ("rss".equals(this.type)) {
70                 return "/*/*/";
71             }
72             else {
73                 return "/*/";
74             }
75         }
76
77         public int size() {
78             // todo: RDF somehow doesn't work with XPATH expression, fix it
79
if ("RDF".equals(this.type)) {
80                 int size = 0;
81                 Iterator JavaDoc it = feed.getRootElement().elements().iterator();
82                 while (it.hasNext()) {
83                     String JavaDoc qName = ((Element) it.next()).getQualifiedName();
84                     if (qName.equals("item")) {
85                         size++;
86                     }
87                 }
88                 return size;
89             }
90             return this.feed.selectNodes(getChannelPrefix() + "item").size();
91         }
92
93         public String JavaDoc getItemAsXML(int index) {
94             Node node = null;
95
96             // determine RDF or other format
97
if ("RDF".equals(this.type)) {
98                 node = getRDFNodeAt(index, null);
99             }
100             else {
101                 node = this.feed.selectSingleNode(getChannelPrefix() + "item[" + (index + 1) + "]");
102             }
103
104             // return as XML
105
if (node != null) {
106                 return node.asXML();
107             }
108
109             return null;
110         }
111
112         public String JavaDoc getChannelTitle() {
113             return this.feed.getRootElement().element("channel").element("title").getText();
114         }
115
116         public String JavaDoc getChannelLink() {
117             return this.feed.getRootElement().element("channel").element("link").getText();
118         }
119
120         public String JavaDoc getLinkAt(int i) {
121             return getItemNodeValue(i, "link");
122         }
123
124         public String JavaDoc getTitleAt(int i) {
125             return getItemNodeValue(i, "title");
126         }
127
128         public String JavaDoc getDescriptionAt(int i) {
129             return getItemNodeValue(i, "description");
130         }
131
132         private String JavaDoc getItemNodeValue(int index, String JavaDoc nodeName) {
133             String JavaDoc value = null;
134
135             // determine RDF or other format
136
if ("RDF".equals(this.type)) {
137                 value = getRDFNodeAt(index, nodeName).getText();
138             }
139             else {
140                 value = this.feed.selectSingleNode(
141                         getChannelPrefix() + "item[" + (index + 1) + "]/" + nodeName).getText();
142             }
143
144             return value;
145         }
146
147         private List JavaDoc getRDFItemNodes() {
148             // iterate through the entire document in search for item nodes
149
List JavaDoc items = new ArrayList JavaDoc(this.feed.getRootElement().elements().size());
150             Iterator JavaDoc it = this.feed.getRootElement().elements().iterator();
151             while (it.hasNext()) {
152                 Element elem = (Element) it.next();
153                 String JavaDoc qName = elem.getQualifiedName();
154                 if ("item".equals(qName)) {
155                     // found one, increment size
156
items.add(elem);
157                 }
158             }
159             return items;
160         }
161
162         private Node getRDFNodeAt(int i, String JavaDoc subNode) {
163             // get all the item nodes
164
List JavaDoc items = getRDFItemNodes();
165             // find the correct item, and either return a 'subNode' or itself
166
Element item = (Element) items.get(i);
167             if (subNode == null) {
168                 return item;
169             }
170             else {
171                 return item.element(subNode);
172             }
173         }
174     }
175
176 }
177
Popular Tags