Skip to content

Commit

Permalink
Added XML initialization methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MacMannes committed Apr 29, 2012
1 parent 4f43ceb commit 4b86b71
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 11 deletions.
33 changes: 30 additions & 3 deletions MMObjectModel/MMObjectModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,55 @@
@param dictionary A dictionary.
@returns A `MMObjectModel` object
@see initWithJSONData:
@see initWithXMLData:
@see initWithXMLData:rootElement:
*/
- (id)initWithDictionary:(NSMutableDictionary *)dictionary;

/**
creates and returns a `MMObjectModel` object initialized using the provided JSON data object
@param jsonData An 'NSData' object containing JSON.
@param jsonData An `NSData` object containing JSON.
@returns A `MMObjectModel` object
@see initWithDictionary:
@see initWithXMLData:
@see initWithXMLData:rootElement:
*/
- (id)initWithJSONData:(NSData *)jsonData;

/**
Returns a dictionary presentation of this object.
creates and returns a `MMObjectModel` object initialized using the provided JSON data object
@param xmlData An `NSData` object containing XML.
@returns A `MMObjectModel` object
@see initWithXMLData:rootElement:
@see initWithJSONData:
@see initWithDictionary:
*/
- (NSDictionary *)dictionary;
- (id)initWithXMLData:(NSData *)xmlData;

/**
creates and returns a `MMObjectModel` object initialized using the provided JSON data object
@param xmlData An `NSData` object containing XML.
@param rootElement An `NSString` object containing the name of the XML root element.
@returns A `MMObjectModel` object
@see initWithJSONData:
@see initWithDictionary:
@see initWithXMLData:
*/
- (id)initWithXMLData:(NSData *)xmlData rootElement:(NSString *)rootElement;

/**-------------------------------------------------------------------------------------
@name Serialization methods
---------------------------------------------------------------------------------------
*/

/**
Returns a dictionary presentation of this object.
*/
- (NSDictionary *)dictionary;

/**
Returns JSON data in compact format (without spaces and indents).
*/
Expand Down
27 changes: 27 additions & 0 deletions MMObjectModel/MMObjectModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// THE SOFTWARE.

#import "MMObjectModel.h"
#import "MMXMLReader.h"
#import <objc/runtime.h>

@implementation MMObjectModel
Expand All @@ -47,6 +48,32 @@ - (id)initWithJSONData: (NSData *)jsonData
}
}

- (id)initWithXMLData:(NSData *)xmlData
{
return [self initWithXMLData:xmlData rootElement:nil];
}

- (id)initWithXMLData:(NSData *)xmlData rootElement:(NSString *)rootElement
{
MMXMLReader *reader = [[MMXMLReader alloc] initWithData:xmlData];
if (reader) {
NSMutableDictionary *xmlDict = [reader convertToDictionary];
if (xmlDict) {
if (rootElement) {
NSMutableDictionary *rootDict = [xmlDict objectForKey:rootElement];
if (rootDict) {
return [self initWithDictionary:rootDict];
}
} else {
return [self initWithDictionary:xmlDict];
}
}
}

return nil;

}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
DLog(@"Undefined key: %@", key);
Expand Down
14 changes: 11 additions & 3 deletions MMObjectModel/MMXMLReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
// THE SOFTWARE.

#import <Foundation/Foundation.h>

Expand All @@ -49,13 +48,22 @@
*/
- (id)initWithURL:(NSURL *)url;

/**
Initializes the receiver with the XML contents encapsulated in a given data object.
@param data An `NSData` object containing XML markup.
@returns An initialized `MMXMLreader` object or nil if an error occurs.
*/
- (id)initWithData:(NSData *)data;


/**-------------------------------------------------------------------------------------
@name Parsing
---------------------------------------------------------------------------------------
*/

/**
Starts the parsing operation and returns an 'NSMutableDictionary' object if succesful.
Starts the parsing operation and returns an `NSMutableDictionary` object if succesful.
@returns An `NSMutableDictionary` object or nil if an error occurs.
*/
Expand Down
34 changes: 29 additions & 5 deletions MMObjectModel/MMXMLReader.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,37 @@

#import "MMXMLReader.h"

@interface MMXMLReader (PRIVATE)

- (void)performInitialization;

@end

@implementation MMXMLReader

- (id)initWithURL:(NSURL *)url
{
if (self = [super init]) {
_dictionaryStack = [[NSMutableArray alloc] init];
_textInProgress = [[NSMutableString alloc] init];

// Initialize the stack with a fresh dictionary
[_dictionaryStack addObject:[NSMutableDictionary dictionary]];
[self performInitialization];

_xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[_xmlParser setDelegate:self];
}
return self;
}

- (id)initWithData:(NSData *)data
{
if (self = [super init]) {
[self performInitialization];

_xmlParser = [[NSXMLParser alloc] initWithData:data];
[_xmlParser setDelegate:self];
}
return self;
}


- (NSMutableDictionary *)convertToDictionary
{
if ([_xmlParser parse]) {
Expand All @@ -51,6 +65,16 @@ - (NSMutableDictionary *)convertToDictionary
return nil;
}

- (void)performInitialization
{
_dictionaryStack = [[NSMutableArray alloc] init];
_textInProgress = [[NSMutableString alloc] init];

// Initialize the stack with a fresh dictionary
[_dictionaryStack addObject:[NSMutableDictionary dictionary]];

}

#pragma mark - NSXMLParserDelegate

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
Expand Down

0 comments on commit 4b86b71

Please sign in to comment.