forked from greenisus/cocoa-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSSParser.m
115 lines (91 loc) · 5.14 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
//
// RSSParser.m
//
// Created by Mike Mayo on 1/28/10.
// Copyright Mike Mayo 2010. All rights reserved.
//
#import "RSSParser.h"
#import "FeedItem.h"
@implementation RSSParser
@synthesize feedItem, currentDataType, feedItems;
#pragma mark -
#pragma mark Date Parser
-(NSDate *)dateFromString:(NSString *)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
return date;
}
#pragma mark -
#pragma mark XML Parser Delegate Methods
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
//Extract the attribute here.
//aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];
// if (![elementName isEqualToString:@"uri"]) {
// // if it's not the uri, it's a data type
// currentDataType = [NSString stringWithString:elementName];
// }
if ([elementName isEqualToString:@"rss"]) {
// we're getting started, so go ahead and alloc the array
self.feedItems = [[NSMutableArray alloc] initWithCapacity:1];
} else if ([elementName isEqualToString:@"item"]) {
self.feedItem = [[FeedItem alloc] init];
parsingItem = YES;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// <pubDate>Fri, 15 Jan 2010 16:16:59 -0600</pubDate>
if ([elementName isEqualToString:@"item"]) {
[self.feedItems addObject:self.feedItem];
parsingItem = NO;
} else if ([elementName isEqualToString:@"title"]) {
feedItem.title = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
} else if ([elementName isEqualToString:@"link"]) {
feedItem.link = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
} else if ([elementName isEqualToString:@"guid"]) {
feedItem.guid = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
} else if ([elementName isEqualToString:@"description"]) {
feedItem.description = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
} else if ([elementName isEqualToString:@"content:encoded"]) {
feedItem.content = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
feedItem.content = [feedItem.content stringByReplacingOccurrencesOfString:@"\n" withString:@""];
feedItem.content = [feedItem.content stringByReplacingOccurrencesOfString:@"\r" withString:@""];
// <p>Huddle 16 is currently experiencing network related issue, you may see issues connecting to your server and accessing the Cloud Servers section of the control panel.&#0160; The control panel may be loading slowly or timing out completely. Our technicians are working to
// quickly resolve the issue. We will post another update once PHP 5 sites
// are back to normal speeds.</p><p></p><p>Update: Connectivity is returning, and all issues should begin resolving.</p>
feedItem.content = [feedItem.content stringByReplacingOccurrencesOfString:@"</p><p>" withString:@"\n\n"];
feedItem.content = [feedItem.content stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
feedItem.content = [feedItem.content stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
feedItem.content = [feedItem.content stringByReplacingOccurrencesOfString:@"</p><p>" withString:@"\n\n"];
feedItem.content = [feedItem.content stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
feedItem.content = [feedItem.content stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
feedItem.content = [feedItem.content stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
if ([feedItem.content characterAtIndex:0] == ' ' && [feedItem.content length] > 0) {
feedItem.content = [feedItem.content substringFromIndex:1];
}
} else if ([elementName isEqualToString:@"dc:creator"]) {
feedItem.creator = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
} else if ([elementName isEqualToString:@"pubDate"]) {
feedItem.pubDate = [self dateFromString:[currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
[currentElementValue release];
currentElementValue = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentElementValue) {
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else {
[currentElementValue appendString:string];
}
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
[feedItem release];
[currentDataType release];
[feedItems release];
[super dealloc];
}
@end