An attempt to fix NSXMLElement's -addAttribute. Also implement

-attributeForName: and add a few tests.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/branches/nsxml_using_libxml2@34640 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Niels Grewe 2012-01-26 21:29:49 +00:00
parent 2917fb9f3c
commit ae94271e12
3 changed files with 113 additions and 22 deletions

View file

@ -1,3 +1,9 @@
2012-01-26 22:28-CET Niels Grewe <niels.grewe@halbordnung.de>
* Source/NSXMLElement.m: Fix behaviour of -addAttribute:. Implement
-attributeForName:
* Tests/base/NSXMLElement/attributes.m: Add some attribute tests.
2012-01-24 21:59-EST Gregory John Casamento <greg.casamento@gmail.com>
* Tests/base/NSXMLElement/children.m: Remove test based

View file

@ -136,10 +136,38 @@ extern void clearPrivatePointers(xmlNodePtr aNode);
{
xmlNodePtr node = (xmlNodePtr)(internal->node);
xmlAttrPtr attr = (xmlAttrPtr)[attribute _node];
//xmlAddChild(node,attr);
// xmlSetProp(node, attr->name, attr->children);
xmlAttrPtr newAttr = xmlCopyProp(node, attr);
[attribute _setNode:newAttr];
xmlAttrPtr oldAttr = xmlHasProp(node, attr->name);
if (nil != [attribute parent])
{
[NSException raise: @"NSInvalidArgumentException"
format: @"Tried to add attribute to multiple parents."];
}
if (NULL != oldAttr)
{
/*
* As per Cocoa documentation, we only add the attribute if it's not
* already set. xmlHasProp() also looks at the DTD for default attributes
* and we need to make sure that we only bail out here on #FIXED
* attributes.
*/
// Do not replace plain attributes.
if (XML_ATTRIBUTE_NODE == oldAttr->type)
{
return;
}
else if (XML_ATTRIBUTE_DECL == oldAttr->type)
{
// If the attribute is from a DTD, do not replace it if it's #FIXED
xmlAttributePtr attrDecl = (xmlAttributePtr)oldAttr;
if (XML_ATTRIBUTE_FIXED == attrDecl->def)
{
return;
}
}
}
xmlAddChild(node, (xmlNodePtr)attr);
[self _addSubNode:attribute];
}
@ -196,8 +224,16 @@ extern void clearPrivatePointers(xmlNodePtr aNode);
- (NSXMLNode*) attributeForName: (NSString*)name
{
[self notImplemented: _cmd];
return nil; // [internal->attributes objectForKey: name];
NSXMLNode *result = nil;
xmlChar *xmlName = xmlCharStrdup([name UTF8String]);
xmlAttrPtr attributeNode = xmlHasProp(MY_NODE, xmlName);
if (NULL != attributeNode)
{
result = [NSXMLNode _objectForNode:(xmlNodePtr)attributeNode];
}
xmlFree(xmlName);
xmlName = NULL;
return result; // [internal->attributes objectForKey: name];
}
- (NSXMLNode*) attributeForLocalName: (NSString*)localName

View file

@ -0,0 +1,49 @@
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSXMLDocument.h>
#import <Foundation/NSXMLElement.h>
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSXMLElement *root1;
NSXMLElement *root2;
NSXMLNode *attr1;
NSXMLNode *attr2;
NSXMLNode *attrSameNameAsAttr1;
root1 = [[NSXMLElement alloc] initWithName: @"root1"];
root2 = [[NSXMLElement alloc] initWithName: @"root2"];
attr1 = [NSXMLNode attributeWithName: @"attr1" stringValue: @"foo"];
attr2 = [NSXMLNode attributeWithName: @"attr2" stringValue: @"foo"];
attrSameNameAsAttr1 = [NSXMLNode attributeWithName: @"attr1" stringValue: @"foo"];
PASS_RUNS([root1 addAttribute: attr1],
"may add attributes");
[root1 addAttribute: attr2];
PASS_EQUAL([root1 attributeForName: @"attr1"], attr1,
"element returns attribute by name");
PASS_RUNS([root1 removeAttributeForName: @"attr2"],
"removing attributes by name works");
PASS_EQUAL([root1 attributeForName: @"attr2"], nil,
"attribute is nil after removal");
[root1 addAttribute: attrSameNameAsAttr1];
PASS_EQUAL([root1 attributeForName: @"attr1"], attr1,
"may not overwrite pre-existing attributes");
PASS_EXCEPTION([root2 addAttribute: attr1],
NSInvalidArgumentException,
"cannot add attributes to multiple parents");
[root1 release];
[root2 release];
[arp release];
arp = nil;
return 0;
}