* Source/NSXMLDocument.m

* Source/NSXMLDTD.m
	* Source/NSXMLDTDNode.m
	* Source/NSXMLElement.m
	* Source/NSXMLNode.m
	* Source/NSXMLPrivate.h: Reimplementation of all DOM classes based on
	libxml2.  Implementation of all methods using libxml2 functions.
	* Tests/base/NSXMLDocument/basic.m
	* Tests/base/NSXMLElement/attributes.m
	* Tests/base/NSXMLElement/children.m
	* Tests/base/NSXMLNode/basic.m
	* Tests/base/NSXMLNode/children.m
	* Tests/base/NSXMLNode/kinds.m: Changes to test for new functionality.



git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34789 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
gcasa 2012-02-20 03:40:15 +00:00
parent 33819c457f
commit 82e85dab93
13 changed files with 1702 additions and 559 deletions

View file

@ -1,3 +1,20 @@
2012-02-19 22:37-EST Gregory John Casamento <greg.casamento@gmail.com>
Doug Simons <doug.simons@testplant.com>
* Source/NSXMLDocument.m
* Source/NSXMLDTD.m
* Source/NSXMLDTDNode.m
* Source/NSXMLElement.m
* Source/NSXMLNode.m
* Source/NSXMLPrivate.h: Reimplementation of all DOM classes based on
libxml2. Implementation of all methods using libxml2 functions.
* Tests/base/NSXMLDocument/basic.m
* Tests/base/NSXMLElement/attributes.m
* Tests/base/NSXMLElement/children.m
* Tests/base/NSXMLNode/basic.m
* Tests/base/NSXMLNode/children.m
* Tests/base/NSXMLNode/kinds.m: Changes to test for new functionality.
2012-02-08 Lubomir Rintel <lubo.rintel@gooddata.com>
* Source/NSHTTPCookie.m:

View file

@ -46,7 +46,6 @@ GS_PRIVATE_INTERNAL(NSXMLDTD)
[internal->entities release];
[internal->elements release];
[internal->notations release];
[internal->attributes release];
[internal->original release];
}
[super dealloc];
@ -76,6 +75,11 @@ GS_PRIVATE_INTERNAL(NSXMLDTD)
return nil;
}
- (void) _createInternal
{
GS_CREATE_INTERNAL(NSXMLDTD);
}
- (id) init
{
return [self initWithKind: NSXMLDTDKind options: 0];
@ -117,7 +121,6 @@ GS_PRIVATE_INTERNAL(NSXMLDTD)
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
{
[self notImplemented: _cmd];
internal->childrenHaveMutated = YES;
}
- (void) insertChildren: (NSArray*)children atIndex: (NSUInteger)index
@ -169,13 +172,11 @@ GS_PRIVATE_INTERNAL(NSXMLDTD)
- (void) setPublicID: (NSString*)publicID
{
[self notImplemented: _cmd];
internal->modified = YES;
}
- (void) setSystemID: (NSString*)systemID
{
[self notImplemented: _cmd];
internal->modified = YES;
}
- (NSString*) systemID

View file

@ -35,9 +35,6 @@ GS_PRIVATE_INTERNAL(NSXMLDTDNode)
{
if (GS_EXISTS_INTERNAL)
{
[internal->notationName release];
[internal->publicID release];
[internal->systemID release];
}
[super dealloc];
}
@ -69,9 +66,8 @@ GS_PRIVATE_INTERNAL(NSXMLDTDNode)
- (BOOL) isExternal
{
if (internal->systemID != nil)
if ([self systemID])
{
// FIXME ... libxml integration?
return YES;
}
return NO;
@ -79,53 +75,37 @@ GS_PRIVATE_INTERNAL(NSXMLDTDNode)
- (NSString*) notationName
{
if (internal->notationName == nil)
{
[self notImplemented: _cmd];
}
return internal->notationName;
return StringFromXMLStringPtr(MY_DTD->name);
}
- (NSString*) publicID
{
if (internal->publicID == nil)
{
[self notImplemented: _cmd];
}
return internal->publicID;
return StringFromXMLStringPtr(MY_DTD->ExternalID);
}
- (void) setDTDKind: (NSXMLDTDNodeKind)kind
{
internal->DTDKind = kind;
// FIXME ... libxml integration?
}
- (void) setNotationName: (NSString*)notationName
{
ASSIGNCOPY(internal->notationName, notationName);
// FIXME ... libxml integration?
MY_DTD->name = XMLSTRING(notationName);
}
- (void) setPublicID: (NSString*)publicID
{
ASSIGNCOPY(internal->publicID, publicID);
// FIXME ... libxml integration?
MY_DTD->ExternalID = XMLSTRING(publicID);
}
- (void) setSystemID: (NSString*)systemID
{
ASSIGNCOPY(internal->systemID, systemID);
// FIXME ... libxml integration?
MY_DTD->ExternalID = XMLSTRING(systemID);
}
- (NSString*) systemID
{
if (internal->systemID == nil)
{
[self notImplemented: _cmd];
}
return internal->systemID;
return StringFromXMLStringPtr(MY_DTD->SystemID);
}
@end

View file

@ -2,8 +2,9 @@
Copyright (C) 2008 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Created: September 2008
Written by: Gregory John Casamento <greg.casamento@gmail.com>
Created/Modified: September 2008,2012
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
@ -27,23 +28,31 @@
#define GSInternal NSXMLDocumentInternal
#import "NSXMLPrivate.h"
#import "GSInternal.h"
#ifdef HAVE_LIBXSLT
#import <libxslt/xslt.h>
#import <libxslt/xsltInternals.h>
#import <libxslt/transform.h>
#import <libxslt/xsltutils.h>
#endif
GS_PRIVATE_INTERNAL(NSXMLDocument)
#import <Foundation/NSXMLParser.h>
//#import <Foundation/NSXMLParser.h>
#import <Foundation/NSError.h>
@interface NSXMLDocument (Debug)
- (id) elementStack;
@end
#if defined(HAVE_LIBXML)
@implementation NSXMLDocument (Debug)
- (id) elementStack
{
return internal->elementStack;
}
@end
extern void clearPrivatePointers(xmlNodePtr aNode);
// Forward declaration of interface for NSXMLParserDelegate
@interface NSXMLDocument (NSXMLParserDelegate)
// Private methods to manage libxml pointers...
@interface NSXMLNode (Private)
- (void *) _node;
- (void) _setNode: (void *)_anode;
+ (NSXMLNode *) _objectForNode: (xmlNodePtr)node;
- (void) _addSubNode:(NSXMLNode *)subNode;
- (void) _removeSubNode:(NSXMLNode *)subNode;
- (void) _insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index;
@end
@implementation NSXMLDocument
@ -57,23 +66,18 @@ 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];
[internal->MIMEType release];
[internal->elementStack release];
[internal->xmlData release];
}
[super dealloc];
}
- (NSString*) characterEncoding
{
return internal->encoding;
if (MY_DOC->encoding)
return [NSString stringWithUTF8String: (const char *)MY_DOC->encoding];
else
return nil;
}
- (NSXMLDocumentContentKind) documentContentKind
@ -86,6 +90,11 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
return internal->docType;
}
- (void) _createInternal
{
GS_CREATE_INTERNAL(NSXMLDocument);
}
- (id) init
{
return [self initWithKind: NSXMLDocumentKind options: 0];
@ -99,7 +108,7 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
NSXMLDocument *doc;
data = [NSData dataWithContentsOfURL: url];
doc = [self initWithData: data options: 0 error: 0];
doc = [self initWithData: data options: mask error: error];
[doc setURI: [url absoluteString]];
return doc;
}
@ -109,51 +118,43 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
options: (NSUInteger)mask
error: (NSError**)error
{
if (NO == [data isKindOfClass: [NSData class]])
NSString *string = nil;
// Check for nil data and throw an exception
if (![data isKindOfClass: [NSData class]])
{
DESTROY(self);
if (nil == data)
{
[NSException raise: NSInvalidArgumentException
format: @"[NSXMLDocument-%@] nil argument",
NSStringFromSelector(_cmd)];
}
[NSException raise: NSInvalidArgumentException
format: @"[NSXMLDocument-%@] invalid argument",
NSStringFromSelector(_cmd)];
format: @"[NSXMLDocument-%@] non data argument",
NSStringFromSelector(_cmd)];
}
GS_CREATE_INTERNAL(NSXMLDocument)
if ((self = [super initWithKind: NSXMLDocumentKind options: 0]) != nil)
if (nil == data)
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData: data];
internal->standalone = YES;
internal->elementStack = [[NSMutableArray alloc] initWithCapacity: 10];
ASSIGN(internal->xmlData, data);
if (nil == parser)
{
DESTROY(self);
}
else
{
[parser setDelegate: self];
[parser parse];
RELEASE(parser);
}
DESTROY(self);
[NSException raise: NSInvalidArgumentException
format: @"[NSXMLDocument-%@] nil argument",
NSStringFromSelector(_cmd)];
}
return self;
// Initialize the string...
string = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
AUTORELEASE(string);
return [self initWithXMLString:string options:mask error:error];
}
- (id) initWithKind: (NSXMLNodeKind)kind options: (NSUInteger)theOptions
{
if (NSXMLDocumentKind == kind)
{
/* Create holder for internal instance variables so that we'll have
* all our ivars available rather than just those of the superclass.
*/
GS_CREATE_INTERNAL(NSXMLDocument)
return [super initWithKind: kind options: theOptions];
}
else
{
[self release];
return [[NSXMLNode alloc] initWithKind: kind
options: theOptions];
}
return [super initWithKind: kind options: theOptions];
}
- (id) initWithRootElement: (NSXMLElement*)element
@ -177,17 +178,47 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
options: (NSUInteger)mask
error: (NSError**)error
{
NSData *data = [NSData dataWithBytes: [string UTF8String]
length: [string length]];
self = [self initWithData: data
options: mask
error: error];
if (NO == [string isKindOfClass: [NSString class]])
{
DESTROY(self);
if (nil == string)
{
[NSException raise: NSInvalidArgumentException
format: @"[NSXMLDocument-%@] nil argument",
NSStringFromSelector(_cmd)];
}
[NSException raise: NSInvalidArgumentException
format: @"[NSXMLDocument-%@] invalid argument",
NSStringFromSelector(_cmd)];
}
if ((self = [self initWithKind: NSXMLDocumentKind options: 0]) != nil)
{
const char *str = [string UTF8String];
char *url = NULL;
char *encoding = NULL; // "UTF8";
int options = XML_PARSE_NOERROR;
xmlDocPtr doc = NULL;
if (!(mask & NSXMLNodePreserveWhitespace))
options |= XML_PARSE_NOBLANKS;
//xmlKeepBlanksDefault(0);
doc = xmlReadDoc((xmlChar *)str, url, encoding, options);
if(doc == NULL)
{
[self release];
self = nil;
if (error != NULL)
*error = [NSError errorWithDomain:@"NSXMLErrorDomain" code:0 userInfo:nil];
//[NSException raise:NSInvalidArgumentException
// format:@"Cannot instantiate NSXMLDocument with invalid data"];
}
[self _setNode:doc];
}
return self;
}
- (BOOL) isStandalone
{
return internal->standalone;
return (MY_DOC->standalone == 1);
}
- (NSString*) MIMEType
@ -197,12 +228,13 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
- (NSXMLElement*) rootElement
{
return internal->rootElement;
xmlNodePtr rootElem = xmlDocGetRootElement(MY_DOC);
return (NSXMLElement *)[NSXMLNode _objectForNode:rootElem];
}
- (void) setCharacterEncoding: (NSString*)encoding
{
ASSIGNCOPY(internal->encoding, encoding);
MY_DOC->encoding = XMLSTRING(encoding);
}
- (void) setDocumentContentKind: (NSXMLDocumentContentKind)kind
@ -212,7 +244,9 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
- (void) setDTD: (NSXMLDTD*)documentTypeDeclaration
{
NSAssert(documentTypeDeclaration != nil, NSInvalidArgumentException);
ASSIGNCOPY(internal->docType, documentTypeDeclaration);
MY_DOC->extSubset = [documentTypeDeclaration _node];
}
- (void) setMIMEType: (NSString*)MIMEType
@ -222,31 +256,31 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
- (void) setRootElement: (NSXMLNode*)root
{
if (nil != root)
{
NSArray *children;
id oldElement = [self rootElement];
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;
if(root == nil)
{
return;
}
xmlDocSetRootElement(MY_DOC,[root _node]);
// Do our subNode housekeeping...
[self _removeSubNode:oldElement];
[self _addSubNode:root];
}
- (void) setStandalone: (BOOL)standalone
{
internal->standalone = standalone;
MY_DOC->standalone = standalone;
}
- (void) setVersion: (NSString*)version
{
if ([version isEqualToString: @"1.0"] || [version isEqualToString: @"1.1"])
{
ASSIGNCOPY(internal->version, version);
}
MY_DOC->version = XMLStringCopy(version);
}
else
{
[NSException raise: NSInvalidArgumentException
@ -256,15 +290,20 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
- (NSString*) version
{
return internal->version;
if (MY_DOC->version)
return StringFromXMLStringPtr(MY_DOC->version);
else
return @"1.0";
}
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
{
NSXMLNodeKind kind;
NSXMLNodeKind kind = [child kind];
NSUInteger childCount = [self childCount];
// Check to make sure this is a valid addition...
NSAssert(nil != child, NSInvalidArgumentException);
NSAssert(index <= internal->childCount, NSInvalidArgumentException);
NSAssert(index <= childCount, NSInvalidArgumentException);
NSAssert(nil == [child parent], NSInvalidArgumentException);
kind = [child kind];
NSAssert(NSXMLAttributeKind != kind, NSInvalidArgumentException);
@ -276,14 +315,7 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
NSAssert(NSXMLNamespaceKind != kind, NSInvalidArgumentException);
NSAssert(NSXMLNotationDeclarationKind != kind, NSInvalidArgumentException);
if (nil == internal->children)
{
internal->children = [[NSMutableArray alloc] initWithCapacity: 10];
}
[internal->children insertObject: child
atIndex: index];
GSIVar(child, parent) = self;
internal->childCount++;
[self _insertChild:child atIndex:index];
}
- (void) insertChildren: (NSArray*)children atIndex: (NSUInteger)index
@ -299,50 +331,39 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
- (void) removeChildAtIndex: (NSUInteger)index
{
NSXMLNode *child = [internal->children objectAtIndex: index];
NSXMLNode *child;
xmlNodePtr n;
if (nil != child)
if (index >= [self childCount])
{
if (internal->rootElement == child)
{
internal->rootElement = nil;
}
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 too large"];
}
child = [[self children] objectAtIndex: index];
n = [child _node];
xmlUnlinkNode(n);
}
- (void) setChildren: (NSArray*)children
{
if (children != internal->children)
NSEnumerator *en;
NSXMLNode *child;
while ([self childCount] > 0)
{
NSEnumerator *en;
NSXMLNode *child;
[children retain];
while (internal->childCount > 0)
{
[self removeChildAtIndex:internal->childCount - 1];
}
en = [children objectEnumerator];
while ((child = [en nextObject]) != nil)
{
[self insertChild: child atIndex: internal->childCount];
}
[children release];
[self removeChildAtIndex: [self childCount] - 1];
}
en = [[self children] objectEnumerator];
while ((child = [en nextObject]) != nil)
{
[self insertChild: child atIndex: [self childCount]];
}
}
- (void) addChild: (NSXMLNode*)child
{
[self insertChild: child atIndex: internal->childCount];
[self insertChild: child atIndex: [self childCount]];
}
- (void) replaceChildAtIndex: (NSUInteger)index withNode: (NSXMLNode*)node
@ -366,37 +387,16 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
- (NSString *) XMLStringWithOptions: (NSUInteger)options
{
NSMutableString *string = [NSMutableString string];
NSEnumerator *en = [internal->children objectEnumerator];
id obj = nil;
NSString *string = nil;
xmlChar *buf = NULL;
int length;
if (internal->version || internal->standalone || internal->encoding)
xmlDocDumpFormatMemoryEnc(MY_DOC, &buf, &length, "utf-8", 1);
if (buf != 0 && length > 0)
{
NSString *version = internal->version;
if (!version)
version = @"1.0";
[string appendString: @"<?xml version=\""];
[string appendString: version];
[string appendString: @"\""];
if (internal->encoding)
{
[string appendString: @" encoding=\""];
[string appendString: internal->encoding];
[string appendString: @"\""];
}
if (YES == internal->standalone)
{
[string appendString: @" standalone=\"yes\""];
}
else
{
[string appendString: @" standalone=\"no\""];
}
[string appendString: @"?>\n"];
}
while ((obj = [en nextObject]) != nil)
{
[string appendString: [obj XMLStringWithOptions: options]];
string = StringFromXMLString(buf, length);
free(buf);
}
return string;
}
@ -405,100 +405,101 @@ GS_PRIVATE_INTERNAL(NSXMLDocument)
arguments: (NSDictionary*)arguments
error: (NSError**)error
{
[self notImplemented: _cmd];
#ifdef HAVE_LIBXSLT
xmlChar *data = (xmlChar *)[xslt bytes];
xmlChar **params = NULL;
xmlDocPtr stylesheetDoc = xmlReadDoc(data, NULL, NULL, XML_PARSE_NOERROR);
xsltStylesheetPtr stylesheet = xsltParseStylesheetDoc(stylesheetDoc);
xmlDocPtr resultDoc = NULL;
NSEnumerator *en = [arguments keyEnumerator];
NSString *key = nil;
NSUInteger index = 0;
// Iterate over the keys and put them into params...
if(arguments != nil)
{
int count = [[arguments allKeys] count];
*params = NSZoneCalloc([self zone],((count + 1) * 2),sizeof(xmlChar *));
while((key = [en nextObject]) != nil)
{
params[index] = (xmlChar *)XMLSTRING(key);
params[index+1] = (xmlChar *)XMLSTRING([arguments objectForKey: key]);
index+=2;
}
}
// Apply the stylesheet and get the result...
resultDoc = xsltApplyStylesheet(stylesheet,MY_DOC,(const char **)params);
// Cleanup...
xsltFreeStylesheet(stylesheet);
xmlFreeDoc(stylesheetDoc);
xsltCleanupGlobals();
xmlCleanupParser();
NSZoneFree([self zone], params);
return [NSXMLNode _objectForNode: (xmlNodePtr)resultDoc];
#else
return nil;
#endif
}
- (id) objectByApplyingXSLTString: (NSString*)xslt
arguments: (NSDictionary*)arguments
error: (NSError**)error
{
[self notImplemented: _cmd];
#ifdef HAVE_LIBXSLT
NSData *data = [[NSData alloc] initWithBytes: [xslt UTF8String]
length: [xslt length]];
NSXMLDocument *result = [self objectByApplyingXSLT: data
arguments: arguments
error: error];
[data release];
return result;
#else
return nil;
#endif
}
- (id) objectByApplyingXSLTAtURL: (NSURL*)xsltURL
arguments: (NSDictionary*)argument
arguments: (NSDictionary*)arguments
error: (NSError**)error
{
[self notImplemented: _cmd];
#ifdef HAVE_LIBXSLT
NSData *data = [NSData dataWithContentsOfURL: xsltURL];
NSXMLDocument *result = [self objectByApplyingXSLT: data
arguments: arguments
error: error];
return result;
#else
return nil;
#endif
}
- (BOOL) validateAndReturnError: (NSError**)error
{
[self notImplemented: _cmd];
return NO;
xmlValidCtxtPtr ctxt = xmlNewValidCtxt();
BOOL result = (BOOL)(xmlValidateDocument(ctxt, MY_DOC));
return result;
}
- (id) copyWithZone: (NSZone *)zone
{
NSXMLDocument *c = (NSXMLDocument*)[super copyWithZone: zone];
NSXMLElement *r = [self rootElement];
NSEnumerator *en;
id obj;
[c setStandalone: internal->standalone];
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];
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;
return c;
}
@end
@implementation NSXMLDocument (NSXMLParserDelegate)
- (void) parser: (NSXMLParser *)parser
didStartElement: (NSString *)elementName
namespaceURI: (NSString *)namespaceURI
qualifiedName: (NSString *)qualifiedName
attributes: (NSDictionary *)attributeDict
- (BOOL) isEqual: (id)other
{
NSXMLElement *lastElement = [internal->elementStack lastObject];
NSXMLElement *currentElement =
[[NSXMLElement alloc] initWithName: elementName];
[lastElement addChild: currentElement];
[internal->elementStack addObject: currentElement];
[currentElement release];
if (nil == internal->rootElement)
if(self == other)
{
[self setRootElement: currentElement];
return YES;
}
[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];
return [[self rootElement] isEqual: [other rootElement]];
}
@end
#endif

View file

@ -10,7 +10,7 @@
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
@ -29,56 +29,78 @@
#import "GSInternal.h"
GS_PRIVATE_INTERNAL(NSXMLElement)
#if defined(HAVE_LIBXML)
extern void clearPrivatePointers(xmlNodePtr aNode);
// Private methods to manage libxml pointers...
@interface NSXMLNode (Private)
- (void *) _node;
- (void) _setNode: (void *)_anode;
+ (NSXMLNode *) _objectForNode: (xmlNodePtr)node;
- (void) _addSubNode:(NSXMLNode *)subNode;
- (void) _removeSubNode:(NSXMLNode *)subNode;
- (id) _initWithNode:(xmlNodePtr)node kind:(NSXMLNodeKind)kind;
- (void) _insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index;
- (void) _updateExternalRetains;
- (void) _invalidate;
@end
@implementation NSXMLElement
- (void) dealloc
{
if (GS_EXISTS_INTERNAL)
/*
if (GS_EXISTS_INTERNAL && _internal != nil)
{
while (internal->childCount > 0)
while ([self childCount] > 0)
{
[self removeChildAtIndex: internal->childCount - 1];
[self removeChildAtIndex: [self childCount] - 1];
}
[internal->attributes release];
[internal->namespaces release];
}
*/
[super dealloc];
}
- (void) _createInternal
{
GS_CREATE_INTERNAL(NSXMLElement);
}
- (id) init
{
return [self initWithKind: NSXMLElementKind options: 0];
}
- (id) initWithKind: (NSXMLNodeKind)kind options: (NSUInteger)theOptions
{
if (NSXMLElementKind == kind)
{
if ((self = [super initWithKind:kind options:theOptions]))
{
internal->objectValue = @"";
}
return self;
}
else
{
[self release];
return [[NSXMLNode alloc] initWithKind: kind
options: theOptions];
}
}
- (id) initWithName: (NSString*)name
{
return [self initWithName: name URI: nil];
}
- (id) initWithKind: (NSXMLNodeKind)kind options: (NSUInteger)theOptions
{
if (NSXMLElementKind == kind)
{
/* Create holder for internal instance variables so that we'll have
* all our ivars available rather than just those of the superclass.
*/
GS_CREATE_INTERNAL(NSXMLElement)
}
return [super initWithKind: kind options: theOptions];
}
- (id) initWithName: (NSString*)name URI: (NSString*)URI
{
/* Create holder for internal instance variables so that we'll have
* all our ivars available rather than just those of the superclass.
*/
GS_CREATE_INTERNAL(NSXMLElement)
if ((self = [super initWithKind: NSXMLElementKind]) != nil)
{
ASSIGNCOPY(internal->name, name);
[self setName:name];
ASSIGNCOPY(internal->URI, URI);
internal->attributes = [[NSMutableDictionary alloc] initWithCapacity: 10];
internal->namespaces = [[NSMutableArray alloc] initWithCapacity: 10];
internal->objectValue = @"";
}
return self;
@ -93,16 +115,42 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
return nil;
}
- (id) initWithXMLString: (NSString*)string error: (NSError**)error
- (id) initWithXMLString: (NSString*)string
error: (NSError**)error
{
[self notImplemented: _cmd];
return nil;
NSXMLElement *result = nil;
if((self = [super init]) != nil)
{
NSXMLDocument *tempDoc =
[[NSXMLDocument alloc] initWithXMLString:string
options:0
error:error];
if(tempDoc != nil)
{
result = RETAIN([tempDoc rootElement]);
[result detach]; // detach from document.
}
[tempDoc release];
}
return result;
}
- (NSArray*) elementsForName: (NSString*)name
{
[self notImplemented: _cmd];
return nil;
NSMutableArray *results = [NSMutableArray arrayWithCapacity: 10];
xmlNodePtr cur = NULL;
for (cur = MY_NODE->children; cur != NULL; cur = cur->next)
{
NSString *n = StringFromXMLStringPtr(cur->name);
if([n isEqualToString: name])
{
NSXMLNode *node = (NSXMLNode *)(cur->_private);
[results addObject: node];
}
}
return results;
}
- (NSArray*) elementsForLocalName: (NSString*)localName URI: (NSString*)URI
@ -113,13 +161,69 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
- (void) addAttribute: (NSXMLNode*)attribute
{
[internal->attributes setObject: attribute
forKey: [attribute name]];
xmlNodePtr node = (xmlNodePtr)(internal->node);
xmlAttrPtr attr = (xmlAttrPtr)[attribute _node];
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];
}
- (void) removeAttributeForName: (NSString*)name
{
[internal->attributes removeObjectForKey: name];
xmlNodePtr node = (xmlNodePtr)(internal->node);
xmlAttrPtr attr = xmlHasProp(node, (xmlChar *)[name UTF8String]);
xmlAttrPtr newAttr = NULL;
NSXMLNode *attrNode = nil;
if (NULL == attr)
{
return;
}
// We need a copy of the node because xmlRemoveProp() frees attr:
newAttr = xmlCopyProp(NULL, attr);
attrNode = [NSXMLNode _objectForNode: (xmlNodePtr)attr];
// This is supposed to return failure for DTD defined attributes
if (0 == xmlRemoveProp(attr))
{
[attrNode _setNode: newAttr];
[self _removeSubNode: attrNode];
}
else
{
// In this case we throw away our copy again.
xmlFreeProp(newAttr);
}
}
- (void) setAttributes: (NSArray*)attributes
@ -127,7 +231,6 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
NSEnumerator *enumerator = [attributes objectEnumerator];
NSXMLNode *attribute;
[internal->attributes removeAllObjects];
while ((attribute = [enumerator nextObject]) != nil)
{
[self addAttribute: attribute];
@ -141,27 +244,45 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
- (void) setAttributesWithDictionary: (NSDictionary*)attributes
{
NSEnumerator *en = [attributes keyEnumerator];
NSString *key;
[internal->attributes removeAllObjects];
while ((key = [en nextObject]) != nil)
{
NSString *val = [attributes objectForKey: key];
NSXMLNode *attribute = [NSXMLNode attributeWithName: key
NSEnumerator *en = [attributes keyEnumerator];
NSString *key;
// [internal->attributes removeAllObjects];
while ((key = [en nextObject]) != nil)
{
NSString *val = [[attributes objectForKey: key] stringValue];
NSXMLNode *attribute = [NSXMLNode attributeWithName: key
stringValue: val];
[self addAttribute: attribute];
[self addAttribute: attribute];
}
}
- (NSArray*) attributes
{
return [internal->attributes allValues];
NSMutableArray *attributes = [NSMutableArray array];
xmlNodePtr node = MY_NODE;
struct _xmlAttr * attributeNode = node->properties;
while (attributeNode)
{
NSXMLNode *attribute = [NSXMLNode _objectForNode:(xmlNodePtr)attributeNode];
[attributes addObject:attribute];
attributeNode = attributeNode->next;
}
return attributes;
}
- (NSXMLNode*) attributeForName: (NSString*)name
{
return [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];
}
free(xmlName); // Free the name string since it's no longer needed.
xmlName = NULL;
return result; // [internal->attributes objectForKey: name];
}
- (NSXMLNode*) attributeForLocalName: (NSString*)localName
@ -173,7 +294,7 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
- (void) addNamespace: (NSXMLNode*)aNamespace
{
[internal->namespaces addObject: aNamespace];
[self notImplemented: _cmd];
}
- (void) removeNamespaceForPrefix: (NSString*)name
@ -183,21 +304,43 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
- (void) setNamespaces: (NSArray*)namespaces
{
ASSIGNCOPY(internal->namespaces, namespaces);
}
NSEnumerator *en = [namespaces objectEnumerator];
NSString *namespace = nil;
xmlNsPtr cur = NULL;
- (void) setObjectValue: (id)value
{
if (nil == value)
while((namespace = (NSString *)[en nextObject]) != nil)
{
value = @""; // May not be nil
xmlNsPtr ns = xmlNewNs([self _node], NULL, XMLSTRING(namespace));
if(MY_NODE->ns == NULL)
{
MY_NODE->ns = ns;
cur = ns;
}
else
{
cur->next = ns;
cur = ns;
}
}
ASSIGN(internal->objectValue, value);
}
- (NSArray*) namespaces
{
return internal->namespaces;
NSMutableArray *result = nil;
xmlNsPtr ns = MY_NODE->ns;
if(ns)
{
xmlNsPtr cur = NULL;
result = [NSMutableArray array];
for(cur = ns; cur != NULL; cur = cur->next)
{
[result addObject: StringFromXMLStringPtr(cur->prefix)];
}
}
// [self notImplemented: _cmd];
return result; // nil; // internal->namespaces;
}
- (NSXMLNode*) namespaceForPrefix: (NSString*)name
@ -220,13 +363,13 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
{
NSXMLNodeKind kind;
NSXMLNodeKind kind = [child kind];
NSUInteger childCount = [self childCount];
// Check to make sure this is a valid addition...
NSAssert(nil != child, NSInvalidArgumentException);
NSAssert(index <= internal->childCount, NSInvalidArgumentException);
NSAssert(nil == [child parent], NSInternalInconsistencyException);
kind = [child kind];
// FIXME ... should we check for valid kinds rather than invalid ones?
NSAssert(index <= childCount, NSInvalidArgumentException);
NSAssert(nil == [child parent], NSInvalidArgumentException);
NSAssert(NSXMLAttributeKind != kind, NSInvalidArgumentException);
NSAssert(NSXMLDTDKind != kind, NSInvalidArgumentException);
NSAssert(NSXMLDocumentKind != kind, NSInvalidArgumentException);
@ -236,21 +379,14 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
NSAssert(NSXMLNamespaceKind != kind, NSInvalidArgumentException);
NSAssert(NSXMLNotationDeclarationKind != kind, NSInvalidArgumentException);
if (nil == internal->children)
{
internal->children = [[NSMutableArray alloc] initWithCapacity: 10];
}
[internal->children insertObject: child
atIndex: index];
GSIVar(child, parent) = self;
internal->childCount++;
[self _insertChild:child atIndex:index];
}
- (void) insertChildren: (NSArray*)children atIndex: (NSUInteger)index
{
NSEnumerator *enumerator = [children objectEnumerator];
NSXMLNode *child;
while ((child = [enumerator nextObject]) != nil)
{
[self insertChild: child atIndex: index++];
@ -260,132 +396,121 @@ GS_PRIVATE_INTERNAL(NSXMLElement)
- (void) removeChildAtIndex: (NSUInteger)index
{
NSXMLNode *child;
xmlNodePtr n;
if (index >= internal->childCount)
if (index >= [self childCount])
{
[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);
format: @"index too large"];
}
child = [[self children] objectAtIndex: index];
n = [child _node];
xmlUnlinkNode(n);
[self _removeSubNode:child];
}
- (void) setChildren: (NSArray*)children
{
if (children != internal->children)
{
NSEnumerator *en;
NSXMLNode *child;
NSEnumerator *en;
NSXMLNode *child;
[children retain];
while (internal->childCount > 0)
{
[self removeChildAtIndex:internal->childCount - 1];
}
en = [children objectEnumerator];
while ((child = [en nextObject]) != nil)
{
[self insertChild: child atIndex: internal->childCount];
}
[children release];
while ([self childCount] > 0)
{
[self removeChildAtIndex: [self childCount] - 1];
}
en = [[self children] objectEnumerator];
while ((child = [en nextObject]) != nil)
{
[self insertChild: child atIndex: [self childCount]];
}
}
- (void) addChild: (NSXMLNode*)child
{
[self insertChild: child atIndex: internal->childCount];
int count = [self childCount];
[self insertChild: child atIndex: count];
}
- (void) replaceChildAtIndex: (NSUInteger)index withNode: (NSXMLNode*)node
{
[self insertChild: node atIndex: index];
[self removeChildAtIndex: index + 1];
}
- (void) normalizeAdjacentTextNodesPreservingCDATA: (BOOL)preserve
static void joinTextNodes(xmlNodePtr nodeA, xmlNodePtr nodeB, NSMutableArray *nodesToDelete)
{
[self notImplemented: _cmd];
NSXMLNode *objA = (nodeA->_private), *objB = (nodeB->_private);
xmlTextMerge(nodeA, nodeB); // merge nodeB into nodeA
if (objA != nil) // objA gets the merged node
{
if (objB != nil) // objB is now invalid
{
[objB _invalidate]; // set it to be invalid and make sure it's not pointing to a freed node
[nodesToDelete addObject:objB];
}
}
else if (objB != nil) // there is no objA -- objB gets the merged node
{
[objB _setNode:nodeA]; // nodeA is the remaining (merged) node
}
}
- (NSString *) XMLStringWithOptions: (NSUInteger)options
- (void) normalizeAdjacentTextNodesPreservingCDATA: (BOOL)preserve
{
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)
NSEnumerator *subEnum = [internal->subNodes objectEnumerator];
NSXMLNode *subNode = nil;
NSMutableArray *nodesToDelete = [NSMutableArray array];
while ((subNode = [subEnum nextObject]))
{
[result appendString: @" "];
[result appendString: [object XMLStringWithOptions: options]];
xmlNodePtr node = [subNode _node];
xmlNodePtr prev = node->prev;
xmlNodePtr next = node->next;
if (node->type == XML_ELEMENT_NODE)
[(NSXMLElement *)subNode normalizeAdjacentTextNodesPreservingCDATA:preserve];
else if (node->type == XML_TEXT_NODE || (node->type == XML_CDATA_SECTION_NODE && !preserve))
{
if (next && (next->type == XML_TEXT_NODE
|| (next->type == XML_CDATA_SECTION_NODE && !preserve)))
{
//combine node & node->next
joinTextNodes(node, node->next, nodesToDelete);
}
if (prev && (prev->type == XML_TEXT_NODE
|| (prev->type == XML_CDATA_SECTION_NODE && !preserve)))
{
//combine node->prev & node
// join the text of both nodes
// assign the joined text to the earlier of the two nodes that has an ObjC object
// unlink the other node
// delete the other node's object (maybe add it to a list of nodes to delete when we're done? -- or just set its node to null, and then remove it from our subNodes when we're done iterating it) (or maybe we need to turn it into an NSInvalidNode too??)
joinTextNodes(node->prev, node, nodesToDelete);
}
}
}
// 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)
if ([nodesToDelete count] > 0)
{
[result appendString: @" "];
[result appendString: [object XMLStringWithOptions: options]];
subEnum = [nodesToDelete objectEnumerator];
while ((subNode = [subEnum nextObject]))
{
[self _removeSubNode:subNode];
}
[self _updateExternalRetains];
}
// Close the entire tag...
[result appendString: [NSString stringWithFormat: @"</%@>",[self name]]];
// return
return result;
}
- (id) copyWithZone: (NSZone *)zone
{
NSXMLElement *c = (NSXMLElement*)[super copyWithZone: zone];
NSEnumerator *en;
id obj;
en = [internal->namespaces objectEnumerator];
while ((obj = [en nextObject]) != nil)
{
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];
}
en = [internal->children objectEnumerator];
while ((obj = [en nextObject]) != nil)
{
NSXMLNode *child = [obj copyWithZone: zone];
[c addChild: child];
[child release];
}
NSXMLElement *c = [[self class] alloc]; ///(NSXMLElement*)[super copyWithZone: zone];
xmlNodePtr newNode = (xmlNodePtr)xmlCopyNode(MY_NODE, 1); // copy recursively
clearPrivatePointers(newNode); // clear out all of the _private pointers in the entire tree
c = [c _initWithNode:newNode kind:internal->kind];
return c;
}
@end
#endif

File diff suppressed because it is too large Load diff

View file

@ -34,6 +34,58 @@
#define EXPOSE_NSXMLElement_IVARS 1
#define EXPOSE_NSXMLNode_IVARS 1
/*
* Macro to cast string to correct type for libxml2
*/
#define XMLSTRING(X) ((const unsigned char*)[X UTF8String])
inline static unsigned char *XMLStringCopy(NSString *source)
{
char *xmlstr;
unsigned int len = [source maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
if (len == 0)
return NULL;
xmlstr = malloc(len);
[source getCString:xmlstr maxLength:len encoding:NSUTF8StringEncoding];
return (unsigned char *)xmlstr;
}
inline static NSString*
StringFromXMLStringPtr(const unsigned char *bytes)
{
NSString *str;
unsigned int length;
if (bytes == NULL)
return @"";
length = strlen((char *)bytes);
str = [[NSString alloc] initWithBytes: bytes
length: length
encoding: NSUTF8StringEncoding];
return AUTORELEASE(str);
}
inline static NSString*
StringFromXMLString(const unsigned char *bytes, unsigned length)
{
NSString *str;
if (bytes == NULL)
return @"";
str = [[NSString alloc] initWithBytes: bytes
length: length
encoding: NSUTF8StringEncoding];
return AUTORELEASE(str);
}
#define MY_DOC ((xmlDoc *)internal->node)
#define MY_NODE ((xmlNode *)internal->node)
#define MY_ATTR ((xmlAttr *)internal->node)
#define MY_ELEM ((xmlElement *)internal->node)
#define MY_DTD ((xmlDtd *)internal->node)
/* Instance variables for NSXMLNode. This macro needs to be defined before
* the NSXMLNode.h header is imported and before GSInternal.h is imported.
*
@ -49,18 +101,17 @@
* nodes are changed.
*/
#define GS_NSXMLNode_IVARS \
void *handle; \
NSUInteger kind; \
NSXMLNode *parent; \
id objectValue; \
NSString *stringValue; \
NSString *name; \
NSString *URI; \
NSMutableArray *children; \
NSUInteger childCount; \
NSXMLNode *previousSibling; \
NSXMLNode *nextSibling; \
NSUInteger options; \
NSUInteger kind; \
NSXMLNode *parent; \
id objectValue; \
NSString *URI; \
NSXMLNode *previousSibling; \
NSXMLNode *nextSibling;\
NSUInteger options; \
void *node; \
NSMutableArray *subNodes; \
int externalRetains; \
int retainedSelf; \
/* When using the non-fragile ABI, the instance variables are exposed to the
@ -81,17 +132,9 @@
* is imported and before GSInternal.h is imported.
*/
#define GS_NSXMLDocument_IVARS SUPERIVARS(GS_NSXMLNode_IVARS) \
NSString *encoding; \
NSString *version; \
NSXMLDTD *docType; \
BOOL childrenHaveMutated; \
BOOL standalone; \
NSXMLElement *rootElement; \
NSString *MIMEType; \
NSUInteger fidelityMask; \
NSInteger contentKind; \
NSMutableArray *elementStack; \
NSData *xmlData; \
/* Instance variables for NSXMLDTD with/without the instance
@ -102,8 +145,6 @@
#define GS_NSXMLDTD_IVARS SUPERIVARS(GS_NSXMLNode_IVARS) \
NSString *publicID; \
NSString *systemID; \
BOOL childrenHaveMutated; \
BOOL modified; \
NSMutableDictionary *entities; \
NSMutableDictionary *elements; \
NSMutableDictionary *notations; \
@ -118,22 +159,13 @@
*/
#define GS_NSXMLDTDNode_IVARS SUPERIVARS(GS_NSXMLNode_IVARS) \
NSUInteger DTDKind; \
NSString *notationName; \
NSString *publicID; \
NSString *systemID; \
/* Instance variables for NSXMLElement with/without the instance
* variable 'inherited' from NSXMLNode.
* This macro needs to be defined before the NSXMLElement.h header
* is imported and before GSInternal.h is imported.
*/
#define GS_NSXMLElement_IVARS SUPERIVARS(GS_NSXMLNode_IVARS) \
NSMutableDictionary *attributes; \
NSMutableArray *namespaces; \
BOOL childrenHaveMutated; \
NSInteger prefixIndex; \
#define GS_NSXMLElement_IVARS SUPERIVARS(GS_NSXMLNode_IVARS)
#import "Foundation/NSArray.h"
#import "Foundation/NSData.h"

View file

@ -6,8 +6,42 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSArray *nodes = nil;
NSXMLDocument *node;
NSXMLDocument *node2;
NSXMLElement *elem;
NSString *documentXML =
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
@"<bookstore>"
@" <book category=\"COOKING\">"
@" <title lang=\"en\">Everyday Italian</title>"
@" <author>Giada De Laurentiis</author>"
@" <year>2005</year>"
@" <price>30.00</price>"
@" </book>"
@" <book category=\"CHILDREN\">"
@" <title lang=\"en\">Harry Potter</title>"
@" <author>J K. Rowling</author>"
@" <year>2005</year>"
@" <price>29.99</price>"
@" </book>"
@" <book category=\"WEB\">"
@" <title lang=\"en\">XQuery Kick Start</title>"
@" <author>James McGovern</author>"
@" <author>Per Bothner</author>"
@" <author>Kurt Cagle</author>"
@" <author>James Linn</author>"
@" <author>Vaidyanathan Nagarajan</author>"
@" <year>2003</year>"
@" <price>49.99</price>"
@" </book>"
@" <book category=\"WEB\">"
@" <title lang=\"en\">Learning XML</title>"
@" <author>Erik T. Ray</author>"
@" <year>2003</year>"
@" <price>39.95</price>"
@" </book>"
@"</bookstore>";
node = [NSXMLDocument alloc];
PASS_EXCEPTION([node initWithData: nil options: 0 error: 0],
@ -35,10 +69,33 @@ int main()
PASS_RUNS([node setRootElement: nil], "setting a nil root is ignored");
PASS_EQUAL([node rootElement], elem, "root element remains");
node = [[NSXMLDocument alloc] initWithXMLString:documentXML
options:0
error:NULL];
elem = [node rootElement];
PASS(node != nil, "document was initialized from a string");
PASS_EQUAL([node rootElement], elem, "root element is correct");
PASS_EQUAL([elem name],@"bookstore", "root element is bookstore");
nodes = [node nodesForXPath:@"/bookstore/book" error:NULL];
PASS([nodes count] == 4,
"Xpath function returns the correct number of elements (4)");
elem = [nodes objectAtIndex: 0];
PASS_EQUAL([elem class],[NSXMLElement class],
"first node in Xpath result is an element");
PASS([[elem name] isEqualToString: @"book"],
"Got the correct elements from XPath query");
node2 = [[NSXMLDocument alloc] initWithXMLString:documentXML
options:0
error:NULL];
PASS([node isEqual: node2],
"Equal documents are equivalent");
[arp release];
arp = nil;
[elem release];
[node release];
// [elem release];
// [node release];
return 0;
}

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;
}

View file

@ -67,10 +67,12 @@ int main()
PASS_RUNS([root1 insertChild: c atIndex: 0],
"may add NSXMLCommentKind child");
/* Removed based on test run on Cocoa.
c = [[[NSXMLNode alloc] initWithKind:
NSXMLAttributeDeclarationKind] autorelease];
PASS_RUNS([root1 insertChild: c atIndex: 0],
"may add NSXMLAttributeDeclarationKind child");
*/
c = [[[NSXMLNode alloc] initWithKind:
NSXMLEntityDeclarationKind] autorelease];

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");
@ -37,7 +38,8 @@ int main()
PASS_EQUAL([node objectValue], @"anObject",
"setting object value on invalid node works");
[node setObjectValue: nil];
PASS_EQUAL([node objectValue], nil,
// Per documentation on NSXMLNode setObjectValue/objectValue,
PASS_EQUAL([node objectValue], @"",
"setting nil object value on invalid node works");
[node setStringValue: @"aString"];
PASS_EQUAL([node stringValue], @"aString",
@ -49,6 +51,37 @@ 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");
[other setStringValue: @"value"];
PASS(![node isEqual: other],
"Nodes with different values are NOT equal");
[node setStringValue: @"value"];
PASS([node isEqual: other],
"Nodes with different values are equal");
[node release];
[other release];
[arp release];
arp = nil;

View file

@ -8,7 +8,6 @@
int main()
{
START_SET("NSXMLNode - handling children")
// testHopeful = YES;
NS_DURING
{
NSXMLElement *node = [[NSXMLElement alloc] initWithKind: NSXMLElementKind];

View file

@ -18,7 +18,6 @@
int main()
{
START_SET("NSXMLNode -initWithKind: initializer")
// testHopeful = YES;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSXMLNode *node;
NS_DURING