-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADVElement.m
133 lines (118 loc) · 2.69 KB
/
ADVElement.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
//
// Copyright © 2011 Yuri Kotov
// Licensed under the MIT license: http://opensource.org/licenses/MIT
// Latest version can be found at http://github.com/advantis/ADVElement
//
#import "ADVElement.h"
#if __has_feature(objc_arc_weak)
#define __desirable_weak __weak
#else
#define __desirable_weak __unsafe_unretained
#endif
@interface ADVElement ()
@property (nonatomic, strong) NSMutableDictionary *subelements;
@end
@implementation ADVElement
{
NSInteger _level;
__desirable_weak id _parent;
NSMutableString *_body;
}
@synthesize startHandler = _startHandler;
@synthesize endHandler = _endHandler;
@synthesize subelements = _subelements;
#pragma mark - Element
- (id) initWithParent:(id<NSXMLParserDelegate>)parent
{
self = [super init];
if (self)
{
_parent = parent;
}
return self;
}
- (NSMutableDictionary *) subelements
{
if (!_subelements)
{
_subelements = [[NSMutableDictionary alloc] initWithCapacity:1];
}
return _subelements;
}
- (ADVElement *) childElementWithName:(NSString *)elementName
{
NSParameterAssert(elementName);
ADVElement *node = [[ADVElement alloc] initWithParent:self];
[self.subelements setObject:node forKey:elementName];
#if !__has_feature(objc_arc)
[node release];
#endif
return node;
}
#pragma mark - NSObject
#if !__has_feature(objc_arc)
- (void) dealloc
{
[_body release];
[_subelements release];
[_startHandler release];
[_endHandler release];
[super dealloc];
}
#endif
#pragma mark - NSXMLParserDelegate
- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
ADVElement *node = [_subelements objectForKey:elementName];
if (node)
{
if (node.startHandler)
{
node.startHandler(attributeDict);
}
parser.delegate = node;
}
else
{
++_level;
}
}
- (void) parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
if (0 == _level && _endHandler)
{
if (_body)
{
[_body appendString:string];
}
else
{
_body = [string mutableCopy];
}
}
}
- (void) parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if (--_level < 0)
{
_level = 0;
if (_endHandler)
{
_endHandler(_body);
}
#if !__has_feature(objc_arc)
[_body release];
#endif
_body = nil;
parser.delegate = _parent;
}
}
@end