-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXMLObject.m
More file actions
375 lines (309 loc) · 12.7 KB
/
XMLObject.m
File metadata and controls
375 lines (309 loc) · 12.7 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//
// XMLObject.m
//
// Created by Petr Syrov on 15/11/13.
// Copyright (c) 2013 Petr Syrov. All rights reserved.
//
#import "XMLObject.h"
#define ATTRIBUTES @"__attributes__"
#define CHARACTERS @"__characters__"
#define ELEMENTS @"__elements__"
@implementation NSObject (XMLObject)
- (id)stringByReplacingQuotes {
if ([self isKindOfClass:[NSString class]]) {
NSString *value = [NSString stringWithString:(NSString *)self];
value = [value stringByReplacingOccurrencesOfString:@"\"" withString:@"""];
value = [value stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
value = [value stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
value = [value stringByReplacingOccurrencesOfString:@">" withString:@">"];
value = [value stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
return value;
}
return self;
}
@end
@interface XMLObject ()
@property (strong) NSXMLParser *parser;
@property (strong, nonatomic) NSMutableArray *rootArray;
@property (strong, nonatomic) NSMutableArray *currentDictionary;
@property (strong, nonatomic) NSMutableArray *currentArray;
@end
@implementation XMLObject
- (id)init {
self = [super init];
if (self) {
}
return self;
}
- (id)initWithData:(NSData *)data {
self = [self init];
if (self) {
self.parser = [[NSXMLParser alloc] initWithData:data];
[self.parser setDelegate:self];
}
return self;
}
- (id)initWithDictionary:(NSDictionary *)dictionary {
self = [self init];
if (self) {
self.rootArray = [NSMutableArray arrayWithObject:dictionary];
}
return self;
}
- (id)initWithArray:(NSArray *)array {
self = [self init];
if (self) {
self.rootArray = [NSMutableArray arrayWithArray:array];
}
return self;
}
- (NSMutableDictionary *)xmlElementDictionaryForKey:(NSString *)key atIndex:(NSInteger)index {
for (NSInteger i = 0; i < self.rootArray.count; i++) {
NSMutableDictionary *dictionary = [self.rootArray objectAtIndex:i];
if ([dictionary objectForKey:key] && (index == NSIntegerMax)?YES:(i == index)) {
return [dictionary objectForKey:key];
}
}
return nil;
}
- (NSString *)description {
return self.rootArray.description;
}
+ (id)xmlObjectWithData:(NSData *)data error:(NSError **)error {
XMLObject *xmlObject = [[XMLObject alloc] initWithData:data];
[xmlObject.parser parse];
if (xmlObject.parser.parserError) {
if (error != NULL) *error = xmlObject.parser.parserError;
return nil;
}
return xmlObject;
}
+ (id)xmlObjectWithXMLObject:(XMLObject *)xmlObject {
return [XMLObject xmlObjectWithArray:xmlObject.rootArray];
}
+ (id)xmlObjectWithXMLObjects:(NSArray *)xmlObjects value:(NSString *)value attributes:(NSDictionary *)attributes forKey:(NSString *)key {
NSMutableArray *xmlObjectsArray = [NSMutableArray arrayWithCapacity:0];
for (XMLObject *xmlObject in xmlObjects) {
[xmlObjectsArray addObjectsFromArray:xmlObject.rootArray];
}
NSMutableDictionary *xmlElementDictionary = [NSMutableDictionary dictionaryWithDictionary:@{CHARACTERS: value?:@"", ELEMENTS: xmlObjectsArray?:@[], ATTRIBUTES: attributes?:@{}}];
return [XMLObject xmlObjectWithArray:[NSMutableArray arrayWithArray:@[[NSMutableDictionary dictionaryWithDictionary:@{key: xmlElementDictionary}]]]];
}
+ (id)xmlObjectWithValue:(NSString *)value attributes:(NSDictionary *)attributes forKey:(NSString *)key {
return [XMLObject xmlObjectWithXMLObjects:@[] value:value attributes:attributes forKey:key];
}
+ (id)xmlObjectWithAttributes:(NSDictionary *)attributes forKey:(NSString *)key {
return [XMLObject xmlObjectWithXMLObjects:@[] value:@"" attributes:attributes forKey:key];
}
+ (id)xmlObjectForKey:(NSString *)key {
return [XMLObject xmlObjectWithXMLObjects:@[] value:@"" attributes:@{} forKey:key];
}
+ (id)xmlObjectWithDictionary:(NSDictionary *)dictionary {
return [[[self class] alloc] initWithDictionary:dictionary];
}
+ (id)xmlObjectWithArray:(NSArray *)array {
return [[[self class] alloc] initWithArray:array];
}
- (NSArray *)allTags {
NSMutableDictionary *tags = [NSMutableDictionary dictionaryWithCapacity:0];
for (NSDictionary *dictionary in self.rootArray) {
[tags setObject:@"tag" forKey:dictionary.allKeys.firstObject];
}
return tags.allKeys;
}
- (NSString *)xml {
return [NSString stringWithFormat:@"<?xml version=\"1.0\"?>%@", self.string];
}
- (NSString *)string {
NSMutableString *string = [NSMutableString stringWithCapacity:0];
for (NSDictionary *xmlElement in self.rootArray) {
NSDictionary *xmlElementObject = [xmlElement objectForKey:xmlElement.allKeys.firstObject];
[string appendFormat:@"<%@", xmlElement.allKeys.firstObject];
NSDictionary *attributes = [xmlElementObject objectForKey:ATTRIBUTES];
if (attributes) {
for (NSString *attributeKey in [attributes allKeys]) {
[string appendFormat:@" %@=\"%@\"", attributeKey, [[attributes valueForKey:attributeKey] stringByReplacingQuotes]];
}
}
[string appendString:@">"];
NSString *value =[xmlElementObject objectForKey:CHARACTERS];
if (value) {
[string appendString:[value stringByReplacingQuotes]];
}
NSArray *elements = [xmlElementObject objectForKey:ELEMENTS];
if (elements) {
[string appendString:[[XMLObject xmlObjectWithArray:elements] string]];
}
[string appendFormat:@"</%@>", xmlElement.allKeys.firstObject];
}
return string;
}
- (XMLObject *)objectForKey:(NSString *)key {
return [self objectForKey:key atIndex:NSIntegerMax];
}
- (XMLObject *)objectForKeys:(NSArray *)keys {
if (keys.count > 1) {
NSMutableArray *mutableKeys = [NSMutableArray arrayWithArray:keys];
[mutableKeys removeObjectAtIndex:0];
XMLObject *xmlObject = [self objectForKey:keys.firstObject];
return [xmlObject objectForKeys:mutableKeys];
} else if (keys.count == 1) {
return [self objectForKey:keys.firstObject];
} else {
return nil;
}
}
- (XMLObject *)objectForKey:(NSString *)key atIndex:(NSInteger)index {
NSMutableDictionary *xmlElementDictionary = [self xmlElementDictionaryForKey:key atIndex:index];
if (xmlElementDictionary) {
return [XMLObject xmlObjectWithArray:[xmlElementDictionary objectForKey:ELEMENTS]];
}
return nil;
}
- (XMLObject *)objectAtIndex:(NSInteger)index {
return [self objectForKey:self.allTags.firstObject atIndex:index];
}
- (NSString *)valueForKey:(NSString *)key {
return [self valueForKey:key atIndex:NSIntegerMax];
}
- (NSString *)valueForKeys:(NSArray *)keys {
NSMutableArray *mutableKeys = [NSMutableArray arrayWithArray:keys];
[mutableKeys removeLastObject];
XMLObject *xmlObject = [self objectForKeys:mutableKeys];
if (xmlObject) {
return [xmlObject valueForKey:keys.lastObject];
} else {
return [self valueForKey:keys.lastObject];
}
}
- (NSString *)valueForKey:(NSString *)key atIndex:(NSInteger)index {
NSMutableDictionary *xmlElementDictionary = [self xmlElementDictionaryForKey:key atIndex:index];
if (xmlElementDictionary) {
return [xmlElementDictionary objectForKey:CHARACTERS];
}
return nil;
}
- (NSString *)valueAtIndex:(NSInteger)index {
return [self valueForKey:self.allTags.firstObject atIndex:index];
}
- (NSDictionary *)attributesForKey:(NSString *)key {
return [self attributesForKey:key atIndex:NSIntegerMax];
}
- (NSDictionary *)attributesForKeys:(NSArray *)keys {
NSMutableArray *mutableKeys = [NSMutableArray arrayWithArray:keys];
[mutableKeys removeLastObject];
XMLObject *xmlObject = [self objectForKeys:mutableKeys];
if (xmlObject) {
return [xmlObject attributesForKey:keys.lastObject];
} else {
return [self attributesForKey:keys.lastObject];
}
}
- (NSDictionary *)attributesForKey:(NSString *)key atIndex:(NSInteger)index {
NSMutableDictionary *xmlElementDictionary = [self xmlElementDictionaryForKey:key atIndex:index];
if (xmlElementDictionary) {
return [xmlElementDictionary objectForKey:ATTRIBUTES];
}
return nil;
}
- (NSDictionary *)attributesAtIndex:(NSInteger)index {
return [self attributesForKey:self.allTags.firstObject atIndex:index];
}
- (NSInteger)countForKey:(NSString *)key {
NSInteger count = 0;
for (NSMutableDictionary *dictionary in self.rootArray) {
if ([dictionary objectForKey:key]) {
count++;
}
}
return count;
}
- (void)setAttributes:(NSDictionary *)attributes forKey:(NSString *)key atIndex:(NSInteger)index {
NSMutableDictionary *xmlElementDictionary = [self xmlElementDictionaryForKey:key atIndex:index];
if (xmlElementDictionary) {
[xmlElementDictionary setObject:attributes forKey:ATTRIBUTES];
}
}
- (void)setAttributes:(NSDictionary *)attributes forKey:(NSString *)key {
[self setAttributes:attributes forKey:key atIndex:NSIntegerMax];
}
- (void)setValue:(NSString *)value forKey:(NSString *)key atIndex:(NSInteger)index {
NSMutableDictionary *xmlElementDictionary = [self xmlElementDictionaryForKey:key atIndex:index];
if (xmlElementDictionary) {
[xmlElementDictionary setObject:value forKey:CHARACTERS];
}
}
- (void)setValue:(NSString *)value forKey:(NSString *)key {
[self setValue:value forKey:key atIndex:NSIntegerMax];
}
- (void)setObject:(XMLObject *)xmlObject forKey:(NSString *)key atIndex:(NSInteger)index {
NSMutableDictionary *xmlElementDictionary = [self xmlElementDictionaryForKey:key atIndex:index];
if (xmlElementDictionary) {
[xmlElementDictionary setObject:xmlObject.rootArray forKey:ELEMENTS];
}
}
- (void)setObject:(XMLObject *)xmlObject forKey:(NSString *)key {
[self setObject:xmlObject forKey:key atIndex:NSIntegerMax];
}
- (void)addObject:(XMLObject *)xmlObject forKey:(NSString *)key atIndex:(NSInteger)index {
NSMutableDictionary *xmlElementDictionary = [self xmlElementDictionaryForKey:key atIndex:index];
if (xmlElementDictionary) {
[xmlElementDictionary setObject:[self.rootArray arrayByAddingObjectsFromArray:xmlObject.rootArray] forKey:ELEMENTS];
}
}
- (void)addObject:(XMLObject *)xmlObject forKey:(NSString *)key {
[self addObject:xmlObject forKey:key atIndex:NSIntegerMax];
}
- (void)removeObjectForKey:(NSString *)key atIndex:(NSInteger)index {
for (NSInteger i = 0; i < self.rootArray.count; i++) {
NSMutableDictionary *dictionary = [self.rootArray objectAtIndex:i];
if ([dictionary objectForKey:key] && (index == NSIntegerMax)?YES:(i == index)) {
[self.rootArray removeObject:dictionary];
break;
}
}
}
- (void)removeObjectForKey:(NSString *)key {
[self removeObjectForKey:key atIndex:NSIntegerMax];
}
#pragma mark - NSXMLParserDelegate
- (void)parserDidStartDocument:(NSXMLParser *)parser {
self.rootArray = [NSMutableArray arrayWithCapacity:0];
self.currentArray = [NSMutableArray arrayWithCapacity:0];
[self.currentArray addObject:self.rootArray];
self.currentDictionary = [NSMutableArray arrayWithCapacity:0];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
NSMutableDictionary *element = [NSMutableDictionary dictionaryWithCapacity:0];
[element setObject:attributeDict forKey:ATTRIBUTES];
NSMutableArray *subElements = [NSMutableArray arrayWithCapacity:0];
[element setObject:subElements forKey:ELEMENTS];
[self.currentDictionary addObject:element];
[self.currentArray.lastObject addObject:@{elementName: element}];
[self.currentArray addObject:subElements];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
/*
NSString *trim = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trim.length) {
*/
NSMutableString *currentString = [self.currentDictionary.lastObject valueForKey:CHARACTERS];
if (!currentString) {
currentString = [NSMutableString stringWithString:@""];
}
[currentString appendString:string];
// [currentString appendString:trim];
[self.currentDictionary.lastObject setObject:currentString forKey:CHARACTERS];
/*
}
*/
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
[self.currentDictionary removeLastObject];
[self.currentArray removeLastObject];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
}
@end