* Source/NSXMLElement.m

* Source/NSXMLNode.m: Add code to automatically instantiate 
	the node to fill in the _private pointer if it is not present.



git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/branches/nsxml_using_libxml2@34508 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Gregory John Casamento 2012-01-12 23:19:26 +00:00
parent a9e3ce01aa
commit e9907e6d40
3 changed files with 69 additions and 41 deletions

View file

@ -1,3 +1,9 @@
2012-01-12 18:18-EST Gregory John Casamento <greg.casamento@gmail.com>
* 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 <greg.casamento@gmail.com> 2012-01-12 12:05-EST Gregory John Casamento <greg.casamento@gmail.com>
* Source/NSXMLElement.m: Changes to make NSXMLElement use * Source/NSXMLElement.m: Changes to make NSXMLElement use

View file

@ -229,6 +229,7 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index - (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
{ {
NSXMLNodeKind kind; NSXMLNodeKind kind;
xmlNodePtr node = (xmlNodePtr)(internal->node);
NSAssert(nil != child, NSInvalidArgumentException); NSAssert(nil != child, NSInvalidArgumentException);
NSAssert(index <= internal->childCount, NSInvalidArgumentException); NSAssert(index <= internal->childCount, NSInvalidArgumentException);
@ -323,42 +324,6 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
[self notImplemented: _cmd]; [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 - (id) copyWithZone: (NSZone *)zone
{ {
NSXMLElement *c = (NSXMLElement*)[super copyWithZone: zone]; NSXMLElement *c = (NSXMLElement*)[super copyWithZone: zone];

View file

@ -33,6 +33,7 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
@interface NSXMLNode (Private) @interface NSXMLNode (Private)
- (void *) _node; - (void *) _node;
- (void) _setNode: (void *)_anode; - (void) _setNode: (void *)_anode;
+ (id) _newFromNode: (xmlNodePtr)node;
@end @end
@implementation NSXMLNode (Private) @implementation NSXMLNode (Private)
@ -46,6 +47,34 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
((xmlNodePtr)_anode)->_private = self; ((xmlNodePtr)_anode)->_private = self;
internal->node = _anode; 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 @end
@implementation NSXMLNode @implementation NSXMLNode
@ -60,8 +89,8 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
[n setStringValue: stringValue]; [n setStringValue: stringValue];
[n setName: name]; [n setName: name];
node = xmlNewProp(NULL, node = xmlNewProp(NULL,
(xmlChar *)[name UTF8String], XMLSTRING(name),
(xmlChar *)[stringValue UTF8String]); XMLSTRING(stringValue));
[n _setNode: node]; [n _setNode: node];
return n; return n;
@ -219,17 +248,45 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
- (NSXMLNode*) childAtIndex: (NSUInteger)index - (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 - (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 - (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 - (id) copyWithZone: (NSZone*)zone