OrderedDictionary.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. //
  2. // OrderedDictionary.m
  3. //
  4. // Version 1.4
  5. //
  6. // Created by Nick Lockwood on 21/09/2010.
  7. // Copyright 2010 Charcoal Design
  8. //
  9. // Distributed under the permissive zlib license
  10. // Get the latest version from here:
  11. //
  12. // https://github.com/nicklockwood/OrderedDictionary
  13. //
  14. // This software is provided 'as-is', without any express or implied
  15. // warranty. In no event will the authors be held liable for any damages
  16. // arising from the use of this software.
  17. //
  18. // Permission is granted to anyone to use this software for any purpose,
  19. // including commercial applications, and to alter it and redistribute it
  20. // freely, subject to the following restrictions:
  21. //
  22. // 1. The origin of this software must not be misrepresented; you must not
  23. // claim that you wrote the original software. If you use this software
  24. // in a product, an acknowledgment in the product documentation would be
  25. // appreciated but is not required.
  26. //
  27. // 2. Altered source versions must be plainly marked as such, and must not be
  28. // misrepresented as being the original software.
  29. //
  30. // 3. This notice may not be removed or altered from any source distribution.
  31. //
  32. #import "OrderedDictionary.h"
  33. #pragma GCC diagnostic ignored "-Wobjc-missing-property-synthesis"
  34. #pragma GCC diagnostic ignored "-Wnullable-to-nonnull-conversion"
  35. #pragma GCC diagnostic ignored "-Wdirect-ivar-access"
  36. #pragma GCC diagnostic ignored "-Wfloat-equal"
  37. #pragma GCC diagnostic ignored "-Wgnu"
  38. #import <Availability.h>
  39. #if !__has_feature(objc_arc)
  40. #error This class requires automatic reference counting
  41. #endif
  42. @implementation NSThread (XMLPlist)
  43. - (NSDateFormatter *)XMLPlistDateFormatter
  44. {
  45. static NSString *const key = @"XMLPlistDateFormatter";
  46. NSDateFormatter *formatter = self.threadDictionary[key];
  47. if (!formatter)
  48. {
  49. formatter = [[NSDateFormatter alloc] init];
  50. formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
  51. formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  52. formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
  53. self.threadDictionary[key] = formatter;
  54. }
  55. return formatter;
  56. }
  57. @end
  58. @interface NSObject (XMLPlistWriting)
  59. - (NSString *)XMLPlistStringWithIndent:(NSString *)indent;
  60. @end
  61. @interface OrderedDictionaryXMLPlistParser : NSObject<NSXMLParserDelegate>
  62. @property (nonatomic, readonly) OrderedDictionary *root;
  63. - (instancetype)initWithData:(NSData *)data root:(OrderedDictionary *)root;
  64. @end
  65. @implementation OrderedDictionary
  66. {
  67. @protected
  68. NSArray *_values;
  69. NSOrderedSet *_keys;
  70. }
  71. + (instancetype)dictionaryWithContentsOfFile:(NSString *)path
  72. {
  73. NSData *data = [NSData dataWithContentsOfFile:path];
  74. __autoreleasing id dictionary = [[self alloc] initWithPlistData:data];
  75. return dictionary;
  76. }
  77. + (instancetype)dictionaryWithContentsOfURL:(NSURL *)url
  78. {
  79. NSData *data = [NSData dataWithContentsOfURL:url];
  80. __autoreleasing id dictionary = [[self alloc] initWithPlistData:data];
  81. return dictionary;
  82. }
  83. - (instancetype)initWithContentsOfFile:(NSString *)path
  84. {
  85. NSData *data = [NSData dataWithContentsOfFile:path];
  86. return [self initWithPlistData:data];
  87. return nil;
  88. }
  89. - (instancetype)initWithContentsOfURL:(NSURL *)url
  90. {
  91. NSData *data = [NSData dataWithContentsOfURL:url];
  92. return [self initWithPlistData:data];
  93. return nil;
  94. }
  95. - (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
  96. {
  97. if ((self = [super init]))
  98. {
  99. _values = [objects copy];
  100. _keys = [NSOrderedSet orderedSetWithArray:keys];
  101. NSParameterAssert([_keys count] == [_values count]);
  102. }
  103. return self;
  104. }
  105. - (instancetype)initWithPlistData:(NSData *)data
  106. {
  107. if (data)
  108. {
  109. const void *bytes = data.bytes;
  110. char header[7];
  111. memcpy(header, &bytes, 6);
  112. header[6] = '\0';
  113. NSAssert(strcmp(header, "bplist") != 0, @"OrderedDictionary does not support loading binary plist files. Use an XML plist file instead. Xcode automatically converts XML plist files to binary files in built apps - see documentation for tips on how to disable this.");
  114. return [[OrderedDictionaryXMLPlistParser alloc] initWithData:data root:[self init]].root;
  115. }
  116. return nil;
  117. }
  118. - (instancetype)initWithObjects:(const __unsafe_unretained id [])objects forKeys:(const __unsafe_unretained id <NSCopying> [])keys count:(NSUInteger)count
  119. {
  120. if ((self = [super init]))
  121. {
  122. _values = [[NSArray alloc] initWithObjects:objects count:count];
  123. _keys = [[NSOrderedSet alloc] initWithObjects:keys count:count];
  124. NSParameterAssert([_values count] == count);
  125. NSParameterAssert([_keys count] == count);
  126. }
  127. return self;
  128. }
  129. - (Class)classForCoder
  130. {
  131. return [self class];
  132. }
  133. - (instancetype)init
  134. {
  135. if ((self = [super init]) && [self class] == [OrderedDictionary class])
  136. {
  137. static OrderedDictionary *singleton;
  138. static dispatch_once_t onceToken;
  139. dispatch_once(&onceToken, ^{
  140. singleton = self;
  141. self->_values = @[];
  142. self->_keys = [[NSOrderedSet alloc] init];
  143. });
  144. return singleton;
  145. }
  146. return self;
  147. }
  148. - (instancetype)initWithCoder:(NSCoder *)decoder
  149. {
  150. if ((self = [super init]))
  151. {
  152. _values = [decoder decodeObjectOfClass:[NSArray class] forKey:@"values"];
  153. _keys = [decoder decodeObjectOfClass:[NSOrderedSet class] forKey:@"keys"];
  154. }
  155. return self;
  156. }
  157. - (void)encodeWithCoder:(NSCoder *)coder
  158. {
  159. [coder encodeObject:_values forKey:@"values"];
  160. [coder encodeObject:_keys forKey:@"keys"];
  161. }
  162. + (BOOL)supportsSecureCoding
  163. {
  164. return YES;
  165. }
  166. - (instancetype)copyWithZone:(__unused NSZone *)zone
  167. {
  168. return self;
  169. }
  170. - (instancetype)mutableCopyWithZone:(NSZone *)zone
  171. {
  172. return [[MutableOrderedDictionary allocWithZone:zone] initWithDictionary:self];
  173. }
  174. - (NSArray *)allKeys
  175. {
  176. return _keys.array;
  177. }
  178. - (NSArray *)allValues
  179. {
  180. return [_values copy];
  181. }
  182. - (NSUInteger)count
  183. {
  184. return _keys.count;
  185. }
  186. - (NSUInteger)indexOfKey:(id)key
  187. {
  188. return [_keys indexOfObject:key];
  189. }
  190. - (id)objectForKey:(id)key
  191. {
  192. NSUInteger index = [_keys indexOfObject:key];
  193. if (index != NSNotFound)
  194. {
  195. return _values[index];
  196. }
  197. return nil;
  198. }
  199. - (NSEnumerator *)keyEnumerator
  200. {
  201. return [_keys objectEnumerator];
  202. }
  203. - (NSEnumerator *)reverseKeyEnumerator
  204. {
  205. return [_keys reverseObjectEnumerator];
  206. }
  207. - (NSEnumerator *)objectEnumerator
  208. {
  209. return [_values objectEnumerator];
  210. }
  211. - (NSEnumerator *)reverseObjectEnumerator
  212. {
  213. return [_values reverseObjectEnumerator];
  214. }
  215. - (void)enumerateKeysAndObjectsWithIndexUsingBlock:(void (^)(id key, id obj, NSUInteger idx, BOOL *stop))block
  216. {
  217. [_keys enumerateObjectsUsingBlock:^(id key, NSUInteger idx, BOOL *stop) {
  218. block(key, self->_values[idx], idx, stop);
  219. }];
  220. }
  221. - (id)keyAtIndex:(NSUInteger)index
  222. {
  223. return _keys[index];
  224. }
  225. - (id)objectAtIndex:(NSUInteger)index
  226. {
  227. return _values[index];
  228. }
  229. - (id)objectAtIndexedSubscript:(NSUInteger)index
  230. {
  231. return _values[index];
  232. }
  233. - (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level
  234. {
  235. NSMutableString *indent = [NSMutableString string];
  236. for (int i = 0; i < level; i++)
  237. {
  238. [indent appendString:@" "];
  239. }
  240. NSMutableString *string = [NSMutableString string];
  241. [string appendString:indent];
  242. [string appendString:@"{\n"];
  243. [self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, __unused BOOL *stop) {
  244. NSString *description;
  245. if ([obj respondsToSelector:@selector(descriptionWithLocale:indent:)]) {
  246. description = [obj descriptionWithLocale:locale indent:level + 1];
  247. } else if ([obj respondsToSelector:@selector(descriptionWithLocale:)]) {
  248. description = [obj descriptionWithLocale:locale];
  249. } else {
  250. description = [obj description];
  251. }
  252. [string appendString:indent];
  253. [string appendFormat:@" %@ = %@;\n", key, description];
  254. }];
  255. [string appendString:indent];
  256. [string appendString:@"}"];
  257. return string;
  258. }
  259. - (NSString *)XMLPlistString
  260. {
  261. return [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  262. "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
  263. "<plist version=\"1.0\">\n%@\n</plist>\n",
  264. [self XMLPlistStringWithIndent:@""]];
  265. }
  266. - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile
  267. {
  268. return [[self XMLPlistString] writeToFile:path atomically:useAuxiliaryFile encoding:NSUTF8StringEncoding error:NULL];
  269. }
  270. - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically
  271. {
  272. return [[self XMLPlistString] writeToURL:url atomically:atomically encoding:NSUTF8StringEncoding error:NULL];
  273. }
  274. @end
  275. @implementation MutableOrderedDictionary
  276. #define _mutableValues ((NSMutableArray *)_values)
  277. #define _mutableKeys ((NSMutableOrderedSet *)_keys)
  278. + (instancetype)dictionaryWithCapacity:(NSUInteger)count
  279. {
  280. return [(MutableOrderedDictionary *)[self alloc] initWithCapacity:count];
  281. }
  282. - (instancetype)initWithObjects:(const __unsafe_unretained id [])objects forKeys:(const __unsafe_unretained id <NSCopying> [])keys count:(NSUInteger)count
  283. {
  284. if ((self = [super init]))
  285. {
  286. _values = [[NSMutableArray alloc] initWithObjects:objects count:count];
  287. _keys = [[NSMutableOrderedSet alloc] initWithObjects:keys count:count];
  288. }
  289. return self;
  290. }
  291. - (instancetype)initWithCapacity:(NSUInteger)capacity
  292. {
  293. if ((self = [super init]))
  294. {
  295. _values = [[NSMutableArray alloc] initWithCapacity:capacity];
  296. _keys = [[NSMutableOrderedSet alloc] initWithCapacity:capacity];
  297. }
  298. return self;
  299. }
  300. - (instancetype)init
  301. {
  302. return [self initWithCapacity:0];
  303. }
  304. - (instancetype)initWithCoder:(NSCoder *)decoder
  305. {
  306. if ((self = [super init]))
  307. {
  308. _values = [decoder decodeObjectOfClass:[NSMutableArray class] forKey:@"values"];
  309. _keys = [decoder decodeObjectOfClass:[NSMutableOrderedSet class] forKey:@"keys"];
  310. }
  311. return self;
  312. }
  313. - (id)copyWithZone:(NSZone *)zone
  314. {
  315. return [[OrderedDictionary allocWithZone:zone] initWithDictionary:self];
  316. }
  317. - (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary
  318. {
  319. [otherDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, __unused BOOL *stop) {
  320. [self setObject:obj forKey:key];
  321. }];
  322. }
  323. - (void)insertObject:(id)object forKey:(id)key atIndex:(NSUInteger)index
  324. {
  325. [self removeObjectForKey:key];
  326. [_mutableKeys insertObject:key atIndex:index];
  327. [_mutableValues insertObject:object atIndex:index];
  328. }
  329. - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)object
  330. {
  331. _mutableValues[index] = object;
  332. }
  333. - (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index
  334. {
  335. _mutableValues[index] = object;
  336. }
  337. - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2
  338. {
  339. [_mutableKeys exchangeObjectAtIndex:idx1 withObjectAtIndex:idx2];
  340. [_mutableValues exchangeObjectAtIndex:idx1 withObjectAtIndex:idx2];
  341. }
  342. - (void)removeAllObjects
  343. {
  344. [_mutableKeys removeAllObjects];
  345. [_mutableValues removeAllObjects];
  346. }
  347. - (void)removeObjectAtIndex:(NSUInteger)index
  348. {
  349. [_mutableKeys removeObjectAtIndex:index];
  350. [_mutableValues removeObjectAtIndex:index];
  351. }
  352. - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes
  353. {
  354. [_mutableKeys removeObjectsAtIndexes:indexes];
  355. [_mutableValues removeObjectsAtIndexes:indexes];
  356. }
  357. - (void)removeObjectForKey:(id)key
  358. {
  359. NSUInteger index = [self->_keys indexOfObject:key];
  360. if (index != NSNotFound)
  361. {
  362. [self removeObjectAtIndex:index];
  363. }
  364. }
  365. - (void)removeObjectsForKeys:(NSArray *)keyArray
  366. {
  367. for (id key in [keyArray copy])
  368. {
  369. [self removeObjectForKey:key];
  370. }
  371. }
  372. - (void)setDictionary:(NSDictionary *)otherDictionary
  373. {
  374. [_mutableKeys removeAllObjects];
  375. [_mutableKeys addObjectsFromArray:otherDictionary.allKeys];
  376. [_mutableValues setArray:otherDictionary.allValues];
  377. }
  378. - (void)setObject:(id)object forKey:(id)key
  379. {
  380. NSUInteger index = [_keys indexOfObject:key];
  381. if (index != NSNotFound)
  382. {
  383. _mutableValues[index] = object;
  384. }
  385. else
  386. {
  387. [_mutableKeys addObject:key];
  388. [_mutableValues addObject:object];
  389. }
  390. }
  391. - (void)setValue:(id)value forKey:(NSString *)key
  392. {
  393. if (value)
  394. {
  395. [self setObject:value forKey:key];
  396. }
  397. else
  398. {
  399. [self removeObjectForKey:key];
  400. }
  401. }
  402. - (void)setObject:(id)object forKeyedSubscript:(id)key
  403. {
  404. [self setObject:object forKey:key];
  405. }
  406. @end
  407. @implementation NSObject (XMLPlistWriting)
  408. - (NSString *)XMLPlistStringWithIndent:(__unused NSString *)indent
  409. {
  410. NSLog(@"%@ is not a supported property list type.", self.classForCoder);
  411. [self doesNotRecognizeSelector:_cmd];
  412. return nil;
  413. }
  414. @end
  415. @implementation NSString (XMLPlistWriting)
  416. - (NSString *)XMLEscapedString
  417. {
  418. return [[[self stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"]
  419. stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"]
  420. stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
  421. }
  422. - (NSString *)XMLPlistStringWithIndent:(__unused NSString *)indent
  423. {
  424. return [NSString stringWithFormat:@"<string>%@</string>", [self XMLEscapedString]];
  425. }
  426. @end
  427. @implementation NSNumber (XMLPlistWriting)
  428. - (NSString *)XMLPlistStringWithIndent:(__unused NSString *)indent
  429. {
  430. if ((__bridge CFBooleanRef)self == kCFBooleanTrue)
  431. {
  432. return @"<true/>";
  433. }
  434. else if ((__bridge CFBooleanRef)self == kCFBooleanFalse)
  435. {
  436. return @"<false/>";
  437. }
  438. else if (self.doubleValue != (double)self.integerValue)
  439. {
  440. return [NSString stringWithFormat:@"<real>%@</real>", self];
  441. }
  442. else
  443. {
  444. return [NSString stringWithFormat:@"<integer>%@</integer>", self];
  445. }
  446. }
  447. @end
  448. @implementation NSDate (XMLPlistWriting)
  449. - (NSString *)XMLPlistStringWithIndent:(__unused NSString *)indent
  450. {
  451. NSDateFormatter *formatter = [[NSThread currentThread] XMLPlistDateFormatter];
  452. return [NSString stringWithFormat:@"<date>%@</date>", [formatter stringFromDate:self]];
  453. }
  454. @end
  455. @implementation NSData (XMLPlistWriting)
  456. - (NSString *)XMLPlistStringWithIndent:(NSString *)indent
  457. {
  458. NSString *base64 = [self base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0];
  459. return [NSString stringWithFormat:@"<data>\n%@%@\n%@</data>", indent, base64, indent];
  460. }
  461. @end
  462. @implementation NSArray (XMLPlistWriting)
  463. - (NSString *)XMLPlistStringWithIndent:(NSString *)indent
  464. {
  465. NSMutableString *xml = [NSMutableString string];
  466. [xml appendString:@"<array>\n"];
  467. NSString *subindent = [indent stringByAppendingString:@"\t"];
  468. for (id value in self)
  469. {
  470. [xml appendString:subindent];
  471. [xml appendString:[value XMLPlistStringWithIndent:subindent]];
  472. [xml appendString:@"\n"];
  473. }
  474. [xml appendString:indent];
  475. [xml appendString:@"</array>"];
  476. return xml;
  477. }
  478. @end
  479. @implementation NSDictionary (XMLPlistWriting)
  480. - (NSString *)XMLPlistStringWithIndent:(NSString *)indent
  481. {
  482. NSMutableString *xml = [NSMutableString string];
  483. [xml appendString:@"<dict>\n"];
  484. NSString *subindent = [indent stringByAppendingString:@"\t"];
  485. [self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull value, __unused BOOL *stop) {
  486. [xml appendString:subindent];
  487. [xml appendFormat:@"<key>%@</key>\n", [[key description] XMLEscapedString]];
  488. [xml appendString:subindent];
  489. [xml appendString:[value XMLPlistStringWithIndent:subindent]];
  490. [xml appendString:@"\n"];
  491. }];
  492. [xml appendString:indent];
  493. [xml appendString:@"</dict>"];
  494. return xml;
  495. }
  496. @end
  497. @implementation OrderedDictionaryXMLPlistParser
  498. {
  499. NSDateFormatter *_formatter;
  500. NSMutableArray *_valueStack;
  501. NSMutableArray *_keyStack;
  502. NSString *_text;
  503. BOOL _failed;
  504. }
  505. - (instancetype)initWithData:(NSData *)data root:(OrderedDictionary *)root
  506. {
  507. if ((self = [super init]))
  508. {
  509. _root = [root isKindOfClass:[MutableOrderedDictionary class]] ? root : nil;
  510. _keyStack = [NSMutableArray array];
  511. NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
  512. parser.delegate = self;
  513. [parser parse];
  514. }
  515. return self;
  516. }
  517. - (void)failWithError:(NSString *)error
  518. {
  519. NSLog(@"OrderedDictionary XML parsing error: %@", error);
  520. _failed = YES;
  521. _root = nil;
  522. }
  523. - (void)parser:(NSXMLParser *)parser didStartElement:(nonnull NSString *)elementName namespaceURI:(__unused NSString *)namespaceURI qualifiedName:(__unused NSString *)qName attributes:(__unused NSDictionary<NSString *, NSString *> *)attributeDict
  524. {
  525. if ([elementName isEqualToString:@"dict"])
  526. {
  527. if (_valueStack == nil)
  528. {
  529. _valueStack = [NSMutableArray arrayWithObject:_root ?: [MutableOrderedDictionary dictionary]];
  530. }
  531. else
  532. {
  533. [_valueStack addObject:[MutableOrderedDictionary dictionary]];
  534. }
  535. }
  536. else if (![elementName isEqualToString:@"plist"] && _valueStack == nil)
  537. {
  538. [self failWithError:@"Root element was not a dictionary."];
  539. [parser abortParsing];
  540. return;
  541. }
  542. else if ([elementName isEqualToString:@"array"])
  543. {
  544. [_valueStack addObject:[NSMutableArray array]];
  545. }
  546. }
  547. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(__unused NSString *)namespaceURI qualifiedName:(__unused NSString *)qName
  548. {
  549. id value = [_text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  550. _text = nil;
  551. if ([elementName isEqualToString:@"key"])
  552. {
  553. [_keyStack addObject:value ?: @""];
  554. return;
  555. }
  556. if ([elementName isEqualToString:@"string"])
  557. {
  558. // Do nothing
  559. }
  560. else if ([elementName isEqualToString:@"real"])
  561. {
  562. value = @([value doubleValue]);
  563. }
  564. else if ([elementName isEqualToString:@"integer"])
  565. {
  566. value = @([value integerValue]);
  567. }
  568. else if ([elementName isEqualToString:@"date"])
  569. {
  570. if (!_formatter)
  571. {
  572. _formatter = [[NSThread currentThread] XMLPlistDateFormatter];
  573. }
  574. NSString *dateString = value;
  575. if (!(value = [_formatter dateFromString:dateString]))
  576. {
  577. [self failWithError:[NSString stringWithFormat:@"Unabled to parse date string: %@", dateString]];
  578. [parser abortParsing];
  579. return;
  580. }
  581. }
  582. else if ([elementName isEqualToString:@"data"])
  583. {
  584. NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding];
  585. if (!(value = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters]))
  586. {
  587. [self failWithError:@"Unabled to parse data."];
  588. [parser abortParsing];
  589. return;
  590. }
  591. }
  592. else if ([elementName isEqualToString:@"true"])
  593. {
  594. value = @YES;
  595. }
  596. else if ([elementName isEqualToString:@"false"])
  597. {
  598. value = @NO;
  599. }
  600. else if ([elementName isEqualToString:@"dict"] || [elementName isEqualToString:@"array"])
  601. {
  602. value = [_valueStack.lastObject copy];
  603. [_valueStack removeLastObject];
  604. }
  605. else if ([elementName isEqualToString:@"plist"])
  606. {
  607. return;
  608. }
  609. id top = _valueStack.lastObject;
  610. if ([top isKindOfClass:[MutableOrderedDictionary class]])
  611. {
  612. NSString *key = _keyStack.lastObject;
  613. ((MutableOrderedDictionary *)top)[key] = value;
  614. [_keyStack removeLastObject];
  615. }
  616. else if ([top isKindOfClass:[NSArray class]])
  617. {
  618. [(NSMutableArray *)top addObject:value];
  619. }
  620. else if (_root == nil && !_failed)
  621. {
  622. _root = [value copy];
  623. }
  624. }
  625. - (void)parser:(__unused NSXMLParser *)parser foundCharacters:(NSString *)string
  626. {
  627. _text = [_text ?: @"" stringByAppendingString:string];
  628. }
  629. @end