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.ParseException;
|
1759
|
25 |
import java.text.SimpleDateFormat;
|
1721
|
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;
|
1824
|
41 |
import com.nokia.carbide.discovery.ui.Messages;
|
1751
|
42 |
|
1721
|
43 |
/**
|
|
44 |
* A simple RSS reader
|
|
45 |
* @see http://www.rssboard.org/rss-specification
|
|
46 |
*/
|
|
47 |
public class SimpleRSSReader {
|
|
48 |
|
|
49 |
public static class Rss {
|
|
50 |
private List<Channel> channels = new ArrayList<Channel>();
|
|
51 |
|
|
52 |
public List<Channel> getChannels() {
|
|
53 |
return channels;
|
|
54 |
}
|
|
55 |
|
|
56 |
protected void addChannel(Channel channel) {
|
|
57 |
channels.add(channel);
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
public static class Container {
|
|
62 |
private String title;
|
|
63 |
private URL link;
|
|
64 |
private String description;
|
|
65 |
private Date pubDate;
|
|
66 |
private List<String> categories = new ArrayList<String>();
|
|
67 |
|
|
68 |
public String getTitle() {
|
|
69 |
return title;
|
|
70 |
}
|
|
71 |
|
|
72 |
public URL getLink() {
|
|
73 |
return link;
|
|
74 |
}
|
|
75 |
|
|
76 |
public String getDescription() {
|
|
77 |
return description;
|
|
78 |
}
|
|
79 |
|
|
80 |
public Date getPubDate() {
|
|
81 |
return pubDate;
|
|
82 |
}
|
|
83 |
|
|
84 |
public String[] getCategories() {
|
|
85 |
return (String[]) categories.toArray(new String[categories.size()]);
|
|
86 |
}
|
|
87 |
|
|
88 |
public boolean isValid() {
|
|
89 |
return title != null && link != null && description != null;
|
|
90 |
}
|
|
91 |
|
|
92 |
protected void setField(String element, String value) {
|
1814
|
93 |
if (RSSHandler.TITLE.equalsIgnoreCase(element))
|
1721
|
94 |
title = value;
|
1814
|
95 |
else if (RSSHandler.LINK.equalsIgnoreCase(element)) {
|
1721
|
96 |
try {
|
|
97 |
link = new URL(value);
|
|
98 |
} catch (MalformedURLException e) {
|
|
99 |
// don't store malformed URLs
|
1824
|
100 |
Activator.logError(Messages.SimpleRSSReader_BadURLError, e);
|
1721
|
101 |
}
|
|
102 |
}
|
1814
|
103 |
else if (RSSHandler.DESCRIPTION.equalsIgnoreCase(element) || RSSHandler.SUMMARY.equalsIgnoreCase(element))
|
1721
|
104 |
description = value;
|
1814
|
105 |
else if (RSSHandler.PUBDATE.equalsIgnoreCase(element)) {
|
1759
|
106 |
pubDate = parseRFC822Date(value);
|
1721
|
107 |
}
|
1814
|
108 |
else if (RSSHandler.CATEGORY.equalsIgnoreCase(element))
|
1721
|
109 |
categories.add(value);
|
|
110 |
}
|
1759
|
111 |
|
|
112 |
private Date parseRFC822Date(String value) {
|
|
113 |
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); //$NON-NLS-1$
|
|
114 |
try {
|
|
115 |
return format.parse(value);
|
|
116 |
} catch (ParseException e) {
|
|
117 |
}
|
|
118 |
return null;
|
|
119 |
}
|
1721
|
120 |
}
|
|
121 |
|
|
122 |
public static class Channel extends Container {
|
|
123 |
List<Item> items = new ArrayList<Item>();
|
|
124 |
|
|
125 |
public List<Item> getItems() {
|
|
126 |
return items;
|
|
127 |
}
|
|
128 |
|
|
129 |
protected void addItem(Item item) {
|
|
130 |
items.add(item);
|
|
131 |
}
|
|
132 |
|
|
133 |
public boolean hasItems() {
|
|
134 |
return !items.isEmpty();
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
public static class Item extends Container {
|
|
139 |
private Channel channel;
|
|
140 |
|
|
141 |
public Item(Channel channel) {
|
|
142 |
this.channel = channel;
|
|
143 |
}
|
|
144 |
|
|
145 |
public Channel getChannel() {
|
|
146 |
return channel;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
private static class RSSHandler extends DefaultHandler {
|
|
151 |
|
|
152 |
private static final String CHANNEL = "channel"; //$NON-NLS-1$
|
|
153 |
private static final String TITLE = "title"; //$NON-NLS-1$
|
|
154 |
private static final String LINK = "link"; //$NON-NLS-1$
|
|
155 |
private static final String DESCRIPTION = "description"; //$NON-NLS-1$
|
|
156 |
private static final String SUMMARY = "atom:summary"; //$NON-NLS-1$
|
|
157 |
private static final String ITEM = "item"; //$NON-NLS-1$
|
|
158 |
private static final String PUBDATE = "pubDate"; //$NON-NLS-1$
|
|
159 |
private static final String CATEGORY = "category"; //$NON-NLS-1$
|
|
160 |
private static final Set<String> charsElements = new HashSet<String>();
|
|
161 |
static {
|
1814
|
162 |
charsElements.add(TITLE.toLowerCase());
|
|
163 |
charsElements.add(LINK.toLowerCase());
|
|
164 |
charsElements.add(DESCRIPTION.toLowerCase());
|
|
165 |
charsElements.add(SUMMARY.toLowerCase());
|
|
166 |
charsElements.add(PUBDATE.toLowerCase());
|
|
167 |
charsElements.add(CATEGORY.toLowerCase());
|
1721
|
168 |
}
|
|
169 |
|
|
170 |
private Channel curChannel;
|
|
171 |
private Item curItem;
|
|
172 |
private StringBuffer charsBuf;
|
|
173 |
private Rss rss;
|
|
174 |
|
|
175 |
public RSSHandler(Rss rss) {
|
|
176 |
this.rss = rss;
|
|
177 |
}
|
|
178 |
|
|
179 |
@Override
|
|
180 |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
1814
|
181 |
if (charsElements.contains(qName.toLowerCase())) {
|
1721
|
182 |
charsBuf = new StringBuffer();
|
|
183 |
}
|
1814
|
184 |
if (CHANNEL.equalsIgnoreCase(qName)) {
|
1721
|
185 |
curChannel = new Channel();
|
|
186 |
}
|
1814
|
187 |
else if (ITEM.equalsIgnoreCase(qName) && curChannel != null) {
|
1721
|
188 |
curItem = new Item(curChannel);
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
@Override
|
|
193 |
public void endElement(String uri, String localName, String qName) throws SAXException {
|
1814
|
194 |
if (CHANNEL.equalsIgnoreCase(qName)) {
|
1721
|
195 |
if (curChannel != null && curChannel.isValid() && curChannel.hasItems())
|
|
196 |
rss.addChannel(curChannel);
|
|
197 |
curChannel = null;
|
|
198 |
curItem = null;
|
|
199 |
}
|
1814
|
200 |
else if (ITEM.equalsIgnoreCase(qName)) {
|
1721
|
201 |
if (curChannel != null && curItem != null && curItem.isValid())
|
|
202 |
curChannel.addItem(curItem);
|
|
203 |
curItem = null;
|
|
204 |
}
|
|
205 |
else if (charsBuf != null && curChannel != null) {
|
|
206 |
if (curItem != null)
|
|
207 |
curItem.setField(qName, charsBuf.toString().trim());
|
|
208 |
else
|
|
209 |
curChannel.setField(qName, charsBuf.toString().trim());
|
|
210 |
charsBuf = null;
|
|
211 |
}
|
|
212 |
}
|
|
213 |
|
|
214 |
@Override
|
|
215 |
public void characters(char[] ch, int start, int length) throws SAXException {
|
|
216 |
if (charsBuf != null) {
|
|
217 |
charsBuf.append(new String(ch, start, length));
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
|
221 |
}
|
|
222 |
|
|
223 |
public static Rss readRSS(URL url) throws SAXException, IOException, ParserConfigurationException {
|
|
224 |
Rss rss = new Rss();
|
|
225 |
URLConnection connection = url.openConnection();
|
|
226 |
connection.connect();
|
|
227 |
InputStream inputStream = url.openStream();
|
|
228 |
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
|
|
229 |
parser.parse(inputStream, new RSSHandler(rss));
|
|
230 |
return rss;
|
|
231 |
}
|
1759
|
232 |
|
|
233 |
|
1721
|
234 |
}
|