1721
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.carbide.internal.discovery.ui.editor;
|
|
18 |
|
|
19 |
import java.io.IOException;
|
|
20 |
import java.io.InputStream;
|
|
21 |
import java.net.MalformedURLException;
|
|
22 |
import java.net.URL;
|
|
23 |
import java.net.URLConnection;
|
|
24 |
import java.text.DateFormat;
|
|
25 |
import java.text.ParseException;
|
|
26 |
import java.util.ArrayList;
|
|
27 |
import java.util.Date;
|
|
28 |
import java.util.HashSet;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.Set;
|
|
31 |
|
|
32 |
import javax.xml.parsers.ParserConfigurationException;
|
|
33 |
import javax.xml.parsers.SAXParser;
|
|
34 |
import javax.xml.parsers.SAXParserFactory;
|
|
35 |
|
|
36 |
import org.xml.sax.Attributes;
|
|
37 |
import org.xml.sax.SAXException;
|
|
38 |
import org.xml.sax.helpers.DefaultHandler;
|
|
39 |
|
1751
|
40 |
import com.nokia.carbide.discovery.ui.Activator;
|
|
41 |
|
1721
|
42 |
/**
|
|
43 |
* A simple RSS reader
|
|
44 |
* @see http://www.rssboard.org/rss-specification
|
|
45 |
*/
|
|
46 |
public class SimpleRSSReader {
|
|
47 |
|
|
48 |
public static class Rss {
|
|
49 |
private List<Channel> channels = new ArrayList<Channel>();
|
|
50 |
|
|
51 |
public List<Channel> getChannels() {
|
|
52 |
return channels;
|
|
53 |
}
|
|
54 |
|
|
55 |
protected void addChannel(Channel channel) {
|
|
56 |
channels.add(channel);
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 |
public static class Container {
|
|
61 |
private String title;
|
|
62 |
private URL link;
|
|
63 |
private String description;
|
|
64 |
private Date pubDate;
|
|
65 |
private List<String> categories = new ArrayList<String>();
|
|
66 |
|
|
67 |
public String getTitle() {
|
|
68 |
return title;
|
|
69 |
}
|
|
70 |
|
|
71 |
public URL getLink() {
|
|
72 |
return link;
|
|
73 |
}
|
|
74 |
|
|
75 |
public String getDescription() {
|
|
76 |
return description;
|
|
77 |
}
|
|
78 |
|
|
79 |
public Date getPubDate() {
|
|
80 |
return pubDate;
|
|
81 |
}
|
|
82 |
|
|
83 |
public String[] getCategories() {
|
|
84 |
return (String[]) categories.toArray(new String[categories.size()]);
|
|
85 |
}
|
|
86 |
|
|
87 |
public boolean isValid() {
|
|
88 |
return title != null && link != null && description != null;
|
|
89 |
}
|
|
90 |
|
|
91 |
protected void setField(String element, String value) {
|
|
92 |
if (RSSHandler.TITLE.equals(element))
|
|
93 |
title = value;
|
|
94 |
else if (RSSHandler.LINK.equals(element)) {
|
|
95 |
try {
|
|
96 |
link = new URL(value);
|
|
97 |
} catch (MalformedURLException e) {
|
|
98 |
// don't store malformed URLs
|
1751
|
99 |
Activator.logError("Bad URL", e);
|
1721
|
100 |
}
|
|
101 |
}
|
|
102 |
else if (RSSHandler.DESCRIPTION.equals(element) || RSSHandler.SUMMARY.equals(element))
|
|
103 |
description = value;
|
|
104 |
else if (RSSHandler.PUBDATE.equals(element)) {
|
|
105 |
try {
|
|
106 |
// FIXME parser needs writing!!
|
1751
|
107 |
DateFormat dateFormat = DateFormat.getInstance();
|
|
108 |
dateFormat.setLenient(true);
|
|
109 |
pubDate = dateFormat.parse(value);
|
1721
|
110 |
} catch (ParseException e) {
|
|
111 |
// don't store malformed dates
|
|
112 |
}
|
|
113 |
}
|
|
114 |
else if (RSSHandler.CATEGORY.equals(element))
|
|
115 |
categories.add(value);
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
public static class Channel extends Container {
|
|
120 |
List<Item> items = new ArrayList<Item>();
|
|
121 |
|
|
122 |
public List<Item> getItems() {
|
|
123 |
return items;
|
|
124 |
}
|
|
125 |
|
|
126 |
protected void addItem(Item item) {
|
|
127 |
items.add(item);
|
|
128 |
}
|
|
129 |
|
|
130 |
public boolean hasItems() {
|
|
131 |
return !items.isEmpty();
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
public static class Item extends Container {
|
|
136 |
private Channel channel;
|
|
137 |
|
|
138 |
public Item(Channel channel) {
|
|
139 |
this.channel = channel;
|
|
140 |
}
|
|
141 |
|
|
142 |
public Channel getChannel() {
|
|
143 |
return channel;
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
private static class RSSHandler extends DefaultHandler {
|
|
148 |
|
|
149 |
private static final String CHANNEL = "channel"; //$NON-NLS-1$
|
|
150 |
private static final String TITLE = "title"; //$NON-NLS-1$
|
|
151 |
private static final String LINK = "link"; //$NON-NLS-1$
|
|
152 |
private static final String DESCRIPTION = "description"; //$NON-NLS-1$
|
|
153 |
private static final String SUMMARY = "atom:summary"; //$NON-NLS-1$
|
|
154 |
private static final String ITEM = "item"; //$NON-NLS-1$
|
|
155 |
private static final String PUBDATE = "pubDate"; //$NON-NLS-1$
|
|
156 |
private static final String CATEGORY = "category"; //$NON-NLS-1$
|
|
157 |
private static final Set<String> charsElements = new HashSet<String>();
|
|
158 |
static {
|
|
159 |
charsElements.add(TITLE);
|
|
160 |
charsElements.add(LINK);
|
|
161 |
charsElements.add(DESCRIPTION);
|
|
162 |
charsElements.add(SUMMARY);
|
|
163 |
charsElements.add(PUBDATE);
|
|
164 |
charsElements.add(CATEGORY);
|
|
165 |
}
|
|
166 |
|
|
167 |
private Channel curChannel;
|
|
168 |
private Item curItem;
|
|
169 |
private StringBuffer charsBuf;
|
|
170 |
private Rss rss;
|
|
171 |
|
|
172 |
public RSSHandler(Rss rss) {
|
|
173 |
this.rss = rss;
|
|
174 |
}
|
|
175 |
|
|
176 |
@Override
|
|
177 |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
|
178 |
if (charsElements.contains(qName)) {
|
|
179 |
charsBuf = new StringBuffer();
|
|
180 |
}
|
|
181 |
if (CHANNEL.equals(qName)) {
|
|
182 |
curChannel = new Channel();
|
|
183 |
}
|
|
184 |
else if (ITEM.equals(qName) && curChannel != null) {
|
|
185 |
curItem = new Item(curChannel);
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
@Override
|
|
190 |
public void endElement(String uri, String localName, String qName) throws SAXException {
|
|
191 |
if (CHANNEL.equals(qName)) {
|
|
192 |
if (curChannel != null && curChannel.isValid() && curChannel.hasItems())
|
|
193 |
rss.addChannel(curChannel);
|
|
194 |
curChannel = null;
|
|
195 |
curItem = null;
|
|
196 |
}
|
|
197 |
else if (ITEM.equals(qName)) {
|
|
198 |
if (curChannel != null && curItem != null && curItem.isValid())
|
|
199 |
curChannel.addItem(curItem);
|
|
200 |
curItem = null;
|
|
201 |
}
|
|
202 |
else if (charsBuf != null && curChannel != null) {
|
|
203 |
if (curItem != null)
|
|
204 |
curItem.setField(qName, charsBuf.toString().trim());
|
|
205 |
else
|
|
206 |
curChannel.setField(qName, charsBuf.toString().trim());
|
|
207 |
charsBuf = null;
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
|
211 |
@Override
|
|
212 |
public void characters(char[] ch, int start, int length) throws SAXException {
|
|
213 |
if (charsBuf != null) {
|
|
214 |
charsBuf.append(new String(ch, start, length));
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
}
|
|
219 |
|
|
220 |
|
|
221 |
public static Rss readRSS(URL url) throws SAXException, IOException, ParserConfigurationException {
|
|
222 |
Rss rss = new Rss();
|
|
223 |
URLConnection connection = url.openConnection();
|
|
224 |
connection.connect();
|
|
225 |
InputStream inputStream = url.openStream();
|
|
226 |
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
|
|
227 |
parser.parse(inputStream, new RSSHandler(rss));
|
|
228 |
return rss;
|
|
229 |
}
|
|
230 |
}
|