2009-02-09 16:16:11 +00:00
|
|
|
/* Implementation for NSXMLDocument for GNUStep
|
|
|
|
Copyright (C) 2008 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
Created: September 2008
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
|
|
|
*/
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
|
|
|
|
2010-02-17 11:47:06 +00:00
|
|
|
#import "NSXMLPrivate.h"
|
2012-01-04 12:41:45 +00:00
|
|
|
|
|
|
|
#define GSInternal NSXMLDocumentInternal
|
|
|
|
#include "GSInternal.h"
|
|
|
|
GS_PRIVATE_INTERNAL(NSXMLDocument)
|
|
|
|
|
2011-12-30 21:40:12 +00:00
|
|
|
#import <Foundation/NSXMLParser.h>
|
|
|
|
|
|
|
|
// Forward declaration of interface for NSXMLParserDelegate
|
|
|
|
@interface NSXMLDocument (NSXMLParserDelegate)
|
|
|
|
@end
|
2009-02-09 16:16:11 +00:00
|
|
|
|
|
|
|
@implementation NSXMLDocument
|
|
|
|
|
|
|
|
+ (Class) replacementClassForClass: (Class)cls
|
|
|
|
{
|
|
|
|
return Nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
if (GS_EXISTS_INTERNAL)
|
|
|
|
{
|
|
|
|
[internal->encoding release];
|
|
|
|
[internal->version release];
|
|
|
|
[internal->docType release];
|
|
|
|
[internal->MIMEType release];
|
|
|
|
[internal->elementStack release];
|
|
|
|
[internal->xmlData release];
|
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) characterEncoding
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->encoding;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLDocumentContentKind) documentContentKind
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->contentKind;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLDTD*) DTD
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->docType;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithContentsOfURL: (NSURL*)url
|
|
|
|
options: (NSUInteger)mask
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
NSData *data;
|
|
|
|
NSXMLDocument *doc;
|
|
|
|
|
|
|
|
data = [NSData dataWithContentsOfURL: url];
|
|
|
|
doc = [self initWithData: data options: 0 error: 0];
|
2011-12-30 21:40:12 +00:00
|
|
|
[doc setURI: [url absoluteString]];
|
2009-02-09 16:16:11 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (id) initWithData: (NSData*)data
|
|
|
|
options: (NSUInteger)mask
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
GS_CREATE_INTERNAL(NSXMLDocument)
|
|
|
|
if ((self = [super initWithKind: NSXMLDocumentKind options: 0]) != nil)
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
|
|
|
NSXMLParser *parser = [[NSXMLParser alloc] initWithData: data];
|
2012-01-04 12:41:45 +00:00
|
|
|
|
2012-01-01 07:38:53 +00:00
|
|
|
if (parser != nil)
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
internal->standalone = YES;
|
|
|
|
internal->elementStack = [[NSMutableArray alloc] initWithCapacity: 10];
|
|
|
|
ASSIGN(internal->xmlData, data);
|
2011-12-30 21:40:12 +00:00
|
|
|
[parser setDelegate: self];
|
|
|
|
[parser parse];
|
2012-01-03 17:15:29 +00:00
|
|
|
RELEASE(parser);
|
2011-12-30 21:40:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-01-04 12:41:45 +00:00
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (id) initWithRootElement: (NSXMLElement*)element
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
if ([element parent] != nil)
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"%@ cannot be used as root of %@",
|
|
|
|
element,
|
|
|
|
self];
|
|
|
|
}
|
2012-01-04 09:20:32 +00:00
|
|
|
self = [self initWithData: nil options: 0 error: 0];
|
2012-01-01 07:38:53 +00:00
|
|
|
if (self != nil)
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
|
|
|
[self setRootElement: (NSXMLNode*)element];
|
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithXMLString: (NSString*)string
|
|
|
|
options: (NSUInteger)mask
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
2011-12-30 21:40:12 +00:00
|
|
|
NSData *data = [NSData dataWithBytes: [string UTF8String]
|
|
|
|
length: [string length]];
|
|
|
|
self = [self initWithData: data
|
|
|
|
options: mask
|
|
|
|
error: error];
|
|
|
|
return self;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isStandalone
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->standalone;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) MIMEType
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->MIMEType;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLElement*) rootElement
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->rootElement;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setCharacterEncoding: (NSString*)encoding
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
ASSIGNCOPY(internal->encoding, encoding);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDocumentContentKind: (NSXMLDocumentContentKind)kind
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
internal->contentKind = kind;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDTD: (NSXMLDTD*)documentTypeDeclaration
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
ASSIGNCOPY(internal->docType, documentTypeDeclaration);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setMIMEType: (NSString*)MIMEType
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
ASSIGNCOPY(internal->MIMEType, MIMEType);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setRootElement: (NSXMLNode*)root
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
NSAssert(internal->rootElement == nil, NSGenericException);
|
|
|
|
[self insertChild: root atIndex: [internal->children count]];
|
|
|
|
internal->rootElement = (NSXMLElement*)root;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStandalone: (BOOL)standalone
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
internal->standalone = standalone;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setVersion: (NSString*)version
|
|
|
|
{
|
|
|
|
if ([version isEqualToString: @"1.0"] || [version isEqualToString: @"1.1"])
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
ASSIGNCOPY(internal->version, version);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Bad XML version (%@)", version];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) version
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->version;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
|
|
|
|
{
|
2011-12-30 21:40:12 +00:00
|
|
|
[child setParent: self];
|
2012-01-04 12:41:45 +00:00
|
|
|
[(NSMutableArray *)internal->children insertObject: child atIndex: index];
|
|
|
|
internal->childrenHaveMutated = YES;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertChildren: (NSArray*)children atIndex: (NSUInteger)index
|
|
|
|
{
|
|
|
|
NSEnumerator *enumerator = [children objectEnumerator];
|
|
|
|
NSXMLNode *node;
|
|
|
|
|
|
|
|
while ((node = [enumerator nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[self insertChild: node atIndex: index++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeChildAtIndex: (NSUInteger)index
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
[(NSMutableArray *)internal->children removeObjectAtIndex: index];
|
|
|
|
internal->childrenHaveMutated = YES;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setChildren: (NSArray*)children
|
|
|
|
{
|
|
|
|
unsigned count;
|
|
|
|
|
2012-01-04 12:41:45 +00:00
|
|
|
while ((count = [internal->children count]) > 0)
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
|
|
|
[self removeChildAtIndex: count - 1];
|
|
|
|
}
|
|
|
|
[self insertChildren: children atIndex: 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addChild: (NSXMLNode*)child
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
[self insertChild: child atIndex: [internal->children count]];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) replaceChildAtIndex: (NSUInteger)index withNode: (NSXMLNode*)node
|
|
|
|
{
|
|
|
|
[self removeChildAtIndex: index];
|
|
|
|
[self insertChild: node atIndex: index];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData*) XMLData
|
|
|
|
{
|
|
|
|
return [self XMLDataWithOptions: NSXMLNodeOptionsNone];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData*) XMLDataWithOptions: (NSUInteger)options
|
|
|
|
{
|
2011-12-30 21:40:12 +00:00
|
|
|
// TODO: Apply options to data.
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->xmlData;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectByApplyingXSLT: (NSData*)xslt
|
|
|
|
arguments: (NSDictionary*)arguments
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectByApplyingXSLTString: (NSString*)xslt
|
|
|
|
arguments: (NSDictionary*)arguments
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectByApplyingXSLTAtURL: (NSURL*)xsltURL
|
|
|
|
arguments: (NSDictionary*)argument
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) validateAndReturnError: (NSError**)error
|
|
|
|
{
|
|
|
|
[self notImplemented: _cmd];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2011-12-30 21:40:12 +00:00
|
|
|
@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];
|
|
|
|
|
2012-01-04 12:41:45 +00:00
|
|
|
[internal->elementStack addObject: currentElement];
|
|
|
|
if (internal->rootElement == nil)
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
|
|
|
[self setRootElement: currentElement];
|
|
|
|
}
|
|
|
|
|
|
|
|
[currentElement setAttributesAsDictionary:
|
|
|
|
attributeDict];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)parser:(NSXMLParser *)parser
|
|
|
|
didEndElement:(NSString *)elementName
|
|
|
|
namespaceURI:(NSString *)namespaceURI
|
|
|
|
qualifiedName:(NSString *)qName
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
if ([internal->elementStack count] > 0)
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
NSXMLElement *currentElement = [internal->elementStack lastObject];
|
2012-01-01 07:38:53 +00:00
|
|
|
if ([[currentElement name] isEqualToString: elementName])
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
[internal->elementStack removeLastObject];
|
2011-12-30 21:40:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) parser: (NSXMLParser *)parser
|
|
|
|
foundCharacters: (NSString *)string
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
NSXMLElement *currentElement = [internal->elementStack lastObject];
|
2011-12-30 21:40:12 +00:00
|
|
|
[currentElement setStringValue: string];
|
|
|
|
}
|
|
|
|
@end
|