summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/feeds/parser/SimpleFeedParser.java
blob: afb1b7cb201a7d4d0ff94bbf5a40685df8ea2c74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.feeds.parser;

import android.util.Log;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import ch.boye.httpclientandroidlib.util.TextUtils;

/**
 * A super simple feed parser written for implementing "content notifications". This XML Pull Parser
 * can read ATOM and RSS feeds and returns an object describing the feed and the latest entry.
 */
public class SimpleFeedParser {
    /**
     * Generic exception that's thrown by the parser whenever a stream cannot be parsed.
     */
    public static class ParserException extends Exception {
        private static final long serialVersionUID = -6119538440219805603L;

        public ParserException(Throwable cause) {
            super(cause);
        }

        public ParserException(String message) {
            super(message);
        }
    }

    private static final String LOGTAG = "Gecko/FeedParser";

    private static final String TAG_RSS = "rss";
    private static final String TAG_FEED = "feed";
    private static final String TAG_RDF = "RDF";
    private static final String TAG_TITLE = "title";
    private static final String TAG_ITEM = "item";
    private static final String TAG_LINK = "link";
    private static final String TAG_ENTRY = "entry";
    private static final String TAG_PUBDATE = "pubDate";
    private static final String TAG_UPDATED = "updated";
    private static final String TAG_DATE = "date";
    private static final String TAG_SOURCE = "source";
    private static final String TAG_IMAGE = "image";
    private static final String TAG_CONTENT = "content";

    private class ParserState {
        public Feed feed;
        public Item currentItem;
        public boolean isRSS;
        public boolean isATOM;
        public boolean inSource;
        public boolean inImage;
        public boolean inContent;
    }

    public Feed parse(InputStream in) throws ParserException, IOException {
        final ParserState state = new ParserState();

        try {
            final XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);

            XmlPullParser parser = factory.newPullParser();
            parser.setInput(in, null);

            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        handleStartDocument(state);
                        break;

                    case XmlPullParser.START_TAG:
                        handleStartTag(parser, state);
                        break;

                    case XmlPullParser.END_TAG:
                        handleEndTag(parser, state);
                        break;
                }

                eventType = parser.next();
            }
        } catch (XmlPullParserException e) {
            throw new ParserException(e);
        }

        if (!state.feed.isSufficientlyComplete()) {
            throw new ParserException("Feed is not sufficiently complete");
        }

        return state.feed;
    }

    private void handleStartDocument(ParserState state) {
        state.feed = new Feed();
    }

    private void handleStartTag(XmlPullParser parser, ParserState state) throws IOException, XmlPullParserException {
        switch (parser.getName()) {
            case TAG_RSS:
                state.isRSS = true;
                break;

            case TAG_FEED:
                state.isATOM = true;
                break;

            case TAG_RDF:
                // This is a RSS 1.0 feed
                state.isRSS = true;
                break;

            case TAG_ITEM:
            case TAG_ENTRY:
                state.currentItem = new Item();
                break;

            case TAG_TITLE:
                handleTitleStartTag(parser, state);
                break;

            case TAG_LINK:
                handleLinkStartTag(parser, state);
                break;

            case TAG_PUBDATE:
                handlePubDateStartTag(parser, state);
                break;

            case TAG_UPDATED:
                handleUpdatedStartTag(parser, state);
                break;

            case TAG_DATE:
                handleDateStartTag(parser, state);
                break;

            case TAG_SOURCE:
                state.inSource = true;
                break;

            case TAG_IMAGE:
                state.inImage = true;
                break;

            case TAG_CONTENT:
                state.inContent = true;
                break;
        }
    }

    private void handleEndTag(XmlPullParser parser, ParserState state) {
        switch (parser.getName()) {
            case TAG_ITEM:
            case TAG_ENTRY:
                handleItemOrEntryREndTag(state);
                break;

            case TAG_SOURCE:
                state.inSource = false;
                break;

            case TAG_IMAGE:
                state.inImage = false;
                break;

            case TAG_CONTENT:
                state.inContent = false;
                break;
        }
    }

    private void handleTitleStartTag(XmlPullParser parser, ParserState state) throws IOException, XmlPullParserException {
        if (state.inSource || state.inImage || state.inContent) {
            // We do not care about titles in <source>, <image> or <media> tags.
            return;
        }

        String title = getTextUntilEndTag(parser, TAG_TITLE);

        title = title.replaceAll("[\r\n]", " ");
        title = title.replaceAll("  +", " ");

        if (state.currentItem != null) {
            state.currentItem.setTitle(title);
        } else {
            state.feed.setTitle(title);
        }
    }

    private void handleLinkStartTag(XmlPullParser parser, ParserState state) throws IOException, XmlPullParserException {
        if (state.inSource || state.inImage) {
            // We do not care about links in <source> or <image> tags.
            return;
        }

        Map<String, String> attributes = fetchAttributes(parser);

        if (attributes.size() > 0) {
            String rel = attributes.get("rel");

            if (state.currentItem == null && "self".equals(rel)) {
                state.feed.setFeedURL(attributes.get("href"));
                return;
            }

            if (rel == null || "alternate".equals(rel)) {
                String type = attributes.get("type");
                if (type == null || type.equals("text/html")) {
                    String link = attributes.get("href");
                    if (TextUtils.isEmpty(link)) {
                        return;
                    }

                    if (state.currentItem != null) {
                        state.currentItem.setURL(link);
                    } else {
                        state.feed.setWebsiteURL(link);
                    }

                    return;
                }
            }
        }

        if (state.isRSS) {
            String link = getTextUntilEndTag(parser, TAG_LINK);
            if (TextUtils.isEmpty(link)) {
                return;
            }

            if (state.currentItem != null) {
                state.currentItem.setURL(link);
            } else {
                state.feed.setWebsiteURL(link);
            }
        }
    }

    private void handleItemOrEntryREndTag(ParserState state) {
        if (state.feed.getLastItem() == null || state.feed.getLastItem().getTimestamp() < state.currentItem.getTimestamp()) {
            // Only set this item as "last item" if we do not have an item yet or this item is newer.
            state.feed.setLastItem(state.currentItem);
        }

        state.currentItem = null;
    }

    private void handlePubDateStartTag(XmlPullParser parser, ParserState state) throws IOException, XmlPullParserException {
        if (state.currentItem == null) {
            return;
        }

        String pubDate = getTextUntilEndTag(parser, TAG_PUBDATE);
        if (TextUtils.isEmpty(pubDate)) {
            return;
        }

        // RFC-822
        SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);

        updateCurrentItemTimestamp(state, pubDate, format);
    }

    private void handleUpdatedStartTag(XmlPullParser parser, ParserState state) throws IOException, XmlPullParserException {
        if (state.inSource) {
            // We do not care about stuff in <source> tags.
            return;
        }

        if (state.currentItem == null) {
            // We are only interested in <updated> values of feed items.
            return;
        }

        String updated = getTextUntilEndTag(parser, TAG_UPDATED);
        if (TextUtils.isEmpty(updated)) {
            return;
        }

        SimpleDateFormat[] formats = new SimpleDateFormat[] {
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US)
        };

        // Fix timezones SimpleDateFormat can't parse:
        // 2016-01-26T18:56:54Z -> 2016-01-26T18:56:54+0000 (Timezone: Z -> +0000)
        updated = updated.replaceFirst("Z$", "+0000");
        // 2016-01-26T18:56:54+01:00 -> 2016-01-26T18:56:54+0100 (Timezone: +01:00 -> +0100)
        updated = updated.replaceFirst("([0-9]{2})([\\+\\-])([0-9]{2}):([0-9]{2})$", "$1$2$3$4");

        updateCurrentItemTimestamp(state, updated, formats);
    }

    private void handleDateStartTag(XmlPullParser parser, ParserState state) throws IOException, XmlPullParserException {
        if (state.currentItem == null) {
            // We are only interested in <updated> values of feed items.
            return;
        }

        String text = getTextUntilEndTag(parser, TAG_DATE);
        if (TextUtils.isEmpty(text)) {
            return;
        }

        // Fix timezones SimpleDateFormat can't parse:
        // 2016-01-26T18:56:54+00:00 -> 2016-01-26T18:56:54+0000
        text = text.replaceFirst("([0-9]{2})([\\+\\-])([0-9]{2}):([0-9]{2})$", "$1$2$3$4");

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);

        updateCurrentItemTimestamp(state, text, format);
    }

    private void updateCurrentItemTimestamp(ParserState state, String text, SimpleDateFormat... formats) {
        for (SimpleDateFormat format : formats) {
            try {
                Date date = format.parse(text);
                state.currentItem.setTimestamp(date.getTime());
                return;
            } catch (ParseException e) {
                Log.w(LOGTAG, "Could not parse 'updated': " + text);
            }
        }
    }

    private Map<String, String> fetchAttributes(XmlPullParser parser) {
        Map<String, String> attributes = new HashMap<>();

        for (int i = 0; i < parser.getAttributeCount(); i++) {
            attributes.put(parser.getAttributeName(i), parser.getAttributeValue(i));
        }

        return attributes;
    }

    private String getTextUntilEndTag(XmlPullParser parser, String tag) throws IOException, XmlPullParserException {
        StringBuilder builder = new StringBuilder();

        while (parser.next() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() == XmlPullParser.TEXT) {
                builder.append(parser.getText());
            } else if (parser.getEventType() == XmlPullParser.END_TAG && tag.equals(parser.getName())) {
                break;
            }
        }

        return builder.toString().trim();
    }
}