-
Notifications
You must be signed in to change notification settings - Fork 35
/
SMXMLDocument.m
215 lines (163 loc) · 7.01 KB
/
SMXMLDocument.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#import "SMXMLDocument.h"
NSString *const SMXMLDocumentErrorDomain = @"SMXMLDocumentErrorDomain";
static NSError *SMXMLDocumentError(NSXMLParser *parser, NSError *parseError) {
NSString *description = [NSString stringWithFormat:NSLocalizedString(@"Malformed XML document. Error at line %@:%@.", @""),
@(parser.lineNumber), @(parser.columnNumber)];
NSDictionary *userInfo = @{
NSUnderlyingErrorKey: parseError,
NSLocalizedDescriptionKey: description,
@"LineNumber": @(parser.lineNumber),
@"ColumnNumber": @(parser.columnNumber)
};
return [NSError errorWithDomain:SMXMLDocumentErrorDomain code:1 userInfo:userInfo];
}
@implementation SMXMLElement
- (id)initWithDocument:(SMXMLDocument *)document {
if (self = [super init])
self.document = document;
return self;
}
- (NSString *)descriptionWithIndent:(NSString *)indent truncatedValues:(BOOL)truncated encoded:(BOOL)encode {
NSMutableString *s = [NSMutableString string];
[s appendFormat:@"%@<%@", indent, self.name];
for (NSString *attribute in self.attributes) {
NSString *attributeValue = self.attributes[attribute];
[s appendFormat:@" %@=\"%@\"", attribute, encode ? [self encodeString:attributeValue] : attributeValue];
}
NSString *valueOrTrimmed = [self.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (truncated && valueOrTrimmed.length > 25)
valueOrTrimmed = [NSString stringWithFormat:@"%@…", [valueOrTrimmed substringToIndex:25]];
if (encode)
valueOrTrimmed = [self encodeString:valueOrTrimmed];
if (self.children.count) {
[s appendString:@">\n"];
NSString *childIndent = [indent stringByAppendingString:@" "];
if (valueOrTrimmed.length)
[s appendFormat:@"%@%@\n", childIndent, valueOrTrimmed];
for (SMXMLElement *child in self.children)
[s appendFormat:@"%@\n", [child descriptionWithIndent:childIndent truncatedValues:truncated encoded:encode]];
[s appendFormat:@"%@</%@>", indent, self.name];
}
else if (valueOrTrimmed.length) {
[s appendFormat:@">%@</%@>", valueOrTrimmed, self.name];
}
else [s appendString:@"/>"];
return s;
}
- (NSString *)encodeString:(NSString *)string
{
if (!string.length)
return string;
NSMutableString *encoded = [NSMutableString stringWithString:string];
[encoded replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
[encoded replaceOccurrencesOfString:@"\"" withString:@""" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
[encoded replaceOccurrencesOfString:@"'" withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
[encoded replaceOccurrencesOfString:@">" withString:@">" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
[encoded replaceOccurrencesOfString:@"<" withString:@"<" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
return encoded;
}
- (NSString *)description {
return [self descriptionWithIndent:@"" truncatedValues:YES encoded:NO];
}
- (NSString *)fullDescription {
return [self descriptionWithIndent:@"" truncatedValues:NO encoded:NO];
}
- (NSString *)encodedDescription {
return [self descriptionWithIndent:@"" truncatedValues:NO encoded:YES];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!string) return;
if (self.value)
[(NSMutableString *)self.value appendString:string];
else
self.value = [NSMutableString stringWithString:string];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
SMXMLElement *child = [[SMXMLElement alloc] initWithDocument:self.document];
child.parent = self;
child.name = elementName;
child.attributes = attributeDict;
if (self.children)
[(NSMutableArray *)self.children addObject:child];
else
self.children = [NSMutableArray arrayWithObject:child];
[parser setDelegate:child];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
[parser setDelegate:self.parent];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
self.document.error = SMXMLDocumentError(parser, parseError);
}
- (SMXMLElement *)childNamed:(NSString *)nodeName {
for (SMXMLElement *child in self.children)
if ([child.name isEqual:nodeName])
return child;
return nil;
}
- (NSArray *)childrenNamed:(NSString *)nodeName {
NSMutableArray *array = [NSMutableArray array];
for (SMXMLElement *child in self.children)
if ([child.name isEqual:nodeName])
[array addObject:child];
return array.count ? [array copy] : nil;
}
- (SMXMLElement *)childWithAttribute:(NSString *)attributeName value:(NSString *)attributeValue {
for (SMXMLElement *child in self.children)
if ([[child attributeNamed:attributeName] isEqual:attributeValue])
return child;
return nil;
}
- (NSString *)attributeNamed:(NSString *)attributeName {
return self.attributes[attributeName];
}
- (SMXMLElement *)descendantWithPath:(NSString *)path {
SMXMLElement *descendant = self;
for (NSString *childName in [path componentsSeparatedByString:@"."])
descendant = [descendant childNamed:childName];
return descendant;
}
- (NSString *)valueWithPath:(NSString *)path {
NSArray *components = [path componentsSeparatedByString:@"@"];
SMXMLElement *descendant = [self descendantWithPath:components[0]];
return [components count] > 1 ? [descendant attributeNamed:components[1]] : descendant.value;
}
- (SMXMLElement *)firstChild { return [self.children count] > 0 ? self.children[0] : nil; }
- (SMXMLElement *)lastChild { return [self.children lastObject]; }
@end
@interface SMXMLDocument ()
@property (nonatomic, assign) BOOL parsedRoot;
@end
@implementation SMXMLDocument
- (id)initWithData:(NSData *)data error:(NSError **)outError {
if (self = [super initWithDocument:self]) {
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
parser.shouldProcessNamespaces = YES;
parser.shouldReportNamespacePrefixes = YES;
parser.shouldResolveExternalEntities = NO;
[parser parse];
if (self.error) {
if (outError)
*outError = self.error;
return nil;
}
else if (outError)
*outError = nil;
}
return self;
}
+ (SMXMLDocument *)documentWithData:(NSData *)data error:(NSError **)outError {
return [[SMXMLDocument alloc] initWithData:data error:outError];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if (!self.parsedRoot) {
self.name = elementName;
self.attributes = attributeDict;
self.parsedRoot = YES;
}
else {
[super parser:parser didStartElement:elementName namespaceURI:namespaceURI qualifiedName:qName attributes:attributeDict];
}
}
@end