From a552c92cb0afbe6ac59f25325ae1f1836f83d7fc Mon Sep 17 00:00:00 2001 From: genkernel Date: Sat, 14 Jul 2012 19:08:59 +0700 Subject: [PATCH] Transitioned to ARC. --- XMLReader.h | 14 +++--------- XMLReader.m | 64 +++++++++++++++++++---------------------------------- 2 files changed, 26 insertions(+), 52 deletions(-) diff --git a/XMLReader.h b/XMLReader.h index 3145071..fb488eb 100644 --- a/XMLReader.h +++ b/XMLReader.h @@ -5,15 +5,7 @@ #import - -@interface XMLReader : NSObject -{ - NSMutableArray *dictionaryStack; - NSMutableString *textInProgress; - NSError **errorPointer; -} - -+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)errorPointer; -+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)errorPointer; - +@interface XMLReader : NSObject ++ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error; ++ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error; @end diff --git a/XMLReader.m b/XMLReader.m index 6820ee6..1a77a09 100644 --- a/XMLReader.m +++ b/XMLReader.m @@ -4,65 +4,48 @@ #import "XMLReader.h" -NSString *const kXMLReaderTextNodeKey = @"text"; +static NSString *const kXMLReaderTextNodeKey = @"text"; -@interface XMLReader (Internal) +@interface XMLReader() +@property (strong, nonatomic) NSMutableArray *dictionaryStack; +@property (strong, nonatomic) NSMutableString *textInProgress; +@property (assign, nonatomic) NSError *__autoreleasing *error; +@end -- (id)initWithError:(NSError **)error; +@interface XMLReader (Internal) - (NSDictionary *)objectWithData:(NSData *)data; - @end @implementation XMLReader +@synthesize dictionaryStack, textInProgress; #pragma mark - #pragma mark Public methods -+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error ++ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)errorPointer { - XMLReader *reader = [[XMLReader alloc] initWithError:error]; - NSDictionary *rootDictionary = [reader objectWithData:data]; - [reader release]; - return rootDictionary; + XMLReader *reader = [XMLReader new]; + reader.error = errorPointer; + return [reader objectWithData:data]; } -+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error ++ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)errorPointer { NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; - return [XMLReader dictionaryForXMLData:data error:error]; + return [XMLReader dictionaryForXMLData:data error:errorPointer]; } #pragma mark - #pragma mark Parsing -- (id)initWithError:(NSError **)error -{ - if (self = [super init]) - { - errorPointer = error; - } - return self; -} - -- (void)dealloc -{ - [dictionaryStack release]; - [textInProgress release]; - [super dealloc]; -} - - (NSDictionary *)objectWithData:(NSData *)data { - // Clear out any old data - [dictionaryStack release]; - [textInProgress release]; - - dictionaryStack = [[NSMutableArray alloc] init]; - textInProgress = [[NSMutableString alloc] init]; + self.dictionaryStack = [[NSMutableArray alloc] init]; + self.textInProgress = [[NSMutableString alloc] init]; // Initialize the stack with a fresh dictionary - [dictionaryStack addObject:[NSMutableDictionary dictionary]]; + [self.dictionaryStack addObject:[NSMutableDictionary dictionary]]; // Parse the XML NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; @@ -72,7 +55,7 @@ - (NSDictionary *)objectWithData:(NSData *)data // Return the stack's root dictionary on success if (success) { - NSDictionary *resultDict = [dictionaryStack objectAtIndex:0]; + NSDictionary *resultDict = [self.dictionaryStack objectAtIndex:0]; return resultDict; } @@ -86,7 +69,7 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam { // Get the dictionary for the current level in the stack NSMutableDictionary *parentDict = [dictionaryStack lastObject]; - + // Create the child dictionary for the new element, and initilaize it with the attributes NSMutableDictionary *childDict = [NSMutableDictionary dictionary]; [childDict addEntriesFromDictionary:attributeDict]; @@ -106,7 +89,7 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam // Create an array if it doesn't exist array = [NSMutableArray array]; [array addObject:existingValue]; - + // Replace the child dictionary with an array of children dictionaries [parentDict setObject:array forKey:elementName]; } @@ -133,10 +116,9 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName names if ([textInProgress length] > 0) { [dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey]; - + // Reset the text - [textInProgress release]; - textInProgress = [[NSMutableString alloc] init]; + self.textInProgress = [[NSMutableString alloc] init]; } // Pop the current dict @@ -152,7 +134,7 @@ - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { // Set the error pointer to the parser's error object - *errorPointer = parseError; + (*self.error) = parseError; } @end