-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOverheadRssManager.m
133 lines (96 loc) · 4.13 KB
/
OverheadRssManager.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
//
// OverheadRssManager.m
// OverHead
//
// Created by Don Garrett on 11/17/09.
// Copyright 2009 Don Garrett. All rights reserved.
//
#import "OverheadRssManager.h"
@implementation OverheadRssManager
@synthesize delegate;
- (id)init {
[super init];
// Create an empty result set for now
satellites = [[NSMutableArray alloc] init];
return self;
}
- (void)startRetrieving:(CLLocation *)currentLocation {
NSString * url = [NSString stringWithFormat:@"http://orbitingfrog.com/overtwitter/rss.php?lat=%f&lng=%f",
currentLocation.coordinate.latitude,
currentLocation.coordinate.longitude];
// Local file for network disconnected development.
// NSString * url = @"file:///Users/dgarrett/tmp/test.rss";
//url = @"file:///Users/dgarrett/tmp/test.rss";
[self parseXMLFileAtURL:url];
}
- (void)parseXMLFileAtURL:(NSString *)URL {
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
// Setup reasonable parsing options
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
-(void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(@"found file and started parsing");
// Before parsing a new file, create a new (different) array to hold
// the results. Doing this insures that we don't confuse anyone reading
// the old values since we pass pointers to this array around.
satellites = [[NSMutableArray alloc] init];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
// Tell someone we errored out
if (delegate)
[delegate rssManager:self didFailWithError:parseError];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"item"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"date"];
[satellites addObject:[item copy]];
NSLog(@"adding story: %@", currentTitle);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"all done!");
NSLog(@"satellites array has %d items", [satellites count]);
// tell someone we finished
if (delegate)
[delegate rssManager:self didUpdate:satellites];
}
@end