mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 08:21:25 +00:00
* 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:
parent
a9e3ce01aa
commit
e9907e6d40
3 changed files with 69 additions and 41 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue