Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transitioned to ARC. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions XMLReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@

#import <Foundation/Foundation.h>


@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<NSXMLParserDelegate>
+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error;
+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error;
@end
64 changes: 23 additions & 41 deletions XMLReader.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
}

Expand All @@ -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];
Expand All @@ -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];
}
Expand All @@ -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
Expand All @@ -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