From de0b4cd507b9abb1ecf10bf46f4e2f7505705074 Mon Sep 17 00:00:00 2001 From: Wito Chandra Date: Sun, 16 Aug 2020 05:27:19 +0700 Subject: [PATCH] support RDF --- lib/domain/atom_feed.dart | 2 +- lib/domain/rss_feed.dart | 12 +++++++++--- test/rss_test.dart | 16 ++++++++++++++++ test/xml/RSS-RDF.xml | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 test/xml/RSS-RDF.xml diff --git a/lib/domain/atom_feed.dart b/lib/domain/atom_feed.dart index 2e4e855..128ed30 100644 --- a/lib/domain/atom_feed.dart +++ b/lib/domain/atom_feed.dart @@ -41,7 +41,7 @@ class AtomFeed { factory AtomFeed.parse(String xmlString) { var document = XmlDocument.parse(xmlString); - var feedElement = findFirstElement(document, 'feed', recursive: true); + var feedElement = findFirstElement(document, 'feed'); if (feedElement == null) { throw ArgumentError('feed not found'); } diff --git a/lib/domain/rss_feed.dart b/lib/domain/rss_feed.dart index 7d84d00..4aced3e 100644 --- a/lib/domain/rss_feed.dart +++ b/lib/domain/rss_feed.dart @@ -59,7 +59,12 @@ class RssFeed { factory RssFeed.parse(String xmlString) { var document = XmlDocument.parse(xmlString); - var channelElement = findFirstElement(document, 'channel', recursive: true); + var rss = findFirstElement(document, 'rss'); + var rdf = findFirstElement(document, 'rdf:RDF'); + if (rss == null && rdf == null) { + throw ArgumentError('not a rss feed'); + } + var channelElement = findFirstElement(rss ?? rdf, 'channel'); if (channelElement == null) { throw ArgumentError('channel not found'); } @@ -68,11 +73,12 @@ class RssFeed { author: findFirstElement(channelElement, 'author')?.text, description: findFirstElement(channelElement, 'description')?.text, link: findFirstElement(channelElement, 'link')?.text, - items: channelElement + items: (rss != null ? channelElement : rdf) .findElements('item') .map((e) => RssItem.parse(e)) .toList(), - image: RssImage.parse(findFirstElement(channelElement, 'image')), + image: RssImage.parse( + findFirstElement(rss != null ? channelElement : rdf, 'image')), cloud: RssCloud.parse(findFirstElement(channelElement, 'cloud')), categories: channelElement .findElements('category') diff --git a/test/rss_test.dart b/test/rss_test.dart index 36f8244..9dc738d 100644 --- a/test/rss_test.dart +++ b/test/rss_test.dart @@ -369,4 +369,20 @@ void main() { expect(item.itunes.title, 'awesome title'); expect(item.itunes.block, false); }); + + test('parse RSS-RDF.xml', () { + var xmlString = File('test/xml/RSS-RDF.xml').readAsStringSync(); + + var feed = RssFeed.parse(xmlString); + + expect(feed.title, 'Mozilla Dot Org'); + expect(feed.link, 'http://www.mozilla.org'); + expect(feed.description, 'the Mozilla Organization web site'); + expect(feed.image.title, 'Mozilla'); + expect(feed.image.url, 'http://www.mozilla.org/images/moz.gif'); + expect(feed.image.link, 'http://www.mozilla.org'); + expect(feed.items.length, 5); + expect(feed.items.first.title, 'New Status Updates'); + expect(feed.items.first.link, 'http://www.mozilla.org/status/'); + }); } diff --git a/test/xml/RSS-RDF.xml b/test/xml/RSS-RDF.xml new file mode 100644 index 0000000..63a5d03 --- /dev/null +++ b/test/xml/RSS-RDF.xml @@ -0,0 +1,35 @@ + + + + Mozilla Dot Org + http://www.mozilla.org + the Mozilla Organization web site + + + Mozilla + http://www.mozilla.org/images/moz.gif + http://www.mozilla.org + + + New Status Updates + http://www.mozilla.org/status/ + + + Bugzilla Reorganized + http://www.mozilla.org/bugs/ + + + Mozilla Party, 2.0! + http://www.mozilla.org/party/1999/ + + + Unix Platform Parity + http://www.mozilla.org/build/unix.html + + + NPL 1.0M published + http://www.mozilla.org/NPL/NPL-1.0M.html + + \ No newline at end of file