diff --git a/ChangeLog b/ChangeLog index 74ab5ca54..2863a33a6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2012-01-12 18:18-EST Gregory John Casamento + + * Source/NSXMLElement.m + * Source/NSXMLNode.m: Add code to automatically instantiate + the node to fill in the _private pointer if it is not present. + 2012-01-12 12:05-EST Gregory John Casamento * Source/NSXMLElement.m: Changes to make NSXMLElement use diff --git a/Source/NSXMLElement.m b/Source/NSXMLElement.m index 07d991303..aeeea2f97 100644 --- a/Source/NSXMLElement.m +++ b/Source/NSXMLElement.m @@ -229,6 +229,7 @@ GS_PRIVATE_INTERNAL(NSXMLElement) - (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index { NSXMLNodeKind kind; + xmlNodePtr node = (xmlNodePtr)(internal->node); NSAssert(nil != child, NSInvalidArgumentException); NSAssert(index <= internal->childCount, NSInvalidArgumentException); @@ -323,42 +324,6 @@ GS_PRIVATE_INTERNAL(NSXMLElement) [self notImplemented: _cmd]; } -- (NSString *) XMLStringWithOptions: (NSUInteger)options -{ - NSMutableString *result = [NSMutableString string]; - NSEnumerator *en = nil; - id object = nil; - - // XML Element open tag... - [result appendString: [NSString stringWithFormat: @"<%@",[self name]]]; - - // get the attributes... - en = [[self attributes] objectEnumerator]; - while ((object = [en nextObject]) != nil) - { - [result appendString: @" "]; - [result appendString: [object XMLStringWithOptions: options]]; - } - // close the brackets... - [result appendString: @">"]; - - [result appendString: [self stringValue]]; // need to escape entities... - - // Iterate over the children... - en = [[self children] objectEnumerator]; - while ((object = [en nextObject]) != nil) - { - [result appendString: @" "]; - [result appendString: [object XMLStringWithOptions: options]]; - } - - // Close the entire tag... - [result appendString: [NSString stringWithFormat: @"",[self name]]]; - - // return - return result; -} - - (id) copyWithZone: (NSZone *)zone { NSXMLElement *c = (NSXMLElement*)[super copyWithZone: zone]; diff --git a/Source/NSXMLNode.m b/Source/NSXMLNode.m index 300d30494..c5698927c 100644 --- a/Source/NSXMLNode.m +++ b/Source/NSXMLNode.m @@ -33,6 +33,7 @@ GS_PRIVATE_INTERNAL(NSXMLNode) @interface NSXMLNode (Private) - (void *) _node; - (void) _setNode: (void *)_anode; ++ (id) _newFromNode: (xmlNodePtr)node; @end @implementation NSXMLNode (Private) @@ -46,6 +47,34 @@ GS_PRIVATE_INTERNAL(NSXMLNode) ((xmlNodePtr)_anode)->_private = self; internal->node = _anode; } + ++ (id) _newFromNode: (xmlNodePtr)node +{ + xmlElementType type = node->type; + NSXMLNode *result = (id)node->_private; + xmlChar *name = NULL; + // NSXMLNodeKind kind = 0; + + if(result == NULL) + { + switch(type) + { + case(XML_ELEMENT_NODE): + name = (xmlChar *)node->name; + result = [[self alloc] initWithKind: NSXMLElementKind]; + break; + case(XML_ATTRIBUTE_NODE): + name = (xmlChar *)node->name; + result = [[self alloc] initWithKind: NSXMLAttributeKind]; + [result setStringValue: StringFromXMLStringPtr(node->content)]; + break; + default: + break; + } + } + + return result; +} @end @implementation NSXMLNode @@ -60,8 +89,8 @@ GS_PRIVATE_INTERNAL(NSXMLNode) [n setStringValue: stringValue]; [n setName: name]; node = xmlNewProp(NULL, - (xmlChar *)[name UTF8String], - (xmlChar *)[stringValue UTF8String]); + XMLSTRING(name), + XMLSTRING(stringValue)); [n _setNode: node]; return n; @@ -219,17 +248,45 @@ GS_PRIVATE_INTERNAL(NSXMLNode) - (NSXMLNode*) childAtIndex: (NSUInteger)index { - return [internal->children objectAtIndex: index]; + NSUInteger count = 0; + xmlNodePtr children = NULL; + xmlNodePtr node = (xmlNodePtr)(internal->node); + + for (children = node->children; children && count != index; children = children->next) + { + count++; + } + + return (NSXMLNode *)[NSXMLNode _newFromNode: children]; } - (NSUInteger) childCount { - return internal->childCount; + NSUInteger childCount = 0; + xmlNodePtr children = NULL; + xmlNodePtr node = (xmlNodePtr)(internal->node); + + for (children = node->children; children; children = children->next) + { + childCount++; + } + + return childCount; } - (NSArray*) children { - return internal->children; + NSMutableArray *childrenArray = [NSMutableArray array]; + xmlNodePtr children = NULL; + xmlNodePtr node = (xmlNodePtr)(internal->node); + + for (children = node->children; children; children = children->next) + { + NSXMLNode *n = [NSXMLNode _newFromNode: children]; + [childrenArray addObject: n]; + } + + return childrenArray; } - (id) copyWithZone: (NSZone*)zone