forked from dennisreimann/ioctocat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GHSearch.m
65 lines (54 loc) · 2.11 KB
/
GHSearch.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
#import "GHSearch.h"
@implementation GHSearch
@synthesize results;
@synthesize searchTerm;
+ (id)searchWithURLFormat:(NSString *)theFormat andParserDelegateClass:(Class)theDelegateClass {
return [[[[self class] alloc] initWithURLFormat:theFormat andParserDelegateClass:theDelegateClass] autorelease];
}
- (id)initWithURLFormat:(NSString *)theFormat andParserDelegateClass:(Class)theDelegateClass {
[super init];
urlFormat = [theFormat retain];
parserDelegate = [(GHResourcesParserDelegate *)[theDelegateClass alloc] initWithTarget:self andSelector:@selector(parsingFinished:)];
return self;
}
- (void)dealloc {
[parserDelegate release], parserDelegate = nil;
[searchTerm release], searchTerm = nil;
[urlFormat release], urlFormat = nil;
[results release], results = nil;
[super dealloc];
}
- (NSString *)description {
return [NSString stringWithFormat:@"<GHSearch searchTerm:'%@' resourceURL:'%@'>", searchTerm, self.resourceURL];
}
- (NSURL *)resourceURL {
// Dynamic resourceURL, because it depends on the
// searchTerm which isn't always available in advance
NSString *encodedSearchTerm = [searchTerm stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:urlFormat, encodedSearchTerm];
NSURL *url = [NSURL URLWithString:urlString];
return url;
}
- (void)parseData:(NSData *)data {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:parserDelegate];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
[pool release];
}
- (void)parsingFinished:(id)theResult {
if ([theResult isKindOfClass:[NSError class]]) {
self.error = theResult;
self.loadingStatus = GHResourceStatusNotLoaded;
} else {
// Mark the results as not loaded, because the search doesn't contain all attributes
for (GHResource *res in theResult) res.loadingStatus = GHResourceStatusNotLoaded;
self.results = theResult;
self.loadingStatus = GHResourceStatusLoaded;
}
}
@end