-
Notifications
You must be signed in to change notification settings - Fork 164
/
MGTwitterMessagesLibXMLParser.m
114 lines (102 loc) · 3.2 KB
/
MGTwitterMessagesLibXMLParser.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
//
// MGTwitterMessagesLibXMLParser.m
// MGTwitterEngine
//
// Created by Matt Gemmell on 11/02/2008.
// Copyright 2008 Instinctive Code.
//
#import "MGTwitterMessagesLibXMLParser.h"
@implementation MGTwitterMessagesLibXMLParser
- (NSDictionary *)_directMessageDictionaryForNodeWithName:(const xmlChar *)parentNodeName {
if (xmlTextReaderIsEmptyElement(_reader))
return nil;
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
int readerResult = xmlTextReaderRead(_reader);
if (readerResult != 1)
return nil;
int nodeType = xmlTextReaderNodeType(_reader);
const xmlChar *name = xmlTextReaderConstName(_reader);
while (! (nodeType == XML_READER_TYPE_END_ELEMENT && xmlStrEqual(parentNodeName, name)))
{
if (nodeType == XML_READER_TYPE_ELEMENT)
{
if (xmlStrEqual(name, BAD_CAST "sender") || xmlStrEqual(name, BAD_CAST "recipient"))
{
// "user" is the name of a sub-dictionary in each <status> item
[dictionary setObject:[self _userDictionaryForNodeWithName:name] forKey:[NSString stringWithUTF8String:(const char *)name]];
}
else if (xmlStrEqual(name, BAD_CAST "id") || xmlStrEqual(name, BAD_CAST "sender_id") || xmlStrEqual(name, BAD_CAST "recipient_id"))
{
// process element as an integer
NSNumber *number = [self _nodeValueAsInt];
if (number)
{
[dictionary setObject:number forKey:[NSString stringWithUTF8String:(const char *)name]];
}
}
else if (xmlStrEqual(name, BAD_CAST "created_at"))
{
// process element as a date
NSDate *date = [self _nodeValueAsDate];
if (date)
{
[dictionary setObject:date forKey:[NSString stringWithUTF8String:(const char *)name]];
}
}
else if (xmlStrEqual(name, BAD_CAST "protected"))
{
// process element as a boolean
NSNumber *number = [self _nodeValueAsBool];
if (number)
{
[dictionary setObject:number forKey:[NSString stringWithUTF8String:(const char *)name]];
}
}
else
{
// process element as a string
NSString *string = [self _nodeValueAsString];
if (string)
{
[dictionary setObject:string forKey:[NSString stringWithUTF8String:(const char *)name]];
}
}
}
// advance reader
readerResult = xmlTextReaderRead(_reader);
if (readerResult != 1)
break;
nodeType = xmlTextReaderNodeType(_reader);
name = xmlTextReaderConstName(_reader);
}
// save the request type in the tweet
[dictionary setObject:[NSNumber numberWithInt:requestType] forKey:TWITTER_SOURCE_REQUEST_TYPE];
return dictionary;
}
- (void)parse
{
int readerResult = xmlTextReaderRead(_reader);
if (readerResult != 1)
return;
int nodeType = xmlTextReaderNodeType(_reader);
const xmlChar *name = xmlTextReaderConstName(_reader);
while (! (nodeType == XML_READER_TYPE_END_ELEMENT && xmlStrEqual(BAD_CAST "direct-messages", name)))
{
if (nodeType == XML_READER_TYPE_ELEMENT)
{
if (xmlStrEqual(name, BAD_CAST "direct_message"))
{
[parsedObjects addObject:[self _directMessageDictionaryForNodeWithName:BAD_CAST "direct_message"]];
}
}
// advance reader
readerResult = xmlTextReaderRead(_reader);
if (readerResult != 1)
{
break;
}
nodeType = xmlTextReaderNodeType(_reader);
name = xmlTextReaderConstName(_reader);
}
}
@end