1 5 package org.exoplatform.portlets.rss.component; 6 7 import java.util.Vector ; 8 9 import javax.xml.parsers.DocumentBuilder ; 10 import javax.xml.parsers.DocumentBuilderFactory ; 11 12 import org.w3c.dom.Document ; 13 import org.w3c.dom.Node ; 14 import org.w3c.dom.NodeList ; 15 16 17 24 public class Channel { 25 private String title_ ; 26 private String description_ ; 27 private String link_ ; 28 private String imageSrc_ ; 29 private Item[] items_ ; 30 private long updateTime_ ; 31 32 public Channel() { 33 } 34 35 public String getTitle() { return title_ ; } 36 public void setTitle(String title) { title_ = title ; } 37 38 public String getLink() { return link_ ; } 39 public void setLink(String link) { link_ = link ; } 40 41 public String getDescription() { return description_ ; } 42 public void setDescription(String desc) {description_ = desc ; } 43 44 public String getImageSrc() { return imageSrc_ ; } 45 public void setImageSrc(String src) { imageSrc_ = src ; } 46 47 public Item[] getItems() { return items_ ; } 48 public void setItems(Item[] items) { items_ = items ; } 49 50 public long getUpdateTime() { return updateTime_ ; } 51 public void setUpdateTime(long time) { updateTime_ = time; } 52 53 static public Channel parse(String url) throws Exception { 54 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 55 factory.setIgnoringComments(true); 56 factory.setCoalescing(true); 57 factory.setNamespaceAware(false); 58 factory.setValidating(false); 59 DocumentBuilder parser = factory.newDocumentBuilder(); 60 Document document = parser.parse(url); 61 Channel channel = new Channel() ; 62 setChannelInfo(channel, document) ; 63 setItems(channel, document) ; 64 channel.setUpdateTime(System.currentTimeMillis()) ; 65 return channel ; 66 } 67 68 69 static private void setChannelInfo(Channel channelObj, Document doc) throws Exception { 70 NodeList list = doc.getElementsByTagName("channel"); 71 if (list.getLength() > 0) { 72 Node channel = list.item(0); 73 list = channel.getChildNodes(); 74 for (int i = 0; i < list.getLength(); ++i) { 75 Node node = list.item(i); 76 String nodeName = node.getNodeName() ; 77 if ("title".equals(nodeName)) { 78 channelObj.setTitle(node.getFirstChild().getNodeValue()); 79 } else if ("link".equals(nodeName)) { 80 channelObj.setLink(node.getFirstChild().getNodeValue()); 81 } else if ("description".equals(nodeName)) { 82 channelObj.setDescription(node.getFirstChild().getNodeValue()); 83 } 84 } 85 } 86 } 87 88 static private void setItems(Channel channelObj, Document doc) throws Exception { 89 String root = doc.getDocumentElement().getTagName(); 90 Item[] items = null ; 91 if (root.equals("rdf:RDF")) { 92 NodeList list = doc.getElementsByTagName("item"); 93 items = getItems(list); 94 } else if (root.equals("rss")) { 95 NodeList list = doc.getElementsByTagName("channel"); 96 if (list.getLength() != 1) { 97 items = new Item[0]; 98 } else { 99 Node channel = list.item(0); 100 items = getItems(channel.getChildNodes()); 101 } 102 } else if (root.equals("xml")) { 103 NodeList list = doc.getElementsByTagName("item"); 104 items = getItems(list); 105 } else { 106 items = new Item[0]; 107 } 108 channelObj.setItems(items) ; 109 } 110 111 static private Item[] getItems(NodeList items) throws Exception { 112 Vector v = new Vector (); 113 for (int i = 0; i < items.getLength(); ++i) { 114 Node node = items.item(i); 115 if (node.getNodeName().equals("item") == false) { 116 continue; 117 } 118 NodeList itemChildren = node.getChildNodes(); 119 Item item = new Item() ; 120 for (int j = 0; j < itemChildren.getLength(); ++j) { 121 Node child = itemChildren.item(j); 122 String nodeName = child.getNodeName() ; 123 if (nodeName.equals("title")) { 124 if (child.getFirstChild() != null) 125 item.setTitle(child.getFirstChild().getNodeValue()); 126 } 127 if (nodeName.equals("link")) { 128 if (child.getFirstChild() != null) 129 item.setLink(child.getFirstChild().getNodeValue()); 130 } 131 if (nodeName.equals("description")) { 132 if (child.getFirstChild() != null) { 133 item.setDescription(child.getFirstChild().getNodeValue()); 134 } 135 } 136 } 137 v.addElement(item); 138 } 139 Item[] foundItems = new Item[v.size()]; 140 v.copyInto(foundItems); 141 return foundItems; 142 } 143 144 public String toString() { 145 StringBuffer b = new StringBuffer () ; 146 for (int i = 0; i < items_.length; ++i) { 147 b.append(items_[i].getTitle()).append("\n"); 148 } 149 return b.toString() ; 150 } 151 } 152 153 | Popular Tags |