Skip to content

Commit 4c35d07

Browse files
author
pborissow
committed
Initial commit of the javaxt rss library
git-svn-id: svn://192.168.0.80/JavaXT/javaxt-rss@53 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 5d27c8c commit 4c35d07

File tree

4 files changed

+463
-0
lines changed

4 files changed

+463
-0
lines changed

src/javaxt/rss/Feed.java

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package javaxt.rss;
2+
3+
import org.w3c.dom.*;
4+
import javaxt.xml.DOM;
5+
import javaxt.geospatial.geometry.Geometry;
6+
import javaxt.geospatial.coordinate.Parser;
7+
8+
//******************************************************************************
9+
//** RSS Feed
10+
//******************************************************************************
11+
/**
12+
* Used to represent an RSS feed/channel. Returns a list of entries and other
13+
* attributes associated with the feed.
14+
*
15+
******************************************************************************/
16+
17+
public class Feed {
18+
19+
private String title = "";
20+
private String description = "";
21+
private java.net.URL link = null;
22+
private Geometry geometry = null;
23+
24+
private javaxt.utils.Date lastUpdate = null;
25+
private Integer interval = null;
26+
27+
private Item[] Items = null;
28+
29+
30+
//**************************************************************************
31+
//** Instantiate Feed
32+
//**************************************************************************
33+
/** Creates a new instance of Feed */
34+
35+
protected Feed(org.w3c.dom.Node node) {
36+
java.util.Vector vec = new java.util.Vector();
37+
NodeList nodeList = node.getChildNodes();
38+
for (int i=0; i<nodeList.getLength(); i++){
39+
node = nodeList.item(i);
40+
String nodeName = node.getNodeName().toLowerCase();
41+
String nodeValue = DOM.getNodeValue(node);
42+
//System.out.println(nodeName + ": " + nodeValue);
43+
if (nodeName.equals("title")) title = nodeValue;
44+
if (nodeName.equals("description") || nodeName.equals("subtitle")){
45+
description = nodeValue;
46+
}
47+
if (nodeName.equals("where") || nodeName.equals("georss:where") ||
48+
nodeName.equals("point") || nodeName.equals("georss:point") ||
49+
nodeName.equals("line") || nodeName.equals("georss:line") ||
50+
nodeName.equals("polygon") || nodeName.equals("georss:polygon") ||
51+
nodeName.equals("box") || nodeName.equals("georss:box")){
52+
try{
53+
geometry = new Parser(nodeValue).getGeometry();
54+
}
55+
catch(Exception e){}
56+
}
57+
58+
59+
if (nodeName.equals("link")){
60+
String url = nodeValue.trim();
61+
if (url.length()==0){
62+
//get href attribute
63+
}
64+
try{
65+
link = new java.net.URL(url);
66+
}
67+
catch(Exception e){}
68+
}
69+
70+
71+
72+
if (nodeName.equals("item") || nodeName.equals("entry")){
73+
vec.add(new Item(node));
74+
}
75+
76+
if (nodeName.equalsIgnoreCase("lastBuildDate")){
77+
if (nodeValue!=null) lastUpdate = new javaxt.utils.Date(nodeValue);
78+
if (lastUpdate.failedToParse()) lastUpdate = null;
79+
}
80+
81+
if (nodeName.equals("ttl")){
82+
try{
83+
interval = javaxt.utils.string.toInt(nodeValue);
84+
}
85+
catch(Exception e){
86+
}
87+
}
88+
89+
}
90+
91+
92+
//Convert Vector to Array
93+
Object[] arr = vec.toArray();
94+
Items = new Item[arr.length];
95+
for (int i=0; i<Items.length; i++){
96+
Items[i] = (Item) arr[i];
97+
}
98+
99+
}
100+
101+
public String getTitle(){ return title; }
102+
public String getDescription(){ return description; }
103+
public java.net.URL getLink(){ return link; }
104+
public Item[] getItems(){ return Items; }
105+
public Geometry getLocation(){ return geometry; }
106+
107+
public javaxt.utils.Date getLastUpdate(){
108+
return lastUpdate;
109+
}
110+
111+
112+
//**************************************************************************
113+
//** getRefreshInterval
114+
//**************************************************************************
115+
/** Returns the number of minutes that the channel can be cached before
116+
* refreshing from the source. Derived from the ttl tag in RSS feeds.
117+
* Returns null if the refresh interval is not specified or unknown.
118+
*/
119+
public Integer getRefreshInterval(){
120+
return interval;
121+
}
122+
123+
public String toString(){
124+
StringBuffer out = new StringBuffer();
125+
String br = "\r\n";
126+
out.append("Title: " + getTitle() + br);
127+
out.append("Description: " + getDescription() + br);
128+
out.append("Last Update: " + getLastUpdate() + br);
129+
out.append("Link: " + getLink() + br);
130+
if (geometry!=null){
131+
out.append("Location: " + geometry + br);
132+
}
133+
return out.toString();
134+
}
135+
136+
}

src/javaxt/rss/Item.java

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package javaxt.rss;
2+
3+
import java.util.ArrayList;
4+
import org.w3c.dom.*;
5+
import javaxt.xml.DOM;
6+
import javaxt.geospatial.geometry.Geometry;
7+
import javaxt.geospatial.coordinate.Parser;
8+
9+
10+
//******************************************************************************
11+
//** RSS Item
12+
//******************************************************************************
13+
/**
14+
* Used to represent an entry in an RSS feed.
15+
*
16+
******************************************************************************/
17+
18+
public class Item {
19+
20+
21+
private String title = "";
22+
private String description = "";
23+
private String author = null;
24+
private String creator = null;
25+
private java.net.URL link = null;
26+
private java.net.URL origLink = null; //<--FeedBurner
27+
private Geometry geometry = null;
28+
private NodeList nodeList = null;
29+
30+
private String pubDate = null;
31+
private String dcDate = null;
32+
33+
private java.util.List<Media> media = new java.util.ArrayList<Media>();
34+
35+
36+
/** Creates a new instance of Item */
37+
protected Item(org.w3c.dom.Node node) {
38+
nodeList = node.getChildNodes();
39+
for (int i=0; i<nodeList.getLength(); i++){
40+
node = nodeList.item(i);
41+
String nodeName = node.getNodeName().toLowerCase();
42+
String nodeValue = DOM.getNodeValue(node).trim();
43+
if (nodeName.equals("title")) title = nodeValue;
44+
45+
//Parse Description
46+
if (nodeName.equals("description") || nodeName.equals("subtitle")){
47+
if (description==null || description.length()==0){
48+
description = nodeValue;
49+
}
50+
}
51+
52+
//Parse Link
53+
if (nodeName.equals("link")){
54+
String url = nodeValue;
55+
if (url.length()==0){
56+
//get href attribute
57+
url = DOM.getAttributeValue(node,"href").trim();
58+
}
59+
if (url.length()>0){
60+
try{ link = new java.net.URL(url); }
61+
catch(Exception e){}
62+
}
63+
}
64+
65+
//Parse FeedBurner Link
66+
if (nodeName.equals("feedburner:origLink")){
67+
String url = nodeValue.trim();
68+
if (url.length()>0){
69+
try{ origLink = new java.net.URL(url); }
70+
catch(Exception e){}
71+
}
72+
}
73+
74+
if (nodeName.equals("author")) author = nodeValue;
75+
if (nodeName.endsWith("creator")) creator = nodeValue;
76+
if (nodeName.equalsIgnoreCase("pubDate")) pubDate = nodeValue;
77+
if (nodeName.equalsIgnoreCase("dc:date")) dcDate = nodeValue;
78+
79+
80+
//Parse Location Information (GeoRSS)
81+
if (nodeName.equals("where") || nodeName.equals("georss:where") ||
82+
nodeName.equals("point") || nodeName.equals("georss:point") ||
83+
nodeName.equals("line") || nodeName.equals("georss:line") ||
84+
nodeName.equals("polygon") || nodeName.equals("georss:polygon") ||
85+
nodeName.equals("box") || nodeName.equals("georss:box")){
86+
try{
87+
geometry = new Parser(nodeValue).getGeometry();
88+
}
89+
catch(Exception e){}
90+
}
91+
92+
93+
if (nodeName.startsWith("media:")){
94+
media.add(new Media(node));
95+
}
96+
}
97+
}
98+
99+
100+
101+
102+
public String getTitle(){ return title; }
103+
public String getDescription(){ return description; }
104+
105+
public String getAuthor(){
106+
if (author==null && creator!=null) return creator;
107+
return author;
108+
}
109+
110+
111+
//**************************************************************************
112+
//** getLink
113+
//**************************************************************************
114+
/** Returns a link/url associated with the current entry. Returns the
115+
* 'feedburner:origLink' if found. Otherwise returns a url associated with
116+
* the 'link' node.
117+
*/
118+
public java.net.URL getLink(){
119+
if (origLink!=null) return origLink;
120+
else return link;
121+
}
122+
123+
124+
//**************************************************************************
125+
//** getDate
126+
//**************************************************************************
127+
/** Return the date/time stamp associated with the current entry. Uses the
128+
* pubDate if it exists. Otherwise, returns dc:date
129+
*/
130+
public javaxt.utils.Date getDate(){
131+
String date = pubDate;
132+
if (date.length()==0) date = dcDate;
133+
if (date.length()>0) return new javaxt.utils.Date(date);
134+
else return null;
135+
}
136+
137+
138+
public Media[] getMedia(){
139+
Media[] arr = new Media[media.size()];
140+
for (int i=0; i<media.size(); i++){
141+
arr[i] = media.get(i);
142+
}
143+
return arr;
144+
}
145+
146+
147+
148+
//**************************************************************************
149+
//** getLocation
150+
//**************************************************************************
151+
/** Returns a geometry associated with the current entry.
152+
*/
153+
public Geometry getLocation(){ return geometry; }
154+
155+
156+
157+
public NodeList getNodeList(){
158+
return nodeList;
159+
}
160+
161+
162+
163+
public String toString(){
164+
StringBuffer out = new StringBuffer();
165+
String br = "\r\n";
166+
out.append("Title: " + getTitle() + br);
167+
//out.append("Description: " + getDescription() + br);
168+
out.append("Author: " + getAuthor() + br);
169+
out.append("Link: " + getLink() + br);
170+
out.append("Date: " + getDate() + br);
171+
172+
if (geometry!=null){
173+
out.append("Location: " + geometry + br);
174+
out.append("Geometry Name: " + geometry.getName() + br);
175+
out.append("Geometry SRS: " + geometry.getSRS() + br);
176+
}
177+
178+
for (int i=0; i<media.size(); i++){
179+
System.out.println(media.get(i));
180+
}
181+
182+
return out.toString();
183+
}
184+
185+
}

src/javaxt/rss/Media.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package javaxt.rss;
2+
import javaxt.xml.DOM;
3+
4+
//******************************************************************************
5+
//** Media Class
6+
//******************************************************************************
7+
/**
8+
* Used to parse a media node associated with an RSS entry
9+
*
10+
******************************************************************************/
11+
12+
public class Media {
13+
14+
private String type;
15+
private java.net.URL url;
16+
private String credit;
17+
private String description;
18+
19+
20+
21+
//**************************************************************************
22+
//** Constructor
23+
//**************************************************************************
24+
/** Creates a new instance of Media. */
25+
26+
protected Media(org.w3c.dom.Node node) {
27+
type = DOM.getAttributeValue(node,"type").trim();
28+
29+
//Parse url
30+
String link = DOM.getAttributeValue(node,"url").trim();
31+
if (link.length()>0){
32+
try{ url = new java.net.URL(link); }
33+
catch(Exception e){}
34+
}
35+
36+
org.w3c.dom.NodeList nodeList = node.getChildNodes();
37+
for (int i=0; i<nodeList.getLength(); i++){
38+
node = nodeList.item(i);
39+
if (node.getNodeType()==1){
40+
String nodeName = node.getNodeName();
41+
if (nodeName.endsWith("credit")){
42+
credit = DOM.getNodeValue(node).trim();
43+
}
44+
else if (nodeName.endsWith("description")){
45+
description = DOM.getNodeValue(node).trim();
46+
}
47+
}
48+
}
49+
}
50+
51+
public String getType(){ return type; }
52+
public String getCredit(){ return credit; }
53+
public String getDescription(){ return description; }
54+
public java.net.URL getLink(){ return url; }
55+
56+
public String toString(){
57+
58+
StringBuffer out = new StringBuffer();
59+
String br = "\r\n";
60+
61+
String mimeType = getType();
62+
if (mimeType.contains("/")) mimeType = mimeType.substring(0, mimeType.indexOf("/"));
63+
mimeType = mimeType.substring(0,1).toUpperCase() + mimeType.substring(1);
64+
out.append(mimeType + ": " + getLink() + br);
65+
out.append("Description: " + getDescription() + br);
66+
out.append("Credit: " + getCredit() + br);
67+
return out.toString();
68+
69+
}
70+
71+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy