1 package org.springframework.samples.tiles; 2 3 import java.net.MalformedURLException ; 4 import java.net.URL ; 5 import java.util.ArrayList ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 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 28 public class NewsFeedController extends ComponentControllerSupport { 29 30 protected void doPerform( 31 ComponentContext componentContext, HttpServletRequest request, HttpServletResponse response) 32 throws Exception { 33 34 NewsFeedConfigurer configurer = (NewsFeedConfigurer) getApplicationContext().getBean("feedConfigurer"); 35 String uri = configurer.feedUri((String ) componentContext.getAttribute("sourceName")); 36 NewsReader reader = new NewsReader(uri); 37 38 int size = Integer.parseInt((String ) componentContext.getAttribute("size")); 39 List items = new ArrayList (); 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 ) 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 type; 57 58 public NewsReader(String uriString) throws MalformedURLException , DocumentException { 59 DocumentFactory docFactory = DocumentFactory.getInstance(); 61 SAXReader saxReader = new SAXReader(docFactory, false); 62 URL uri = new URL (uriString); 63 this.feed = saxReader.read(uri); 64 this.type = feed.getRootElement().getName(); 66 } 67 68 private String getChannelPrefix() { 69 if ("rss".equals(this.type)) { 70 return "/*/*/"; 71 } 72 else { 73 return "/*/"; 74 } 75 } 76 77 public int size() { 78 if ("RDF".equals(this.type)) { 80 int size = 0; 81 Iterator it = feed.getRootElement().elements().iterator(); 82 while (it.hasNext()) { 83 String 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 getItemAsXML(int index) { 94 Node node = null; 95 96 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 if (node != null) { 106 return node.asXML(); 107 } 108 109 return null; 110 } 111 112 public String getChannelTitle() { 113 return this.feed.getRootElement().element("channel").element("title").getText(); 114 } 115 116 public String getChannelLink() { 117 return this.feed.getRootElement().element("channel").element("link").getText(); 118 } 119 120 public String getLinkAt(int i) { 121 return getItemNodeValue(i, "link"); 122 } 123 124 public String getTitleAt(int i) { 125 return getItemNodeValue(i, "title"); 126 } 127 128 public String getDescriptionAt(int i) { 129 return getItemNodeValue(i, "description"); 130 } 131 132 private String getItemNodeValue(int index, String nodeName) { 133 String value = null; 134 135 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 getRDFItemNodes() { 148 List items = new ArrayList (this.feed.getRootElement().elements().size()); 150 Iterator it = this.feed.getRootElement().elements().iterator(); 151 while (it.hasNext()) { 152 Element elem = (Element) it.next(); 153 String qName = elem.getQualifiedName(); 154 if ("item".equals(qName)) { 155 items.add(elem); 157 } 158 } 159 return items; 160 } 161 162 private Node getRDFNodeAt(int i, String subNode) { 163 List items = getRDFItemNodes(); 165 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 |