-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSXMLParser+ReactiveCocoa.m
162 lines (134 loc) · 5.93 KB
/
NSXMLParser+ReactiveCocoa.m
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
//
// NSXMLParser+ReactiveCocoa.m
//
//
// The MIT License (MIT)
//
// Copyright (c) 2014 Alex Manarpies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "NSXMLParser+ReactiveCocoa.h"
#import "NSXMLParserRACDelegate.h"
#import "NSXMLParserRACElement.h"
#import <ReactiveCocoa.h>
@implementation NSXMLParser (ReactiveCocoa)
+ (RACSignal *)rac_parseURL:(NSURL *)url
{
return [self rac_parseWithParser:[[NSXMLParser alloc] initWithContentsOfURL:url]];
}
+ (RACSignal *)rac_parseData:(NSData *)data
{
return [self rac_parseWithParser:[[NSXMLParser alloc] initWithData:data]];
}
+ (RACSignal *)rac_dictionaryFromString:(NSString *)string elementFilter:(ElementFilterBlock)filterBlock
{
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
return [self rac_dictionaryFromData:data elementFilter:filterBlock];
}
+ (RACSignal *)rac_dictionaryFromURL:(NSURL *)url elementFilter:(ElementFilterBlock)filterBlock
{
return [self rac_dictionaryFromSignal:[self rac_parseURL:url] elementfilter:filterBlock];
}
+ (RACSignal *)rac_dictionaryFromData:(NSData *)data elementFilter:(ElementFilterBlock)filterBlock
{
return [self rac_dictionaryFromSignal:[self rac_parseData:data] elementfilter:filterBlock];
}
- (RACSignal *)rac_parser
{
return [NSXMLParser rac_parseWithParser:self];
}
- (RACSignal *)rac_parserWithElementFilter:(ElementFilterBlock)filterBlock
{
return [NSXMLParser rac_dictionaryFromSignal:[self rac_parser] elementfilter:filterBlock];
}
#pragma mark - Internal
/**
* Parse a XML using a pre-init'ed parser.
*/
+ (RACSignal *)rac_parseWithParser:(NSXMLParser *)parser
{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
return [[RACScheduler scheduler] schedule:^{
NSXMLParserRACDelegate *delegate = [[NSXMLParserRACDelegate alloc] init];
[delegate.elementParsed subscribe:subscriber];
parser.delegate = delegate;
[parser parse];
}];
}];
}
+ (RACSignal *)rac_dictionaryFromSignal:(RACSignal *)signal elementfilter:(ElementFilterBlock)filterBlock
{
// Parsing algorythm inspired by http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
return [[RACScheduler scheduler] schedule:^{
__block NSMutableArray *stack = [@[[@{} mutableCopy]] mutableCopy];
__block NSXMLParserRACElement *currentElement;
[[signal filter:^BOOL(NSXMLParserRACElement *element) {
// If provided delegate to filterBlock to determine which elements to ignore
return filterBlock ? filterBlock(element.name) : YES;
}] subscribeNext:^(NSXMLParserRACElement *element) {
if (element.phase == NSXMLParserRACElementPhaseOpen) {
NSMutableDictionary *parent = [stack lastObject];
NSMutableDictionary *child = [@{} mutableCopy];
if ([element.attributes count]) {
for (NSString *key in [element.attributes allKeys]) {
child[key] = element.attributes[key];
}
}
id existing = parent[element.name];
if (existing) {
NSMutableArray *array = nil;
if ([existing isKindOfClass:[NSMutableArray class]]) {
array = existing;
}
else {
array = [@[] mutableCopy];
[array addObject:existing];
parent[element.name] = array;
}
[array addObject:child];
}
else {
parent[element.name] = child;
}
[stack addObject:child];
}
if (element.phase == NSXMLParserRACElementPhaseData) {
currentElement = element;
}
if (element.phase == NSXMLParserRACElementPhaseClose) {
NSMutableDictionary *current = [stack lastObject];
if (currentElement.body) {
NSString *sanitized = [currentElement.body stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (sanitized && ![sanitized isEqualToString:@""]) {
current[@"text"] = sanitized;
}
}
[stack removeLastObject];
}
} error:^(NSError *error) {
[subscriber sendError:error];
} completed:^{
if ([stack count] > 0) [subscriber sendNext:stack[0]];
[subscriber sendCompleted];
}];
}];
}];
}
@end