1
|
|
package net.sf.flock.support;
|
2
|
|
|
3
|
|
import java.net.MalformedURLException;
|
4
|
|
import java.net.URL;
|
5
|
|
import java.util.ArrayList;
|
6
|
|
import java.util.Iterator;
|
7
|
|
import java.util.List;
|
8
|
|
|
9
|
|
import net.sf.flock.FeedInfoI;
|
10
|
|
import net.sf.flock.FlockResourceException;
|
11
|
|
import net.sf.flock.MetaData;
|
12
|
|
import net.sf.flock.SubscriptionInfoI;
|
13
|
|
import org.apache.log4j.LogManager;
|
14
|
|
import org.apache.log4j.Logger;
|
15
|
|
import org.jdom.Document;
|
16
|
|
import org.jdom.Element;
|
17
|
|
|
18
|
|
/**
|
19
|
|
* Imports subscriptions from an Radio/AmphetaDesk-style OPML.
|
20
|
|
*
|
21
|
|
* @version $Revision: 1.5 $
|
22
|
|
* @author $Author: phraktle $
|
23
|
|
*/
|
24
|
|
public class OpmlSubscriptionImport {
|
25
|
|
|
26
|
|
private final static Logger LOGGER = LogManager.getLogger( OpmlSubscriptionImport.class );
|
27
|
|
|
28
|
|
private final static String[] KNOWN_ATTRIBUTES = { "path" };
|
29
|
|
|
30
|
|
private final MetaData metaDataTemplate;
|
31
|
|
|
32
|
1
|
public OpmlSubscriptionImport() {
|
33
|
1
|
this(new MetaData());
|
34
|
|
}
|
35
|
|
|
36
|
1
|
public OpmlSubscriptionImport(MetaData metaDataTemplate) {
|
37
|
1
|
this.metaDataTemplate = metaDataTemplate;
|
38
|
|
}
|
39
|
|
|
40
|
1
|
public boolean isSuitable(Document doc) {
|
41
|
1
|
Element root = doc.getRootElement();
|
42
|
1
|
if (!"opml".equals(root.getName())) {
|
43
|
0
|
return false;
|
44
|
|
}
|
45
|
|
// @consider more opml checks
|
46
|
1
|
return true;
|
47
|
|
}
|
48
|
|
|
49
|
|
/**
|
50
|
|
* @return Collection of SubscriptionI
|
51
|
|
*/
|
52
|
1
|
public SubscriptionInfoI[] load(Document opml) throws FlockResourceException {
|
53
|
1
|
Element body = opml.getRootElement().getChild("body");
|
54
|
|
|
55
|
1
|
List subscriptions = new ArrayList();
|
56
|
|
|
57
|
1
|
for (Iterator i = body.getChildren("outline").iterator(); i.hasNext(); ) {
|
58
|
1
|
Element outline = (Element)i.next();
|
59
|
1
|
final URL feedURL;
|
60
|
1
|
try {
|
61
|
1
|
String atr = outline.getAttributeValue("xmlUrl", outline.getAttributeValue("xmlurl"));
|
62
|
1
|
feedURL = new URL( atr );
|
63
|
|
} catch (MalformedURLException e) {
|
64
|
0
|
throw new FlockResourceException(e);
|
65
|
|
}
|
66
|
|
|
67
|
1
|
final MetaData metaData = new MetaData( this.metaDataTemplate );
|
68
|
|
|
69
|
1
|
for (int a=0; a<KNOWN_ATTRIBUTES.length; a++) {
|
70
|
1
|
String value = outline.getAttributeValue( KNOWN_ATTRIBUTES[a] );
|
71
|
1
|
if (value!=null) {
|
72
|
1
|
metaData.set( KNOWN_ATTRIBUTES[a], value);
|
73
|
|
}
|
74
|
|
}
|
75
|
|
|
76
|
1
|
final String title = outline.getAttributeValue("title", "New feed");
|
77
|
|
|
78
|
1
|
String htmlUrl = outline.getAttributeValue("htmlurl", outline.getAttributeValue("htmlUrl"));
|
79
|
1
|
URL siteURL = null;
|
80
|
1
|
if (htmlUrl!=null) {
|
81
|
0
|
try {
|
82
|
0
|
siteURL = new URL(htmlUrl);
|
83
|
|
} catch (MalformedURLException e) {
|
84
|
0
|
LOGGER.warn("Invalid htmlUrl", e);
|
85
|
|
}
|
86
|
|
}
|
87
|
1
|
final URL site = siteURL;
|
88
|
|
|
89
|
|
|
90
|
1
|
subscriptions.add( new SubscriptionInfoI() {
|
91
|
1
|
public URL getLocation() {
|
92
|
1
|
return feedURL;
|
93
|
|
}
|
94
|
|
|
95
|
1
|
public MetaData getMetaData() {
|
96
|
1
|
return metaData;
|
97
|
|
}
|
98
|
|
|
99
|
0
|
public FeedInfoI getFeedInfo() {
|
100
|
0
|
return new FeedInfoI() {
|
101
|
0
|
public String getTitle() {
|
102
|
0
|
return title;
|
103
|
|
}
|
104
|
0
|
public URL getSite() {
|
105
|
0
|
return site;
|
106
|
|
}
|
107
|
0
|
public SubscriptionInfoI getSubscriptionInfo() {
|
108
|
0
|
return null;
|
109
|
|
}
|
110
|
|
};
|
111
|
|
}
|
112
|
|
|
113
|
|
} );
|
114
|
|
}
|
115
|
|
|
116
|
1
|
return (SubscriptionInfoI[])subscriptions.toArray(new SubscriptionInfoI[subscriptions.size()]);
|
117
|
|
}
|
118
|
|
|
119
|
|
}
|
120
|
|
|