Ver código fonte

support RDF

master
Wito Chandra 6 anos atrás
pai
commit
de0b4cd507
4 arquivos alterados com 61 adições e 4 exclusões
  1. +1
    -1
      lib/domain/atom_feed.dart
  2. +9
    -3
      lib/domain/rss_feed.dart
  3. +16
    -0
      test/rss_test.dart
  4. +35
    -0
      test/xml/RSS-RDF.xml

+ 1
- 1
lib/domain/atom_feed.dart Ver arquivo

@@ -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');
}


+ 9
- 3
lib/domain/rss_feed.dart Ver arquivo

@@ -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')


+ 16
- 0
test/rss_test.dart Ver arquivo

@@ -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/');
});
}

+ 35
- 0
test/xml/RSS-RDF.xml Ver arquivo

@@ -0,0 +1,35 @@
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://channel.netscape.com/rdf/simple/0.9/">
<channel>
<title>Mozilla Dot Org</title>
<link>http://www.mozilla.org</link>
<description>the Mozilla Organization web site</description>
</channel>
<image>
<title>Mozilla</title>
<url>http://www.mozilla.org/images/moz.gif</url>
<link>http://www.mozilla.org</link>
</image>
<item>
<title>New Status Updates</title>
<link>http://www.mozilla.org/status/</link>
</item>
<item>
<title>Bugzilla Reorganized</title>
<link>http://www.mozilla.org/bugs/</link>
</item>
<item>
<title>Mozilla Party, 2.0!</title>
<link>http://www.mozilla.org/party/1999/</link>
</item>
<item>
<title>Unix Platform Parity</title>
<link>http://www.mozilla.org/build/unix.html</link>
</item>
<item>
<title>NPL 1.0M published</title>
<link>http://www.mozilla.org/NPL/NPL-1.0M.html</link>
</item>
</rdf:RDF>

Carregando…
Cancelar
Salvar