From fd96ccd439a77fe4af1b2ed1eef9584b0d9b2737 Mon Sep 17 00:00:00 2001 From: rfm Date: Sat, 7 Jan 2012 13:08:03 +0000 Subject: [PATCH] Make sure code passes testsuite git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34456 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 14 +++++++++++ Source/NSXMLDocument.m | 40 +++++++++++++++++++++++-------- Source/NSXMLElement.m | 53 +++++++++++++++++++++++++++++------------- Source/NSXMLNode.m | 12 +++++----- Source/NSXMLPrivate.h | 1 - 5 files changed, 87 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0a7e8e57f..caa79c1b8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2012-01-05 Richard Frith-Macdonald + + * Source/NSXMLDocument.m: + * Source/NSXMLPrivate.h: + * Source/NSXMLNode.m: + * Source/NSXMLElement.m: + Replace missing assertion (needed for OSX compatibility) also fix + several other issues in node hierarchy handling ... all rather + academic since greg's going to rewrite, but these fixes do mean + the current code passes the testsuite. It's a bad idea to leave + the code and testsuite in an inconsistent state, and the testsuite + represents accumulated information about OSX, so we want our code + to pass all tests rather than removing tests. + 2012-01-06 17:57-EST Gregory John Casamento * Source/NSXMLElement.m: Removing assertion that diff --git a/Source/NSXMLDocument.m b/Source/NSXMLDocument.m index 9cdf6457b..33d1b3080 100644 --- a/Source/NSXMLDocument.m +++ b/Source/NSXMLDocument.m @@ -57,6 +57,10 @@ GS_PRIVATE_INTERNAL(NSXMLDocument) { if (GS_EXISTS_INTERNAL) { + while (internal->childCount > 0) + { + [self removeChildAtIndex: internal->childCount - 1]; + } [internal->encoding release]; [internal->version release]; [internal->docType release]; @@ -218,15 +222,18 @@ GS_PRIVATE_INTERNAL(NSXMLDocument) - (void) setRootElement: (NSXMLNode*)root { - NSArray *children; + if (nil != root) + { + NSArray *children; - NSAssert(internal->rootElement == nil, NSGenericException); - /* this method replaces *all* children with the specified element. - */ - children = [[NSArray alloc] initWithObjects: &root count: 1]; - [self setChildren: children]; - [children release]; - internal->rootElement = (NSXMLElement*)root; + NSAssert(internal->rootElement == nil, NSGenericException); + /* this method replaces *all* children with the specified element. + */ + children = [[NSArray alloc] initWithObjects: &root count: 1]; + [self setChildren: children]; + [children release]; + internal->rootElement = (NSXMLElement*)root; + } } - (void) setStandalone: (BOOL)standalone @@ -427,10 +434,23 @@ GS_PRIVATE_INTERNAL(NSXMLDocument) - (id) copyWithZone: (NSZone *)zone { NSXMLDocument *c = (NSXMLDocument*)[super copyWithZone: zone]; + NSXMLElement *r = [self rootElement]; + NSEnumerator *en; + id obj; [c setStandalone: internal->standalone]; - [c setChildren: internal->children]; - GSIVar(c, rootElement) = internal->rootElement; + en = [internal->children objectEnumerator]; + while ((obj = [en nextObject]) != nil) + { + NSXMLNode *child = [obj copyWithZone: zone]; + + if ([child isEqual: r]) + { + GSIVar(c, rootElement) = (NSXMLElement*)child; + } + [c addChild: child]; + [child release]; + } [c setDTD: internal->docType]; [c setMIMEType: internal->MIMEType]; return c; diff --git a/Source/NSXMLElement.m b/Source/NSXMLElement.m index d8896b865..a2bc920db 100644 --- a/Source/NSXMLElement.m +++ b/Source/NSXMLElement.m @@ -35,6 +35,10 @@ GS_PRIVATE_INTERNAL(NSXMLElement) { if (GS_EXISTS_INTERNAL && _internal != nil) { + while (internal->childCount > 0) + { + [self removeChildAtIndex: internal->childCount - 1]; + } [internal->attributes release]; [internal->namespaces release]; } @@ -220,7 +224,8 @@ GS_PRIVATE_INTERNAL(NSXMLElement) NSAssert(nil != child, NSInvalidArgumentException); NSAssert(index <= internal->childCount, NSInvalidArgumentException); - kind = [child kind]; + NSAssert(nil == [child parent], NSInternalInconsistencyException); + kind = [child kind]; // FIXME ... should we check for valid kinds rather than invalid ones? NSAssert(NSXMLAttributeKind != kind, NSInvalidArgumentException); NSAssert(NSXMLDTDKind != kind, NSInvalidArgumentException); @@ -254,19 +259,22 @@ GS_PRIVATE_INTERNAL(NSXMLElement) - (void) removeChildAtIndex: (NSUInteger)index { - NSXMLNode *child = [internal->children objectAtIndex: index]; + NSXMLNode *child; - if (nil != child) + if (index >= internal->childCount) { - GSIVar(child, parent) = nil; - [internal->children removeObjectAtIndex: index]; - if (0 == --internal->childCount) - { - /* The -children method must return nil if there are no children, - * so we destroy the container. - */ - DESTROY(internal->children); - } + [NSException raise: NSRangeException + format: @"index to large"]; + } + child = [internal->children objectAtIndex: index]; + GSIVar(child, parent) = nil; + [internal->children removeObjectAtIndex: index]; + if (0 == --internal->childCount) + { + /* The -children method must return nil if there are no children, + * so we destroy the container. + */ + DESTROY(internal->children); } } @@ -346,22 +354,35 @@ GS_PRIVATE_INTERNAL(NSXMLElement) - (id) copyWithZone: (NSZone *)zone { NSXMLElement *c = (NSXMLElement*)[super copyWithZone: zone]; - NSEnumerator *en = [internal->namespaces objectEnumerator]; - id obj = nil; + NSEnumerator *en; + id obj; + en = [internal->namespaces objectEnumerator]; while ((obj = [en nextObject]) != nil) { - [c addNamespace: [obj copyWithZone: zone]]; + NSXMLNode *ns = [obj copyWithZone: zone]; + + [c addNamespace: ns]; + [ns release]; } en = [internal->attributes objectEnumerator]; while ((obj = [en nextObject]) != nil) { NSXMLNode *attr = [obj copyWithZone: zone]; + [c addAttribute: attr]; + [attr release]; } - [c setChildren: [self children]]; + en = [internal->children objectEnumerator]; + while ((obj = [en nextObject]) != nil) + { + NSXMLNode *child = [obj copyWithZone: zone]; + + [c addChild: child]; + [child release]; + } return c; } diff --git a/Source/NSXMLNode.m b/Source/NSXMLNode.m index d2917f422..0f1a74f2c 100644 --- a/Source/NSXMLNode.m +++ b/Source/NSXMLNode.m @@ -227,11 +227,7 @@ GS_PRIVATE_INTERNAL(NSXMLNode) - (void) detach { - if (internal->parent != nil) - { - [(NSXMLElement*)internal->parent removeChildAtIndex: internal->index]; - internal->parent = nil; - } + [(NSXMLElement*)internal->parent removeChildAtIndex: [self index]]; } - (NSUInteger) hash @@ -241,7 +237,11 @@ GS_PRIVATE_INTERNAL(NSXMLNode) - (NSUInteger) index { - return internal->index; + if (nil == internal->parent) + { + return 0; + } + return [GSIVar(internal->parent, children) indexOfObjectIdenticalTo: self]; } - (id) init diff --git a/Source/NSXMLPrivate.h b/Source/NSXMLPrivate.h index 92113bcd0..d0c09513e 100644 --- a/Source/NSXMLPrivate.h +++ b/Source/NSXMLPrivate.h @@ -52,7 +52,6 @@ void *handle; \ NSUInteger kind; \ NSXMLNode *parent; \ - NSUInteger index; \ id objectValue; \ NSString *stringValue; \ NSString *name; \