-
Notifications
You must be signed in to change notification settings - Fork 30
/
RSSParser.m
executable file
·233 lines (212 loc) · 8.62 KB
/
RSSParser.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
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
//
// RSSParser.m
// RSSKit
//
// Created by Árpád Goretity on 01/11/2011.
// Licensed under a CreativeCommons Attribution 3.0 Unported License
//
#import "RSSParser.h"
#import "NSMutableString+RSSKit.h"
@implementation RSSParser
@synthesize delegate;
@synthesize url;
@synthesize synchronous;
- (id) initWithUrl:(NSString *)theUrl synchronous:(BOOL)sync {
self = [super init];
self.url = theUrl;
self.synchronous = sync;
if (self.synchronous) {
NSURL *contentUrl = [[NSURL alloc] initWithString:self.url];
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:contentUrl];
[contentUrl release];
[xmlParser setDelegate:self];
}
return self;
}
- (id) initWithUrl:(NSString *)theUrl {
self = [self initWithUrl:theUrl synchronous:NO];
return self;
}
- (id) init {
self = [self initWithUrl:NULL];
return self;
}
- (void) dealloc {
[xmlParser setDelegate:NULL];
[xmlParser release];
self.url = NULL;
[super dealloc];
}
// self
- (void) parse {
if (!self.synchronous) {
NSURL *contentUrl = [[NSURL alloc] initWithString:self.url];
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:contentUrl];
[contentUrl release];
[xmlParser setDelegate:self];
}
[xmlParser parse];
}
// NSXMLParserDelegate
- (void) parserDidStartDocument:(NSXMLParser *)parser {
feed = [[RSSFeed alloc] init];
tagStack = [[NSMutableArray alloc] init];
tagPath = [[NSMutableString alloc] initWithString:@"/"];
}
- (void) parserDidEndDocument:(NSXMLParser *)parser {
[tagStack release];
[tagPath release];
if ([delegate respondsToSelector:@selector(rssParser:parsedFeed:)]) {
[delegate rssParser:self parsedFeed:feed];
}
[feed release];
}
- (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)error {
[tagStack release];
[tagPath release];
[feed release];
if ([delegate respondsToSelector:@selector(rssParser:errorOccurred:)]) {
[delegate rssParser:self errorOccurred:error];
}
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)element namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributes {
// decide type of the feed based on its root element
if ([element isEqualToString:@"rss"]) {
feed.type = RSSFeedTypeRSS;
} else if ([element isEqualToString:@"feed"]) {
feed.type = RSSFeedTypeAtom;
} else if ([element isEqualToString:@"item"] || [element isEqualToString:@"entry"]) {
// or, if it's an article summary tag, create an article object
entry = [[RSSEntry alloc] init];
}
// prepare to successively receive characters
// then push element onto stack
NSMutableDictionary *context = [[NSMutableDictionary alloc] init];
[context setObject:attributes forKey:@"attributes"];
NSMutableString *text = [[NSMutableString alloc] init];
[context setObject:text forKey:@"text"];
[text release];
[tagStack addObject:context];
[context release];
[tagPath appendPathComponent:element];
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)element namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName {
NSMutableDictionary *context = [tagStack lastObject];
NSMutableString *text = [context objectForKey:@"text"];
NSDictionary *attributes = [context objectForKey:@"attributes"];
if ([tagPath isEqualToString:@"/rss/channel/title"] || [tagPath isEqualToString:@"/feed/title"]) {
feed.title = text;
} else if ([tagPath isEqualToString:@"/rss/channel/description"] || [tagPath isEqualToString:@"/feed/subtitle"]) {
feed.description = text;
} else if ([tagPath isEqualToString:@"/feed/id"]) {
feed.uid = text;
} else if ([tagPath isEqualToString:@"/rss/channel/link"] || [tagPath isEqualToString:@"/feed/link"]) {
// RSS 2.0 or Atom 1.0?
NSString *href = [attributes objectForKey:@"href"];
if (href) {
// Atom 1.0
feed.url = href;
} else {
// RSS 2.0
feed.url = text;
}
} else if ([tagPath isEqualToString:@"/rss/channel/language"]) {
feed.language = text;
} else if ([tagPath isEqualToString:@"/rss/channel/copyright"] || [tagPath isEqualToString:@"/feed/rights"]) {
feed.copyright = text;
} else if ([tagPath isEqualToString:@"/rss/channel/category"] || [tagPath isEqualToString:@"/feed/category"]) {
// RSS 2.0 or Atom 1.0?
NSString *term = [attributes objectForKey:@"term"];
if (term) {
// Atom 1.0
[feed.categories addObject:term];
} else {
// RSS 2.0
[feed.categories addObject:text];
}
} else if ([tagPath isEqualToString:@"/rss/channel/generator"] || [tagPath isEqualToString:@"/feed/generator"]) {
feed.generator = text;
} else if ([tagPath isEqualToString:@"/rss/channel/ttl"]) {
feed.validTime = [text floatValue];
} else if ([tagPath isEqualToString:@"/rss/channel/image/url"] || [tagPath isEqualToString:@"/feed/icon"]) {
feed.iconUrl = text;
} else if ([tagPath isEqualToString:@"/rss/channel/cloud"]) {
RSSCloudService *cloudService = [[RSSCloudService alloc] init];
cloudService.domain = [attributes objectForKey:@"domain"];
cloudService.port = [[attributes objectForKey:@"port"] intValue];
cloudService.path = [attributes objectForKey:@"path"];
cloudService.procedure = [attributes objectForKey:@"registerProcedure"];
cloudService.protocol = [attributes objectForKey:@"protocol"];
feed.cloudService = cloudService;
[cloudService release];
} else if ([tagPath isEqualToString:@"/rss/channel/lastBuildDate"] || [tagPath isEqualToString:@"/rss/channel/dc:date"] || [tagPath isEqualToString:@"/feed/updated"]) {
feed.date = text;
} else if ([tagPath isEqualToString:@"/rss/channel/managingEditor"]) {
feed.author = text;
} else if ([tagPath isEqualToString:@"/feed/author/name"]) {
feed.author = feed.author ? [NSString stringWithFormat:@"%@ (%@)", text, feed.author] : text;
} else if ([tagPath isEqualToString:@"/feed/author/email"]) {
feed.author = feed.author ? [NSString stringWithFormat:@"%@ (%@)", feed.author, text] : text;
} else if ([tagPath isEqualToString:@"/rss/channel/item/title"] || [tagPath isEqualToString:@"/feed/entry/title"]) {
entry.title = text;
} else if ([tagPath isEqualToString:@"/rss/channel/item/link"] || [tagPath isEqualToString:@"/feed/entry/link"]) {
// RSS 2.0 or Atom 1.0?
NSString *href = [attributes objectForKey:@"href"];
if (href) {
// Atom 1.0
entry.url = href;
} else {
// RSS 2.0
entry.url = text;
}
} else if ([tagPath isEqualToString:@"/rss/channel/item/description"] || [tagPath isEqualToString:@"/feed/entry/summary"]) {
entry.summary = text;
} else if ([tagPath isEqualToString:@"/rss/channel/item/category"] || [tagPath isEqualToString:@"/feed/entry/category"]) {
// RSS 2.0 or Atom 1.0?
NSString *term = [attributes objectForKey:@"term"];
if (term) {
// Atom 1.0
[entry.categories addObject:term];
} else {
// RSS 2.0
[entry.categories addObject:text];
}
} else if ([tagPath isEqualToString:@"/rss/channel/item/comments"] || [tagPath isEqualToString:@""]) {
entry.comments = text;
} else if ([tagPath isEqualToString:@"/rss/channel/item/author"] || [tagPath isEqualToString:@"/feed/entry/author/name"]) {
entry.author = text;
} else if ([tagPath isEqualToString:@"/feed/entry/content"] || [tagPath isEqualToString:@"/rss/channel/item/content:encoded"]) {
entry.content = text;
} else if ([tagPath isEqualToString:@"/rss/channel/item/enclosure"]) {
RSSAttachedMedia *media = [[RSSAttachedMedia alloc] init];
media.url = [attributes objectForKey:@"url"];
media.length = [[attributes objectForKey:@"length"] intValue];
media.type = [attributes objectForKey:@"type"];
entry.attachedMedia = media;
[media release];
} else if ([tagPath isEqualToString:@"/rss/channel/item/guid"] || [tagPath isEqualToString:@"/feed/entry/id"]) {
entry.uid = text;
} else if ([tagPath isEqualToString:@"/rss/channel/item/pubDate"] || [tagPath isEqualToString:@"/rss/channel/item/dc:date"] || [tagPath isEqualToString:@"/feed/entry/updated"]) {
entry.date = text;
} else if ([tagPath isEqualToString:@"/feed/entry/rights"]) {
entry.copyright = text;
} else if ([tagPath isEqualToString:@"/rss/channel/item"] || [tagPath isEqualToString:@"/feed/entry"]) {
[feed.articles addObject:entry];
[entry release];
}
[tagStack removeLastObject];
[tagPath deleteLastPathComponent];
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSMutableDictionary *context = [tagStack lastObject];
NSMutableString *text = [context objectForKey:@"text"];
[text appendString:string];
}
- (void) parser:(NSXMLParser *)parser foundCDATA:(NSData *)data {
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableDictionary *context = [tagStack lastObject];
NSMutableString *text = [context objectForKey:@"text"];
[text appendString:string];
[string release];
}
@end