forked from gihanjayatilaka/Motify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSSHandling.java
84 lines (73 loc) · 3.15 KB
/
RSSHandling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.Collections;
public class RSSHandling {
public static NodeList getNodeList(File fileName,String tagName) throws Exception{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(fileName);
doc.getDocumentElement().normalize();
NodeList nodeList=doc.getElementsByTagName(tagName);
return nodeList;
}
public static ArrayList<NotificationUpdate> getNotificationArrayList(NodeList nodeList){
ArrayList<NotificationUpdate> listOfNotifications=new ArrayList<NotificationUpdate>();
for(int x=0;x<nodeList.getLength();x++){
Node thisNode=nodeList.item(x);
if(thisNode.getNodeType()==javax.xml.soap.Node.ELEMENT_NODE){
Element thisElement=(Element) thisNode;
NotificationUpdate thisNotification=new NotificationUpdate(thisElement.getElementsByTagName("pubDate").item(0).getTextContent(),thisElement.getElementsByTagName("title").item(0).getTextContent(),thisElement.getElementsByTagName("link").item(0).getTextContent(),thisElement.getElementsByTagName("description").item(0).getTextContent());
listOfNotifications.add(thisNotification);
}
}
return listOfNotifications;
}
public static ArrayList<NotificationUpdate> mergeTwoArrayLists(ArrayList<NotificationUpdate> arrayList1,ArrayList<NotificationUpdate> arrayList2){
ArrayList<NotificationUpdate> newArrayList=new ArrayList<NotificationUpdate>();
newArrayList.addAll(arrayList1);
newArrayList.addAll(arrayList2);
Collections.sort(newArrayList);
return newArrayList;
}
public static void downloadRSS(String[] urlArray){
for(int x=0;x<urlArray.length;x++){
try{
URL urlToDownload=new URL(urlArray[x]);
ReadableByteChannel in=Channels.newChannel(urlToDownload.openStream());
FileOutputStream out=new FileOutputStream("rssFile"+x+".xml");
out.getChannel().transferFrom(in, 0, Long.MAX_VALUE);
}
catch(Exception e){
//
};
}
}
public static ArrayList<NotificationUpdate> getArrayForAllRSSLinks(String[] rssLinks) throws Exception{
ArrayList<NotificationUpdate> answer=new ArrayList<NotificationUpdate>();
downloadRSS(rssLinks);
for(int x=0;x<rssLinks.length;x++){
File file=new File("rssFile"+x+".xml");
ArrayList<NotificationUpdate> thisRSSUpdateSet=getNotificationArrayList(getNodeList(file,"item"));
answer=mergeTwoArrayLists(answer,thisRSSUpdateSet);
}
return answer;
}
public static ArrayList<NotificationUpdate> getArrayForAllRSSLinks(ArrayList<String> rsslinksArrayList) throws Exception{
String[] rssLinks=new String[rsslinksArrayList.size()];
for(int x=0;x<rsslinksArrayList.size();x++){
rssLinks[x]=rsslinksArrayList.get(x);
}
return getArrayForAllRSSLinks(rssLinks);
}
}