-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.java
More file actions
407 lines (322 loc) · 13.9 KB
/
Item.java
File metadata and controls
407 lines (322 loc) · 13.9 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package javaxt.rss;
import org.w3c.dom.*;
//******************************************************************************
//** RSS Item
//******************************************************************************
/**
* Used to represent an entry in an RSS feed.
*
******************************************************************************/
public class Item {
private String title;
private String description;
private String author;
private String creator;
private String category;
private java.net.URL link;
private java.net.URL origLink; //<--FeedBurner
private java.util.Date date;
private Location location;
private java.util.ArrayList<Media> media = new java.util.ArrayList<Media>();
//**************************************************************************
//** Constructor
//**************************************************************************
public Item(){}
//**************************************************************************
//** Constructor
//**************************************************************************
public Item(String title, java.net.URL link, java.util.Date date){
this.title = title;
this.link = link;
this.date = date;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using an XML node from an RSS Feed.
*/
protected Item(Node item, java.util.HashMap<String, String> namespaces) {
String mediaNS = namespaces.get("http://search.yahoo.com/mrss");
if (mediaNS==null) mediaNS = "media";
String geoNS = namespaces.get("http://www.w3.org/2003/01/geo/wgs84_pos#");
if (geoNS==null) geoNS = "geo";
String pubDate = null;
String dcDate = null;
String updated = null;
String description = null;
String subtitle = null;
String summary = null;
String lat = null;
String lon = null;
java.util.ArrayList<Node> mediaNodes = new java.util.ArrayList<Node>();
NodeList nodeList = item.getChildNodes();
for (int i=0; i<nodeList.getLength(); i++){
Node node = nodeList.item(i);
if (node.getNodeType()==1){
String nodeName = node.getNodeName().toLowerCase();
String nodeValue = Parser.getNodeValue(node).trim();
if (nodeValue.length()==0) nodeValue = null;
//Parse Common Attributes
if (nodeName.equals("title")) title = nodeValue;
else if (nodeName.equals("author")) author = nodeValue;
else if (nodeName.endsWith("creator")) creator = nodeValue;
else if (nodeName.equalsIgnoreCase("pubDate")) pubDate = nodeValue;
else if (nodeName.equalsIgnoreCase("dc:date")) dcDate = nodeValue;
else if (nodeName.equalsIgnoreCase("updated")) updated = nodeValue;
else if (nodeName.endsWith("description")) description = nodeValue;
else if (nodeName.endsWith("subtitle")) subtitle = nodeValue;
else if (nodeName.endsWith("summary")) summary = nodeValue;
//Parse Link
else if(nodeName.equals("link")){
String url = "";
if (nodeValue!=null){
url = nodeValue.replace("\"", "").trim();
}
if (url.length()==0){
//get href attribute
url = Parser.getAttributeValue(node,"href").trim();
}
if (url.length()>0){
try{ link = new java.net.URL(url); }
catch(Exception e){}
}
}
//Parse FeedBurner Link
else if(nodeName.equals("feedburner:origLink")){
if (nodeValue!=null){
try{ origLink = new java.net.URL(nodeValue); }
catch(Exception e){}
}
}
//Enclosure (e.g. TASS News agency)
else if (nodeName.equals("enclosure")){
addMedia(new Media(node));
}
else if (nodeName.equals("category")){
category = nodeValue;
}
else if(Location.isLocationNode(nodeName, namespaces)){
location = new Location(node, namespaces);
}
else if (nodeName.equals("lat") || nodeName.equals(geoNS + ":lat")){
lat = nodeValue;
}
else if (nodeName.equals("long") || nodeName.equals(geoNS + ":long")){
lon = nodeValue;
}
else{
if (nodeName.startsWith(mediaNS + ":")){
mediaNodes.add(node);
}
}
}
}
//Set date
String date = pubDate;
if (date==null || date.length()==0) date = dcDate;
if (date==null || date.length()==0) date = updated;
if (date!=null && date.length()>0){
try{
this.date = Parser.getDate(date);
}
catch(java.text.ParseException e){
}
}
//Set description
String desc = description;
if (desc==null || desc.length()==0) desc = subtitle;
if (desc==null || desc.length()==0) desc = summary;
this.description = desc;
//Parse media nodes
if (!mediaNodes.isEmpty()){
//Check if there are any content nodes and if those nodes have children (e.g. The Guardian News Feed)
for (Node mediaNode : mediaNodes){
String nodeName = mediaNode.getNodeName().toLowerCase();
if (nodeName.equals(mediaNS + ":content")){
NodeList nodes = mediaNode.getChildNodes();
for (int i=0; i<nodes.getLength(); i++){
Node node = nodes.item(i);
if (node.getNodeType()==1){
addMedia(new Media(mediaNode));
break;
}
}
}
}
//If none of the of the content nodes have children (standard use case)
if (media.isEmpty()){
addMedia(new Media(mediaNodes.toArray(new Node[mediaNodes.size()])));
}
}
//Set location
if (lat!=null && lon!=null){
location = new Location(lat, lon);
}
}
//**************************************************************************
//** 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;
}
//**************************************************************************
//** getAuthor
//**************************************************************************
public String getAuthor(){
if (author==null && creator!=null) return creator;
return author;
}
public void setAuthor(String author){
this.author = author;
}
//**************************************************************************
//** getCategory
//**************************************************************************
public String getCategory(){
return category;
}
public void setCategory(String category){
this.category = category;
}
//**************************************************************************
//** getLink
//**************************************************************************
/** Returns a link/url associated with the current entry. Returns the
* 'feedburner:origLink' if found. Otherwise returns a url associated with
* the 'link' node.
*/
public java.net.URL getLink(){
if (origLink!=null) return origLink;
else return link;
}
//**************************************************************************
//** setLink
//**************************************************************************
public void setLink(java.net.URL url){
origLink = null;
link = url;
}
//**************************************************************************
//** getDate
//**************************************************************************
/** Return the date/time stamp associated with the current entry. Uses the
* pubDate if it exists. Otherwise, returns dc:date
*/
public java.util.Date getDate(){
return date;
}
//**************************************************************************
//** setDate
//**************************************************************************
public void setDate(java.util.Date date){
this.date = date;
}
//**************************************************************************
//** addMedia
//**************************************************************************
public void addMedia(Media media){
this.media.add(media);
}
//**************************************************************************
//** getMedia
//**************************************************************************
/** Returns an array of media items associated with the current entry.
*/
public Media[] getMedia(){
return media.toArray(new Media[media.size()]);
}
//**************************************************************************
//** getLocation
//**************************************************************************
/** Returns location information associated with the current entry (e.g.
* GeoRSS element).
*/
public Location getLocation(){
return location;
}
//**************************************************************************
//** setLocation
//**************************************************************************
public void setLocation(Location location){
this.location = location;
}
//**************************************************************************
//** toString
//**************************************************************************
public String toString(){
StringBuffer out = new StringBuffer();
String br = "\r\n";
out.append("Title: " + getTitle() + br);
//out.append("Description: " + getDescription() + br);
out.append("Author: " + getAuthor() + br);
out.append("Link: " + getLink() + br);
out.append("Date: " + getDate() + br);
if (location!=null){
out.append("Location: " + location.toWKT() + br);
//out.append("Geometry Name: " + geometry.getName() + br);
//out.append("Geometry SRS: " + geometry.getSRS() + br);
}
for (int i=0; i<media.size(); i++){
System.out.println(media.get(i));
}
return out.toString();
}
//**************************************************************************
//** toXML
//**************************************************************************
/** Returns an XML fragment used by the Feed class to generate an RSS/XML
* document.
*/
protected String toXML(){
StringBuffer str = new StringBuffer();
java.util.HashMap<String, Object> info = new java.util.HashMap<String, Object>();
String title = getTitle();
if (title!=null){
title = title.trim();
if (title.length()>0) info.put("title", title);
}
String desc = getDescription();
if (desc!=null){
desc = desc.trim();
if (desc.length()>0) info.put("description", desc);
}
if (link!=null) info.put("link", link);
if (date!=null) info.put("pubDate", date);
if (!info.isEmpty()){
str.append(" <item>\n");
java.util.Iterator<String> it = info.keySet().iterator();
while (it.hasNext()){
String key = it.next();
Object val = info.get(key);
str.append(" <" + key + ">");
if (val instanceof String){
str.append("<![CDATA[");
str.append(val);
str.append("]]>");
}
else if (val instanceof java.util.Date){
String d = Feed.formatDate((java.util.Date) val);
str.append(d);
}
else{
str.append(val);
}
str.append("</" + key + ">\n");
}
str.append(" </item>\n");
}
return str.toString();
}
}