* Source/NSXMLDocument.m: Remove commented out code

from old implementation.
	* Source/NSXMLNode.m: Flesh out functions started by
	doug to test equality for nodes.
	* Source/NSXMLPrivate.h: Remove uneeded ivars.
	* Tests/base/NSXMLNode/basic.m: Add tests to for equality.



git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/branches/nsxml_using_libxml2@34701 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Gregory John Casamento 2012-02-03 03:58:42 +00:00
parent cccdbe5428
commit a43c970b66
5 changed files with 193 additions and 90 deletions

View file

@ -1,3 +1,12 @@
2012-02-02 22:57-EST Gregory John Casamento <greg.casamento@gmail.com>
* Source/NSXMLDocument.m: Remove commented out code
from old implementation.
* Source/NSXMLNode.m: Flesh out functions started by
doug to test equality for nodes.
* Source/NSXMLPrivate.h: Remove uneeded ivars.
* Tests/base/NSXMLNode/basic.m: Add tests to for equality.
2012-02-01 11:32-EST Gregory John Casamento <greg.casamento@gmail.com>
* Source/NSXMLNode.m: Eliminate warning message which was

View file

@ -427,59 +427,7 @@ extern void clearPrivatePointers(xmlNodePtr aNode);
internal->node = (xmlDoc *)xmlCopyDoc(MY_DOC, 1); // copy recursively
clearPrivatePointers(internal->node); // clear out all of the _private pointers in the entire tree
((xmlNodePtr)internal->node)->_private = c;
// [c setStandalone: MY_DOC->standalone];
// [c setChildren: MY_DOC->children];
//GSIVar(c, rootElement) = MY_DOC->rootElement;
// [c setDTD: MY_DOC->docType];
// [c setMIMEType: MY_DOC->MIMEType];
return c;
}
@end
/*
@implementation NSXMLDocument (NSXMLParserDelegate)
- (void) parser: (NSXMLParser *)parser
didStartElement: (NSString *)elementName
namespaceURI: (NSString *)namespaceURI
qualifiedName: (NSString *)qualifiedName
attributes: (NSDictionary *)attributeDict
{
NSXMLElement *lastElement = [internal->elementStack lastObject];
NSXMLElement *currentElement =
[[NSXMLElement alloc] initWithName: elementName];
[lastElement addChild: currentElement];
[internal->elementStack addObject: currentElement];
[currentElement release];
if (nil == internal->rootElement)
{
[self setRootElement: currentElement];
}
[currentElement setAttributesAsDictionary: attributeDict];
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([internal->elementStack count] > 0)
{
NSXMLElement *currentElement = [internal->elementStack lastObject];
if ([[currentElement name] isEqualToString: elementName])
{
[internal->elementStack removeLastObject];
}
}
}
- (void) parser: (NSXMLParser *)parser
foundCharacters: (NSString *)string
{
NSXMLElement *currentElement = [internal->elementStack lastObject];
[currentElement setStringValue: string];
}
@end
*/

View file

@ -29,6 +29,163 @@
#import "GSInternal.h"
GS_PRIVATE_INTERNAL(NSXMLNode)
int countAttributes(xmlNodePtr node)
{
int count = 0;
xmlAttrPtr attr = node->properties;
while (attr)
{
count++;
attr = attr->next;
}
return count;
}
BOOL isEqualAttr(xmlAttrPtr attrA, xmlAttrPtr attrB)
{
xmlChar* contentA;
xmlChar* contentB;
xmlChar* nameA;
xmlChar* nameB;
// what has to be the same for two attributes to be equal -- just their values??
if(attrA == NULL || attrB == NO)
{
return NO;
}
if(attrB == NULL && attrA == NULL)
{
return YES;
}
// get the content...
contentA = xmlNodeGetContent((const xmlNodePtr)attrA);
contentB = xmlNodeGetContent((const xmlNodePtr)attrB);
nameA = (xmlChar *)attrA->name;
nameB = (xmlChar *)attrB->name;
if(strcmp((const char *)nameA,
(const char *)nameB) == 0)
{
if(strcmp((const char *)contentA,
(const char *)contentB) == 0)
{
return YES;
}
return NO;
}
return NO;
}
xmlAttrPtr findAttrWithName(xmlNodePtr node, xmlChar* targetName)
{
xmlAttrPtr attr = node->properties;
// find an attr in node with the given name, and return it, else NULL
if(attr == NULL)
{
return NULL;
}
while (strcmp((const char *)attr->name,(const char *)targetName) != 0)
{
attr = attr->next;
}
return attr;
}
BOOL isEqualAttributes(xmlNodePtr nodeA, xmlNodePtr nodeB)
{
xmlAttrPtr attrA = NULL;
if (countAttributes(nodeA) != countAttributes(nodeB))
return NO;
attrA = nodeA->properties;
while (attrA)
{
xmlAttrPtr attrB = findAttrWithName(nodeB, (xmlChar *)attrA->name);
if (!isEqualAttr(attrA, attrB))
{
return NO;
}
attrA = attrA->next;
}
return YES;
}
BOOL isEqualNode(xmlNodePtr nodeA, xmlNodePtr nodeB)
{
if (nodeA == nodeB)
return YES;
if (nodeA->type != nodeB->type)
return NO;
if (strcmp((const char *)nodeA->name,
(const char *)nodeB->name) != 0)
return NO;
if (nodeA->type == XML_ELEMENT_NODE)
{
xmlChar *contentA = NULL;
xmlChar *contentB = NULL;
if (!isEqualAttributes(nodeA, nodeB))
{
return NO;
}
// Get the value of any text node underneath the current element.
contentA = xmlNodeGetContent((const xmlNodePtr)nodeA);
contentB = xmlNodeGetContent((const xmlNodePtr)nodeB);
if(strcmp((const char *)contentA,
(const char *)contentB) != 0)
{
return NO;
}
}
return YES;
}
BOOL isEqualTree(xmlNodePtr nodeA, xmlNodePtr nodeB)
{
if (nodeA == NULL && nodeB == NULL)
{
return YES;
}
if (nodeA == NULL || nodeB == NULL)
{
return NO;
}
if (!isEqualNode(nodeA, nodeB))
{
return NO;
}
if (!isEqualTree(nodeA->children, nodeB->children))
{
return NO;
}
if (!isEqualTree(nodeA->next, nodeB->next))
{
return NO;
}
return YES;
}
// Private methods to manage libxml pointers...
@interface NSXMLNode (Private)
- (void *) _node;
@ -989,43 +1146,11 @@ NSLog(@"RELEASING TRICKY EXTRA RETAIN in %@ now: %d", self, internal->externalRe
- (BOOL) isEqual: (id)other
{
NSString *s;
NSArray *c;
if (other == (id)self)
{
return YES;
}
if (NO == [other isKindOfClass: [self class]])
if([self kind] != [other kind])
{
return NO;
}
if ([(NSXMLNode*)other kind] != internal->kind)
{
return NO;
}
s = [other name];
if (s != [self name] && NO == [s isEqual: [self name]])
{
return NO;
}
s = [other URI];
if (s != internal->URI && NO == [s isEqual: internal->URI])
{
return NO;
}
c = [other children];
if (c != [self children] && NO == [c isEqual: [self children]])
{
return NO;
}
return YES;
return isEqualTree(MY_NODE,(xmlNodePtr)[other _node]);
}
- (NSXMLNodeKind) kind

View file

@ -101,10 +101,8 @@ StringFromXMLString(const unsigned char *bytes, unsigned length)
* nodes are changed.
*/
#define GS_NSXMLNode_IVARS \
void *handle; \
NSUInteger kind; \
NSXMLNode *parent; \
NSUInteger index; \
id objectValue; \
NSString *URI; \
NSXMLNode *previousSibling; \
@ -112,7 +110,6 @@ StringFromXMLString(const unsigned char *bytes, unsigned length)
NSUInteger options; \
void *node; \
NSMutableArray *subNodes; \
NSString *xpath; \
int externalRetains; \
int retainedSelf; \
@ -137,7 +134,6 @@ StringFromXMLString(const unsigned char *bytes, unsigned length)
#define GS_NSXMLDocument_IVARS SUPERIVARS(GS_NSXMLNode_IVARS) \
NSXMLDTD *docType; \
NSString *MIMEType; \
NSUInteger fidelityMask; \
NSInteger contentKind; \

View file

@ -7,6 +7,7 @@ int main()
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSXMLNode *node;
NSXMLNode *other;
NSXMLNode *attr;
node = [[NSXMLNode alloc] initWithKind: NSXMLInvalidKind];
test_alloc(@"NSXMLNode");
@ -50,6 +51,30 @@ int main()
[node release];
[other release];
// Equality tests.
node = [[NSXMLNode alloc] initWithKind: NSXMLElementKind];
other = [[NSXMLNode alloc] initWithKind: NSXMLElementKind];
[other setName: @"test"];
[node setName: @"test"];
PASS([node isEqual: other],
"Nodes with the same name are equal");
attr = [NSXMLNode attributeWithName: @"key"
stringValue: @"value"];
[node addAttribute:attr];
PASS(![node isEqual: other],
"Nodes with different attributes are NOT equal");
attr = [NSXMLNode attributeWithName: @"key"
stringValue: @"value"];
[other addAttribute:attr];
PASS([node isEqual: other],
"Nodes with the same attributes are equal");
[node release];
[other release];
[arp release];
arp = nil;