File tree

3 files changed

+101
-55
lines changed

3 files changed

+101
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ public class Item {
1313

1414
private String title;
1515
private String description;
16-
private String author = null;
17-
private String creator = null;
18-
private String category = null;
19-
private java.net.URL link = null;
20-
private java.net.URL origLink = null; //<--FeedBurner
21-
private java.util.Date date = null;
22-
private Location location = null;
23-
private NodeList nodeList = null;
16+
private String author;
17+
private String creator;
18+
private String category;
19+
private java.net.URL link;
20+
private java.net.URL origLink; //<--FeedBurner
21+
private java.util.Date date;
22+
private Location location;
2423
private java.util.ArrayList<Media> media = new java.util.ArrayList<Media>();
2524

2625

@@ -45,7 +44,7 @@ public Item(String title, java.net.URL link, java.util.Date date){
4544
//**************************************************************************
4645
/** Creates a new instance of this class using an XML node from an RSS Feed.
4746
*/
48-
protected Item(org.w3c.dom.Node node, java.util.HashMap<String, String> namespaces) {
47+
protected Item(Node item, java.util.HashMap<String, String> namespaces) {
4948

5049
String mediaNS = namespaces.get("http://search.yahoo.com/mrss");
5150
if (mediaNS==null) mediaNS = "media";
@@ -54,15 +53,23 @@ protected Item(org.w3c.dom.Node node, java.util.HashMap<String, String> namespac
5453
if (geoNS==null) geoNS = "geo";
5554

5655

57-
nodeList = node.getChildNodes();
5856
String pubDate = null;
5957
String dcDate = null;
58+
String updated = null;
59+
60+
String description = null;
61+
String subtitle = null;
62+
String summary = null;
63+
6064
String lat = null;
6165
String lon = null;
62-
java.util.ArrayList<org.w3c.dom.Node> mediaNodes = new java.util.ArrayList<org.w3c.dom.Node>();
6366

67+
java.util.ArrayList<Node> mediaNodes = new java.util.ArrayList<Node>();
68+
69+
70+
NodeList nodeList = item.getChildNodes();
6471
for (int i=0; i<nodeList.getLength(); i++){
65-
node = nodeList.item(i);
72+
Node node = nodeList.item(i);
6673
if (node.getNodeType()==1){
6774
String nodeName = node.getNodeName().toLowerCase();
6875
String nodeValue = Parser.getNodeValue(node).trim();
@@ -72,26 +79,30 @@ protected Item(org.w3c.dom.Node node, java.util.HashMap<String, String> namespac
7279
if (nodeName.equals("title")) title = nodeValue;
7380
else if (nodeName.equals("author")) author = nodeValue;
7481
else if (nodeName.endsWith("creator")) creator = nodeValue;
82+
7583
else if (nodeName.equalsIgnoreCase("pubDate")) pubDate = nodeValue;
7684
else if (nodeName.equalsIgnoreCase("dc:date")) dcDate = nodeValue;
77-
else if(nodeName.equals("description") || nodeName.equals("subtitle")){
78-
if (description==null || description.length()==0){
79-
description = nodeValue;
80-
}
81-
}
85+
else if (nodeName.equalsIgnoreCase("updated")) updated = nodeValue;
86+
87+
else if (nodeName.endsWith("description")) description = nodeValue;
88+
else if (nodeName.endsWith("subtitle")) subtitle = nodeValue;
89+
else if (nodeName.endsWith("summary")) summary = nodeValue;
90+
8291

8392
//Parse Link
8493
else if(nodeName.equals("link")){
94+
String url = "";
8595
if (nodeValue!=null){
86-
String url = nodeValue.replace("\"", "");
87-
if (url.length()==0){
88-
//get href attribute
89-
url = Parser.getAttributeValue(node,"href").trim();
90-
}
91-
if (url.length()>0){
92-
try{ link = new java.net.URL(url); }
93-
catch(Exception e){}
94-
}
96+
url = nodeValue.replace("\"", "").trim();
97+
}
98+
99+
if (url.length()==0){
100+
//get href attribute
101+
url = Parser.getAttributeValue(node,"href").trim();
102+
}
103+
if (url.length()>0){
104+
try{ link = new java.net.URL(url); }
105+
catch(Exception e){}
95106
}
96107
}
97108

@@ -131,9 +142,10 @@ else if (nodeName.equals("long") || nodeName.equals(geoNS + ":long")){
131142
}
132143

133144

134-
//Parse date
145+
//Set date
135146
String date = pubDate;
136147
if (date==null || date.length()==0) date = dcDate;
148+
if (date==null || date.length()==0) date = updated;
137149
if (date!=null && date.length()>0){
138150
try{
139151
this.date = Parser.getDate(date);
@@ -143,18 +155,25 @@ else if (nodeName.equals("long") || nodeName.equals(geoNS + ":long")){
143155
}
144156

145157

158+
//Set description
159+
String desc = description;
160+
if (desc==null || desc.length()==0) desc = subtitle;
161+
if (desc==null || desc.length()==0) desc = summary;
162+
this.description = desc;
163+
164+
165+
146166
//Parse media nodes
147167
if (!mediaNodes.isEmpty()){
148168

149169
//Check if there are any content nodes and if those nodes have children (e.g. The Guardian News Feed)
150-
for (org.w3c.dom.Node mediaNode : mediaNodes){
170+
for (Node mediaNode : mediaNodes){
151171
String nodeName = mediaNode.getNodeName().toLowerCase();
152172
if (nodeName.equals(mediaNS + ":content")){
153173

154-
155-
org.w3c.dom.NodeList nodeList = mediaNode.getChildNodes();
156-
for (int i=0; i<nodeList.getLength(); i++){
157-
node = nodeList.item(i);
174+
NodeList nodes = mediaNode.getChildNodes();
175+
for (int i=0; i<nodes.getLength(); i++){
176+
Node node = nodes.item(i);
158177
if (node.getNodeType()==1){
159178
addMedia(new Media(mediaNode));
160179
break;
@@ -167,7 +186,7 @@ else if (nodeName.equals("long") || nodeName.equals(geoNS + ":long")){
167186

168187
//If none of the of the content nodes have children (standard use case)
169188
if (media.isEmpty()){
170-
addMedia(new Media(mediaNodes.toArray(new org.w3c.dom.Node[mediaNodes.size()])));
189+
addMedia(new Media(mediaNodes.toArray(new Node[mediaNodes.size()])));
171190
}
172191
}
173192

@@ -296,16 +315,13 @@ public Media[] getMedia(){
296315
public Location getLocation(){
297316
return location;
298317
}
299-
318+
300319

301320
//**************************************************************************
302-
//** getNodeList
321+
//** setLocation
303322
//**************************************************************************
304-
/** Returns the NodeList used to instantiate this class via the RSS Parser.
305-
* @deprecated This method will be removed in future releases.
306-
*/
307-
public NodeList getNodeList(){
308-
return nodeList;
323+
public void setLocation(Location location){
324+
this.location = location;
309325
}
310326

311327

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package javaxt.rss;
22
import org.w3c.dom.*;
3+
import java.math.BigDecimal;
34

45
//******************************************************************************
56
//** Location Class
@@ -14,8 +15,8 @@ public class Location {
1415

1516
private org.w3c.dom.Node node;
1617
private Object geometry;
17-
private String lat;
18-
private String lon;
18+
private BigDecimal lat;
19+
private BigDecimal lon;
1920
private Boolean hasGeometry = null; //Has 3 states: true, false, and null
2021

2122
private static String[] SupportedGeometryTypes = new String[]{
@@ -51,11 +52,14 @@ protected Location(org.w3c.dom.Node node, java.util.HashMap<String, String> name
5152
//**************************************************************************
5253
/** Creates a new instance of this class using a point. */
5354

54-
protected Location(String lat, String lon){
55+
public Location(BigDecimal lat, BigDecimal lon){
5556
this.lat = lat;
5657
this.lon = lon;
5758
}
5859

60+
protected Location(String lat, String lon){
61+
this(new BigDecimal(lat), new BigDecimal(lon));
62+
}
5963

6064
/*
6165
public String toGML(){
Original file line numberDiff line numberDiff line change
@@ -293,23 +293,49 @@ protected static java.util.Date getDate(String date) throws java.text.ParseExcep
293293
return (java.util.Date) method.invoke(instance);
294294
}
295295
catch(Exception e){
296-
297-
298-
//Special Case: Java fails to parse the "T" in strings like
299-
//"1976-06-07T01:02:09.000" and "1976-06-07T13:02-0500"
300-
if (date.length()>="1976-06-07T13:02".length()){
301-
if (date.substring(10, 11).equalsIgnoreCase("T")){
302-
date = date.replace("T", " ");
303-
}
304-
}
305296

306297

307298
//Loop through all known date formats and try to convert the string to a date
308299
for (String format : SupportedFormats){
309300

310-
//Special Case: Java fails to parse the "Z" in "1976-06-07 00:00:00Z"
311-
if (date.endsWith("Z") && format.endsWith("Z")){
312-
date = date.substring(0, date.length()-1) + "UTC";
301+
if (format.endsWith("Z")){
302+
303+
//Special Case: Java fails to parse the "T" in strings like
304+
//"1976-06-07T01:02:09.000" and "1976-06-07T13:02-0500"
305+
int idx = date.indexOf("T");
306+
if (idx==10 && format.startsWith("yyyy-MM-dd HH:mm")){
307+
date = date.substring(0, idx) + " " + date.substring(idx+1);
308+
}
309+
310+
311+
312+
if (date.endsWith("Z") && date.length()==format.length()){
313+
//If the date literally ends with the letter "Z", then the
314+
//date is probably referencing "Zulu" timezone (i.e. UTC).
315+
//Example: "1976-06-07 00:00:00Z". Java doesn't understand
316+
//what the "Z" timezone is so we'll replace the "Z" with
317+
//"UTC".
318+
date = date.substring(0, date.length()-1) + "UTC";
319+
}
320+
else{
321+
322+
323+
//Check if the timezone offset is specified in "+/-HH:mm"
324+
//format (e.g. "2018-01-17T01:00:35+07:00"). If so, update
325+
//the timezone offset by removing the colon.
326+
if (date.length()>=format.length()){
327+
int len = format.length()-1;
328+
String tz = date.substring(len);
329+
if (tz.length()==6){
330+
String a = tz.substring(0,1);
331+
if ((a.equals("-") || a.equals("+")) && tz.indexOf(":")==3){
332+
tz = tz.replace(":", "");
333+
date = date.substring(0, len) + tz;
334+
}
335+
}
336+
337+
}
338+
}
313339
}
314340

315341
try{

0 commit comments

Comments
 (0)