-
Notifications
You must be signed in to change notification settings - Fork 164
/
MGTwitterXMLParser.m
176 lines (131 loc) · 5.05 KB
/
MGTwitterXMLParser.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
//
// MGTwitterXMLParser.m
// MGTwitterEngine
//
// Created by Matt Gemmell on 18/02/2008.
// Copyright 2008 Instinctive Code.
//
#import "MGTwitterXMLParser.h"
@implementation MGTwitterXMLParser
#pragma mark Creation and Destruction
+ (id)parserWithXML:(NSData *)theXML delegate:(NSObject *)theDelegate
connectionIdentifier:(NSString *)identifier requestType:(MGTwitterRequestType)reqType
responseType:(MGTwitterResponseType)respType
{
id parser = [[self alloc] initWithXML:theXML
delegate:theDelegate
connectionIdentifier:identifier
requestType:reqType
responseType:respType];
return [parser autorelease];
}
- (id)initWithXML:(NSData *)theXML delegate:(NSObject *)theDelegate
connectionIdentifier:(NSString *)theIdentifier requestType:(MGTwitterRequestType)reqType
responseType:(MGTwitterResponseType)respType
{
if ((self = [super init])) {
xml = [theXML retain];
identifier = [theIdentifier retain];
requestType = reqType;
responseType = respType;
delegate = theDelegate;
parsedObjects = [[NSMutableArray alloc] initWithCapacity:0];
// Set up the parser object.
parser = [[NSXMLParser alloc] initWithData:xml];
[parser setDelegate:self];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldProcessNamespaces:NO];
[parser setShouldResolveExternalEntities:NO];
// Begin parsing.
[parser parse];
}
return self;
}
- (void)dealloc
{
[parser release];
[parsedObjects release];
[xml release];
[identifier release];
delegate = nil;
[super dealloc];
}
#pragma mark NSXMLParser delegate methods
- (void)parserDidStartDocument:(NSXMLParser *)theParser
{
//NSLog(@"Parsing begun");
}
- (void)parserDidEndDocument:(NSXMLParser *)theParser
{
//NSLog(@"Parsing complete: %@", parsedObjects);
[delegate parsingSucceededForRequest:identifier ofResponseType:responseType
withParsedObjects:parsedObjects];
}
- (void)parser:(NSXMLParser *)theParser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
//NSLog(@"Started element: %@ (%@)", elementName, attributeDict);
}
- (void)parser:(NSXMLParser *)theParser foundCharacters:(NSString *)characters
{
//NSLog(@"Found characters: %@", characters);
}
- (void)parser:(NSXMLParser *)theParser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
//NSLog(@"Ended element: %@", elementName);
[self setLastOpenedElement:nil];
if ([elementName isEqualToString:@"protected"]
|| [elementName isEqualToString:@"truncated"]
|| [elementName isEqualToString:@"following"]) {
// Change "true"/"false" into an NSNumber with a BOOL value.
NSNumber *boolNumber = [NSNumber numberWithBool:[[currentNode objectForKey:elementName] isEqualToString:@"true"]];
[currentNode setObject:boolNumber forKey:elementName];
} else if ([elementName isEqualToString:@"created_at"]) {
// Change date-string into an NSDate.
// NSLog(@"%@", [currentNode objectForKey:elementName]);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
dateFormatter.dateFormat = @"EEE MMM dd HH:mm:ss +0000 yyyy";
NSDate *creationDate = [dateFormatter dateFromString:[currentNode objectForKey:elementName]];
[dateFormatter release];
if (creationDate) {
[currentNode setObject:creationDate forKey:elementName];
}
}
}
- (void)parser:(NSXMLParser *)theParser foundAttributeDeclarationWithName:(NSString *)attributeName
forElement:(NSString *)elementName type:(NSString *)type defaultValue:(NSString *)defaultValue
{
//NSLog(@"Found attribute: %@ (%@) [%@] {%@}", attributeName, elementName, type, defaultValue);
}
- (void)parser:(NSXMLParser *)theParser foundIgnorableWhitespace:(NSString *)whitespaceString
{
//NSLog(@"Found ignorable whitespace: %@", whitespaceString);
}
- (void)parser:(NSXMLParser *)theParser parseErrorOccurred:(NSError *)parseError
{
//NSLog(@"Parsing error occurred: %@", parseError);
[delegate parsingFailedForRequest:identifier ofResponseType:responseType
withError:parseError];
}
#pragma mark Accessors
- (NSString *)lastOpenedElement {
return [[lastOpenedElement retain] autorelease];
}
- (void)setLastOpenedElement:(NSString *)value {
if (lastOpenedElement != value) {
[lastOpenedElement release];
lastOpenedElement = [value copy];
}
}
#pragma mark Utility methods
- (void)addSource
{
if (![currentNode objectForKey:TWITTER_SOURCE_REQUEST_TYPE]) {
[currentNode setObject:[NSNumber numberWithInt:requestType]
forKey:TWITTER_SOURCE_REQUEST_TYPE];
}
}
@end