KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > rss > component > Channel


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.rss.component;
6
7 import java.util.Vector JavaDoc;
8
9 import javax.xml.parsers.DocumentBuilder JavaDoc;
10 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
11
12 import org.w3c.dom.Document JavaDoc;
13 import org.w3c.dom.Node JavaDoc;
14 import org.w3c.dom.NodeList JavaDoc;
15
16
17 /*
18  * Fri, May 30, 2003 @
19  * @author: Tuan Nguyen
20  * @version: $Id: Channel.java,v 1.2 2004/07/29 14:09:43 tuan08 Exp $
21  * @since: 0.0
22  * @email: tuan08@yahoo.com
23  */

24 public class Channel {
25   private String JavaDoc title_ ;
26   private String JavaDoc description_ ;
27   private String JavaDoc link_ ;
28   private String JavaDoc imageSrc_ ;
29   private Item[] items_ ;
30   private long updateTime_ ;
31   
32   public Channel() {
33   }
34
35   public String JavaDoc getTitle() { return title_ ; }
36   public void setTitle(String JavaDoc title) { title_ = title ; }
37
38   public String JavaDoc getLink() { return link_ ; }
39   public void setLink(String JavaDoc link) { link_ = link ; }
40
41   public String JavaDoc getDescription() { return description_ ; }
42   public void setDescription(String JavaDoc desc) {description_ = desc ; }
43
44   public String JavaDoc getImageSrc() { return imageSrc_ ; }
45   public void setImageSrc(String JavaDoc 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 JavaDoc url) throws Exception JavaDoc {
54     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
55     factory.setIgnoringComments(true);
56     factory.setCoalescing(true);
57     factory.setNamespaceAware(false);
58     factory.setValidating(false);
59     DocumentBuilder JavaDoc parser = factory.newDocumentBuilder();
60     Document JavaDoc 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 JavaDoc doc) throws Exception JavaDoc {
70     NodeList JavaDoc list = doc.getElementsByTagName("channel");
71     if (list.getLength() > 0) {
72       Node JavaDoc channel = list.item(0);
73       list = channel.getChildNodes();
74       for (int i = 0; i < list.getLength(); ++i) {
75         Node JavaDoc node = list.item(i);
76         String JavaDoc 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 JavaDoc doc) throws Exception JavaDoc {
89     String JavaDoc root = doc.getDocumentElement().getTagName();
90     Item[] items = null ;
91     if (root.equals("rdf:RDF")) {
92       NodeList JavaDoc list = doc.getElementsByTagName("item");
93       items = getItems(list);
94     } else if (root.equals("rss")) {
95       NodeList JavaDoc list = doc.getElementsByTagName("channel");
96       if (list.getLength() != 1) {
97         items = new Item[0];
98       } else {
99         Node JavaDoc channel = list.item(0);
100         items = getItems(channel.getChildNodes());
101       }
102     } else if (root.equals("xml")) {
103       NodeList JavaDoc 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 JavaDoc items) throws Exception JavaDoc {
112     Vector JavaDoc v = new Vector JavaDoc();
113     for (int i = 0; i < items.getLength(); ++i) {
114       Node JavaDoc node = items.item(i);
115       if (node.getNodeName().equals("item") == false) {
116         continue;
117       }
118       NodeList JavaDoc itemChildren = node.getChildNodes();
119       Item item = new Item() ;
120       for (int j = 0; j < itemChildren.getLength(); ++j) {
121         Node JavaDoc child = itemChildren.item(j);
122         String JavaDoc 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 JavaDoc toString() {
145     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
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