-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeed.java
More file actions
237 lines (196 loc) · 9.21 KB
/
Feed.java
File metadata and controls
237 lines (196 loc) · 9.21 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package javaxt.rss;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
//******************************************************************************
//** RSS Feed
//******************************************************************************
/**
* Used to represent an RSS feed/channel. Returns a list of entries and other
* attributes associated with the feed.
*
******************************************************************************/
public class Feed {
private String title = "";
private String description = "";
private java.net.URL link = null;
private Location location = null;
private java.util.Date lastUpdate = null;
private Integer interval = null;
private java.util.ArrayList<Item> items = new java.util.ArrayList<Item>();
//**************************************************************************
//** Constructor
//**************************************************************************
public Feed(){}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using an XML node from an RSS XML
* document.
*/
protected Feed(org.w3c.dom.Node node, java.util.HashMap<String, String> namespaces) {
//java.util.ArrayList<Item> items = new java.util.ArrayList<Item>();
NodeList nodeList = node.getChildNodes();
for (int i=0; i<nodeList.getLength(); i++){
node = nodeList.item(i);
if (node.getNodeType()==1){
String nodeName = node.getNodeName().toLowerCase();
String nodeValue = Parser.getNodeValue(node).trim();
if (nodeName.equals("title")) title = nodeValue;
else if(nodeName.equals("description") || nodeName.equals("subtitle")){
description = nodeValue;
}
//Parse Location Information (GeoRSS)
else if (Location.isLocationNode(nodeName, namespaces)){
location = new Location(node, namespaces);
}
else if (nodeName.equals("link")){
String url = nodeValue.trim();
if (url.length()==0){
//get href attribute?
}
try{
link = new java.net.URL(url);
}
catch(Exception e){}
}
else if (nodeName.equals("item") || nodeName.equals("entry")){
items.add(new Item(node, namespaces));
}
else if (nodeName.equalsIgnoreCase("lastBuildDate")){ //pubDate?
try{
lastUpdate = Parser.getDate(nodeValue);
}
catch(java.text.ParseException e){
}
}
else if (nodeName.equals("ttl")){
try{
interval = Integer.parseInt(nodeValue);
}
catch(Exception e){
}
}
}
}
}
//**************************************************************************
//** getTitle
//**************************************************************************
public String getTitle(){ return title; }
public void setTitle(String title){ this.title = title; }
//**************************************************************************
//** getDescription
//**************************************************************************
public String getDescription(){ return description; }
public void setDescription(String description){ this.description = description; }
//**************************************************************************
//** getLink
//**************************************************************************
public java.net.URL getLink(){ return link; }
public void setLink(java.net.URL link){ this.link = link; }
//**************************************************************************
//** getLocation
//**************************************************************************
/** Returns location information associated with the current feed (e.g.
* GeoRSS element).
*/
public Location getLocation(){
return location;
}
//**************************************************************************
//** getLastUpdate
//**************************************************************************
public java.util.Date getLastUpdate(){ return lastUpdate; }
public void setLastUpdate(java.util.Date date){ this.lastUpdate = date; }
//**************************************************************************
//** getItems
//**************************************************************************
/** Returns a list of items found in an RSS feed.
*/
public Item[] getItems(){
return items.toArray(new Item[items.size()]);
}
//**************************************************************************
//** addItem
//**************************************************************************
/** Used to add an RSS item to the Feed.
*/
public void addItem(Item item){
items.add(item);
}
//**************************************************************************
//** getRefreshInterval
//**************************************************************************
/** Returns the number of minutes that the channel can be cached before
* refreshing from the source. Derived from the ttl tag in RSS feeds.
* Returns null if the refresh interval is not specified or unknown.
*/
public Integer getRefreshInterval(){
return interval;
}
public void setRefreshInterval(Integer interval){
this.interval = interval;
}
//**************************************************************************
//** toString
//**************************************************************************
public String toString(){
StringBuffer out = new StringBuffer();
String br = "\r\n";
out.append("Title: " + getTitle() + br);
out.append("Description: " + getDescription() + br);
out.append("Last Update: " + getLastUpdate() + br);
out.append("Link: " + getLink() + br);
if (location!=null){
out.append("Location: " + location.toWKT() + br);
}
return out.toString();
}
//**************************************************************************
//** toXML
//**************************************************************************
/** Returns an RSS/XML document for this Feed.
*/
public org.w3c.dom.Document toXML(){
StringBuffer str = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
str.append("<rss xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:atom=\"http://www.w3.org/2005/Atom\" version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss/\">\n");
str.append(" <channel>\n");
str.append(" <title>" + getTitle() + "</title>\n");
str.append(" <link>" + getLink() + "</link>\n");
str.append(" <lastBuildDate>" + formatDate(lastUpdate==null ? new java.util.Date() : lastUpdate) + "</lastBuildDate>\n");
str.append(" <generator>JavaXT RSS</generator>\n");
str.append(" <ttl>" + (interval==null ? 15 : interval) + "</ttl>\n");
for (Item item : items){
str.append(item.toXML());
}
str.append(" </channel>\n");
str.append("</rss>");
return createDocument(str.toString());
}
//**************************************************************************
//** createDocument
//**************************************************************************
private Document createDocument(String xml){
try{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
return builder.parse(is);
}
catch(Exception e){
//e.printStackTrace();
return null;
}
}
//**************************************************************************
//** formatDate
//**************************************************************************
protected static String formatDate(java.util.Date date){
java.text.DateFormat formatter = new java.text.SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss zzz", java.util.Locale.US);
return formatter.format(date);
}
}