First cut at NSXMLDocument changes.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34371 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Gregory John Casamento 2011-12-30 21:40:12 +00:00
parent 0babe2697b
commit 7b7b2a6f9d
10 changed files with 181 additions and 40 deletions

View file

@ -1,3 +1,16 @@
2011-12-30 14:22-EST Gregory John Casamento <greg.casamento@gmail.com>
* Headers/Foundation/NSXMLDocument.h
* Headers/Foundation/NSXMLElement.h
* Headers/Foundation/NSXMLNode.h
* Source/NSXMLDocument.m
* Source/NSXMLElement.m
* Source/NSXMLNode.m
* Source/NSXMLPrivate.h
* Tests/base/NSXMLNode/children.m
* Tests/base/NSXMLNode/kinds.m: First cut at NSXMLDocument
implementation.
2011-12-29 Doug Simons <doug.simons@testplant.com>
* Source/NSCalendarDate.m: Fix -addTimeInterval: to copy calendarFormat

View file

@ -34,6 +34,7 @@ extern "C" {
@class NSData;
@class NSXMLDTD;
@class NSMutableArray;
/*
* Input options
@ -78,7 +79,7 @@ typedef NSUInteger NSXMLDocumentContentKind;
NSString *_encoding;
NSString *_version;
NSXMLDTD *_docType;
NSArray *_children;
NSArray *_children;
BOOL _childrenHaveMutated;
BOOL _standalone;
NSXMLElement *_rootElement;
@ -86,6 +87,10 @@ typedef NSUInteger NSXMLDocumentContentKind;
NSString *_MIMEType;
NSUInteger _fidelityMask;
NSXMLDocumentContentKind _contentKind;
@private
NSMutableArray *_elementStack;
NSData *_xmlData;
#endif
}

View file

@ -42,13 +42,14 @@ extern "C" {
{
#if GS_EXPOSE(NSXMLElement)
@protected
NSString *_name;
// NSString *_name;
NSMutableArray *_attributes;
NSMutableArray *_namespaces;
NSArray *_children;
NSMutableArray *_children;
BOOL _childrenHaveMutated;
NSString *_URI;
NSInteger _prefixIndex;
NSString *_value;
#endif
}

View file

@ -87,6 +87,7 @@ typedef NSUInteger NSXMLNodeKind;
NSXMLNode *_parent;
NSUInteger _index;
id _objectValue;
NSString *_name;
#endif
#if GS_NONFRAGILE
# if defined(GS_NSXMLNode_IVARS)

View file

@ -25,6 +25,11 @@
#import "common.h"
#import "NSXMLPrivate.h"
#import <Foundation/NSXMLParser.h>
// Forward declaration of interface for NSXMLParserDelegate
@interface NSXMLDocument (NSXMLParserDelegate)
@end
@implementation NSXMLDocument
@ -35,12 +40,14 @@
- (void) dealloc
{
[_encoding release];
[_version release];
[_docType release];
[_children release];
[_URI release];
[_MIMEType release];
RELEASE(_encoding);
RELEASE(_version);
RELEASE(_docType);
RELEASE(_children);
RELEASE(_URI);
RELEASE(_MIMEType);
RELEASE(_elementStack);
RELEASE(_xmlData);
[super dealloc];
}
@ -68,7 +75,7 @@
data = [NSData dataWithContentsOfURL: url];
doc = [self initWithData: data options: 0 error: 0];
[doc setURI: [url absoluteString]];
[doc setURI: [url absoluteString]];
return doc;
}
@ -77,15 +84,36 @@
options: (NSUInteger)mask
error: (NSError**)error
{
[self notImplemented: _cmd];
_children = [NSMutableArray new];
return nil;
if((self = [super init]) != nil)
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData: data];
if(parser != nil)
{
_standalone = YES;
_children = [[NSMutableArray alloc] initWithCapacity: 10];
_elementStack = [[NSMutableArray alloc] initWithCapacity: 10];
ASSIGN(_xmlData, data);
[parser setDelegate: self];
[parser parse];
}
}
return self;
}
- (id) initWithRootElement: (NSXMLElement*)element
{
if([_children containsObject: element] || [element parent] != nil)
{
[NSException raise: NSInternalInconsistencyException
format: @"%@ cannot be used as root of %@",
element,
self];
}
self = [self initWithData: nil options: 0 error: 0];
[self setRootElement: (NSXMLNode*)element];
if(self != nil)
{
[self setRootElement: (NSXMLNode*)element];
}
return self;
}
@ -93,8 +121,12 @@
options: (NSUInteger)mask
error: (NSError**)error
{
[self notImplemented: _cmd];
return nil;
NSData *data = [NSData dataWithBytes: [string UTF8String]
length: [string length]];
self = [self initWithData: data
options: mask
error: error];
return self;
}
- (BOOL) isStandalone
@ -164,7 +196,9 @@
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
{
[self notImplemented: _cmd];
[child setParent: self];
[(NSMutableArray *)_children insertObject: child atIndex: index];
_childrenHaveMutated = YES;
}
- (void) insertChildren: (NSArray*)children atIndex: (NSUInteger)index
@ -180,7 +214,8 @@
- (void) removeChildAtIndex: (NSUInteger)index
{
[self notImplemented: _cmd];
[(NSMutableArray *)_children removeObjectAtIndex: index];
_childrenHaveMutated = YES;
}
- (void) setChildren: (NSArray*)children
@ -212,8 +247,8 @@
- (NSData*) XMLDataWithOptions: (NSUInteger)options
{
[self notImplemented: _cmd];
return nil;
// TODO: Apply options to data.
return _xmlData;
}
- (id) objectByApplyingXSLT: (NSData*)xslt
@ -248,3 +283,47 @@
@end
@implementation NSXMLDocument (NSXMLParserDelegate)
- (void) parser: (NSXMLParser *)parser
didStartElement: (NSString *)elementName
namespaceURI: (NSString *)namespaceURI
qualifiedName: (NSString *)qualifiedName
attributes: (NSDictionary *)attributeDict
{
NSXMLElement *currentElement =
[[NSXMLElement alloc] initWithName: elementName];
[_elementStack insertObject: currentElement
atIndex: 0];
if(_rootElement == nil)
{
[self setRootElement: currentElement];
}
[currentElement setAttributesAsDictionary:
attributeDict];
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if([_elementStack count] > 0)
{
NSXMLElement *currentElement = [_elementStack objectAtIndex: 0];
if([[currentElement name] isEqualToString: elementName])
{
[_elementStack removeObjectAtIndex: 0];
}
}
}
- (void) parser: (NSXMLParser *)parser
foundCharacters: (NSString *)string
{
NSXMLElement *currentElement = [_elementStack objectAtIndex: 0];
[currentElement setStringValue: string];
}
@end

View file

@ -45,8 +45,11 @@
- (id) initWithName: (NSString*)name URI: (NSString*)URI
{
[self notImplemented: _cmd];
return nil;
if((self = [super init]) != nil)
{
ASSIGN(_name, name);
}
return self;
}
- (id) initWithName: (NSString*)name stringValue: (NSString*)string
@ -75,12 +78,24 @@
- (void) addAttribute: (NSXMLNode*)attribute
{
[self notImplemented: _cmd];
[_attributes addObject: attribute];
}
- (void) removeAttributeForName: (NSString*)name
{
[self notImplemented: _cmd];
NSEnumerator *en = [_attributes objectEnumerator];
NSXMLNode *node = nil;
int index = 0;
while((node = [en nextObject]) != nil)
{
NSString *nodeName = [node name];
if([nodeName isEqualToString: name])
{
[_attributes removeObjectAtIndex: index];
}
index++;
}
}
- (void) setAttributes: (NSArray*)attributes
@ -96,15 +111,19 @@
- (void) setAttributesAsDictionary: (NSDictionary*)attributes
{
[self notImplemented: _cmd];
NSEnumerator *ken = [attributes keyEnumerator];
id key = nil;
while((key = [ken nextObject]) != nil)
{
id value = [attributes objectForKey: key];
NSXMLNode *node = [NSXMLNode attributeWithName: key
stringValue: value];
[self addAttribute: node];
}
}
- (NSArray*) attributes
{
if (_attributes == nil)
{
[self notImplemented: _cmd];
}
return _attributes;
}
@ -181,7 +200,7 @@
{
NSEnumerator *enumerator = [children objectEnumerator];
NSXMLNode *child;
while ((child = [enumerator nextObject]) != nil)
{
[self insertChild: child atIndex: index++];
@ -190,22 +209,25 @@
- (void) removeChildAtIndex: (NSUInteger)index
{
[self notImplemented: _cmd];
[_children removeObjectAtIndex: index];
}
- (void) setChildren: (NSArray*)children
{
[self notImplemented: _cmd];
ASSIGN(_children, [children mutableCopy]);
_childrenHaveMutated = YES;
}
- (void) addChild: (NSXMLNode*)child
{
[self notImplemented: _cmd];
[_children addObject: child];
_childrenHaveMutated = YES;
}
- (void) replaceChildAtIndex: (NSUInteger)index withNode: (NSXMLNode*)node
{
[self notImplemented: _cmd];
[self removeChildAtIndex: index];
[self insertChild: node atIndex: index];
}
- (void) normalizeAdjacentTextNodesPreservingCDATA: (BOOL)preserve

View file

@ -41,7 +41,7 @@
NSUInteger childCount; \
NSXMLNode *previousSibling; \
NSXMLNode *nextSibling; \
NSUInteger options;
NSUInteger options;
#define GSInternal NSXMLNodeInternal
#include "GSInternal.h"
@ -50,6 +50,18 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
#import "NSXMLPrivate.h"
@implementation NSXMLNode (Private)
- (void) setName: (NSString *)name
{
ASSIGN(_name, name);
}
- (void) setParent: (NSXMLNode *)node
{
ASSIGN(_parent,node);
}
@end
@implementation NSXMLNode
+ (id) attributeWithName: (NSString*)name
@ -59,6 +71,7 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
n = [[[self alloc] initWithKind: NSXMLAttributeKind] autorelease];
[n setStringValue: stringValue];
[n setName: name];
return n;
}
@ -71,6 +84,7 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
n = [[[self alloc] initWithKind: NSXMLAttributeKind] autorelease];
[n setURI: URI];
[n setStringValue: stringValue];
[n setName: name];
return n;
}
@ -227,6 +241,7 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
[internal->children release];
GS_DESTROY_INTERNAL(NSXMLNode)
[_objectValue release];
[_name release];
[super dealloc];
}
@ -346,7 +361,7 @@ GS_PRIVATE_INTERNAL(NSXMLNode)
- (NSString*) name
{
return [self notImplemented: _cmd]; // FIXME ... fetch from libxml
return _name; // FIXME: Fetch from libxml????
}
- (NSXMLNode*) _nodeFollowingInNaturalDirection: (BOOL)forward

View file

@ -74,3 +74,8 @@
#endif /* HAVE_LIBXML */
#endif
@interface NSXMLNode (Private)
- (void) setName: (NSString *)name;
- (void) setParent: (NSXMLNode *)node;
@end

View file

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

View file

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