Support XML namespace prefixes when parsing appcasts

Fixes #771
This commit is contained in:
Kornel Lesiński committed 2016-03-01 00:37:52 +00:00
1 parent 64d95bb36f
commit 48f18d000c
3 files changed
+34 -24

No files matched your search

+28 -20
View File
@@ -19,23 +19,6 @@
@property (readonly, copy) NSDictionary *attributesAsDictionary;
@end
@implementation NSXMLElement (SUAppcastExtensions)
- (NSDictionary *)attributesAsDictionary
{
NSEnumerator *attributeEnum = [[self attributes] objectEnumerator];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSXMLNode *attribute in attributeEnum) {
NSString *attrName = [attribute name];
if (!attrName) {
continue;
}
dictionary[attrName] = [attribute stringValue];
}
return dictionary;
}
@end
@interface SUAppcast () <NSURLDownloadDelegate>
@property (strong) void (^completionBlock)(NSError *);
@property (copy) NSString *downloadFilename;
@@ -115,6 +98,31 @@
}
}
- (NSDictionary *)attributesOfNode:(NSXMLElement *)node
{
NSEnumerator *attributeEnum = [[node attributes] objectEnumerator];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSXMLNode *attribute in attributeEnum) {
NSString *attrName = [self sparkleNamespacedNameOfNode:attribute];
if (!attrName) {
continue;
}
dictionary[attrName] = [attribute stringValue];
}
return dictionary;
}
-(NSString *)sparkleNamespacedNameOfNode:(NSXMLNode *)node {
// XML namespace prefix is semantically meaningless, so compare namespace URI
if ([[node URI] isEqualToString:@"http://www.andymatuschak.org/xml-namespaces/sparkle"]) {
NSString *localName = [node localName]; assert(localName);
return [@"sparkle:" stringByAppendingString:localName];
} else {
return [node name]; // Backwards compatibility
}
}
-(NSArray *)parseAppcastItemsFromXMLFile:(NSURL *)appcastFile error:(NSError *__autoreleasing*)errorp {
if (errorp) {
*errorp = nil;
@@ -147,7 +155,7 @@
if ([[node children] count]) {
node = [node childAtIndex:0];
while (nil != node) {
NSString *name = [node name];
NSString *name = [self sparkleNamespacedNameOfNode:node];
if (name) {
NSMutableArray *nodes = nodesDict[name];
if (nodes == nil) {
@@ -164,7 +172,7 @@
node = [self bestNodeInNodes:nodesDict[name]];
if ([name isEqualToString:SURSSElementEnclosure]) {
// enclosure is flattened as a separate dictionary for some reason
NSDictionary *encDict = [(NSXMLElement *)node attributesAsDictionary];
NSDictionary *encDict = [self attributesOfNode:(NSXMLElement *)node];
dict[name] = encDict;
}
else if ([name isEqualToString:SURSSElementPubDate]) {
@@ -182,7 +190,7 @@
NSEnumerator *childEnum = [[node children] objectEnumerator];
for (NSXMLNode *child in childEnum) {
if ([[child name] isEqualToString:SURSSElementEnclosure]) {
[deltas addObject:[(NSXMLElement *)child attributesAsDictionary]];
[deltas addObject:[self attributesOfNode:(NSXMLElement *)child]];
}
}
dict[name] = deltas;
+4 -3
View File
@@ -1,17 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle">
<rss version="2.0" xmlns:customNS="http://www.andymatuschak.org/xml-namespaces/sparkle">
<channel>
<title>For unit test only</title>
<item>
<title>Version 2.0</title>
<description>desc</description>
<pubDate>Sat, 26 Jul 2014 15:20:11 +0000</pubDate>
<enclosure url="http://0.0.0.0:1337/Sparkle_Test_App.zip" sparkle:version="2.0" />
<enclosure url="http://0.0.0.0:1337/Sparkle_Test_App.zip" customNS:version="2.0" />
</item>
<item>
<title>Version 3.0</title>
<pubDate>Sat, 26 Jul 2014 15:20:12 +0000</pubDate>
<enclosure url="http://0.0.0.0:1337/Sparkle_Test_App.zip" sparkle:version="3.0" />
<customNS:releaseNotesLink>https://sparkle-project.org/#works</customNS:releaseNotesLink>
<enclosure url="http://0.0.0.0:1337/Sparkle_Test_App.zip" customNS:version="3.0" />
</item>
</channel>
</rss>
+2 -1
View File
@@ -11,7 +11,7 @@ import Sparkle;
class SUAppcastTest: XCTestCase {
func testExample() {
func testParseAppcast() {
let appcast = SUAppcast();
let testFile = NSBundle(forClass: SUAppcastTest.self).pathForResource("testappcast", ofType: "xml")!;
let testFileUrl = NSURL(fileURLWithPath: testFile);
@@ -26,6 +26,7 @@ class SUAppcastTest: XCTestCase {
XCTAssertEqual("Version 2.0", items[1].title);
XCTAssertEqual("desc", items[1].itemDescription);
XCTAssertEqual("https://sparkle-project.org/#works", items[0].releaseNotesURL!.absoluteString);
} catch let err as NSError {
NSLog("%@", err);
XCTFail(err.localizedDescription);