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>
|
2012-01-10 20:42:32 +00:00
|
|
|
Written by: Gregory John Casamento <greg.casamento@gmail.com>
|
|
|
|
Created/Modified: September 2008,2012
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
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"
|
|
|
|
|
2012-01-04 12:41:45 +00:00
|
|
|
#define GSInternal NSXMLDocumentInternal
|
2012-01-05 20:40:12 +00:00
|
|
|
#import "NSXMLPrivate.h"
|
|
|
|
#import "GSInternal.h"
|
2012-01-04 12:41:45 +00:00
|
|
|
GS_PRIVATE_INTERNAL(NSXMLDocument)
|
|
|
|
|
2011-12-30 21:40:12 +00:00
|
|
|
#import <Foundation/NSXMLParser.h>
|
|
|
|
|
2012-01-11 01:21:59 +00:00
|
|
|
// Private methods to manage libxml pointers...
|
|
|
|
@interface NSXMLNode (Private)
|
|
|
|
- (void *) _node;
|
|
|
|
- (void) _setNode: (void *)_anode;
|
|
|
|
@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->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-12 22:41:55 +00:00
|
|
|
if (MY_DOC->encoding)
|
|
|
|
return [NSString stringWithUTF8String: (const char *)MY_DOC->encoding];
|
|
|
|
else
|
|
|
|
return nil;
|
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
|
|
|
}
|
|
|
|
|
2012-01-05 18:55:17 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
return [self initWithKind: NSXMLDocumentKind options: 0];
|
|
|
|
}
|
|
|
|
|
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-10 20:42:32 +00:00
|
|
|
NSString *string = [[NSString alloc] initWithData: data
|
|
|
|
encoding: NSUTF8StringEncoding];
|
|
|
|
AUTORELEASE(string);
|
2012-01-11 00:29:28 +00:00
|
|
|
return [self initWithXMLString:string options:mask error:error];
|
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.
|
|
|
|
*/
|
2012-01-12 22:41:55 +00:00
|
|
|
xmlChar *version = (xmlChar *)"1.0";
|
|
|
|
GS_CREATE_INTERNAL(NSXMLDocument);
|
|
|
|
internal->node = xmlNewDoc(version);
|
|
|
|
MY_DOC->_private = (void *)self;
|
2012-01-04 12:41:45 +00:00
|
|
|
}
|
|
|
|
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-05 18:55:17 +00:00
|
|
|
self = [self initWithKind: NSXMLDocumentKind options: 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
|
|
|
|
{
|
2012-01-10 22:25:22 +00:00
|
|
|
if (NO == [string isKindOfClass: [NSString class]])
|
2012-01-10 20:42:32 +00:00
|
|
|
{
|
|
|
|
DESTROY(self);
|
2012-01-10 22:25:22 +00:00
|
|
|
if (nil == string)
|
2012-01-10 20:42:32 +00:00
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[NSXMLDocument-%@] nil argument",
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[NSXMLDocument-%@] invalid argument",
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
GS_CREATE_INTERNAL(NSXMLDocument)
|
|
|
|
if ((self = [super initWithKind: NSXMLDocumentKind options: 0]) != nil)
|
|
|
|
{
|
2012-01-10 22:25:22 +00:00
|
|
|
const char *str = [string UTF8String];
|
2012-01-10 20:42:32 +00:00
|
|
|
char *url = NULL;
|
|
|
|
char *encoding = NULL; // "UTF8";
|
|
|
|
int options = 0;
|
|
|
|
|
|
|
|
GS_CREATE_INTERNAL(NSXMLDocument); // create internal ivars...
|
2012-01-10 22:25:22 +00:00
|
|
|
internal->node = xmlReadDoc((xmlChar *)str, url, encoding, options);
|
|
|
|
MY_DOC->_private = (void *)self;
|
2012-01-10 20:42:32 +00:00
|
|
|
}
|
2011-12-30 21:40:12 +00:00
|
|
|
return self;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isStandalone
|
|
|
|
{
|
2012-01-10 22:25:22 +00:00
|
|
|
return (MY_DOC->standalone == 1);
|
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-10 22:25:22 +00:00
|
|
|
xmlNodePtr node = xmlDocGetRootElement(MY_DOC);
|
2012-01-10 20:42:32 +00:00
|
|
|
return (NSXMLElement *)(node->_private);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setCharacterEncoding: (NSString*)encoding
|
|
|
|
{
|
2012-01-11 00:29:28 +00:00
|
|
|
MY_DOC->encoding = XMLSTRING(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-10 22:25:22 +00:00
|
|
|
NSAssert(documentTypeDeclaration != nil, NSInvalidArgumentException);
|
2012-01-04 12:41:45 +00:00
|
|
|
ASSIGNCOPY(internal->docType, documentTypeDeclaration);
|
2012-01-10 22:25:22 +00:00
|
|
|
MY_DOC->extSubset = [documentTypeDeclaration _node];
|
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-12 18:49:13 +00:00
|
|
|
/*
|
2012-01-10 20:42:32 +00:00
|
|
|
NSArray *children;
|
2012-01-06 12:22:30 +00:00
|
|
|
|
2012-01-10 20:42:32 +00:00
|
|
|
NSAssert(root == nil, NSInvalidArgumentException);
|
|
|
|
|
2012-01-12 18:49:13 +00:00
|
|
|
// this method replaces *all* children with the specified element.
|
2012-01-10 20:42:32 +00:00
|
|
|
children = [[NSArray alloc] initWithObjects: &root count: 1];
|
|
|
|
[self setChildren: children];
|
|
|
|
[children release];
|
|
|
|
internal->rootElement = (NSXMLElement*)root;
|
2012-01-12 18:49:13 +00:00
|
|
|
*/
|
2012-01-13 19:24:44 +00:00
|
|
|
xmlNodePtr newrootnode;
|
2012-01-13 00:44:10 +00:00
|
|
|
NSAssert(root != nil, NSInvalidArgumentException);
|
2012-01-10 20:42:32 +00:00
|
|
|
// Set
|
2012-01-10 22:25:22 +00:00
|
|
|
xmlDocSetRootElement(MY_DOC,[root _node]);
|
2012-01-13 00:44:10 +00:00
|
|
|
newrootnode = MY_DOC->children;
|
|
|
|
newrootnode->_private = root; // hmmm, this probably isn't where this belongs, but try it...
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStandalone: (BOOL)standalone
|
|
|
|
{
|
2012-01-10 22:25:22 +00:00
|
|
|
MY_DOC->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-12 22:41:55 +00:00
|
|
|
MY_DOC->version = XMLStringCopy(version);
|
2012-01-10 20:42:32 +00:00
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Bad XML version (%@)", version];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) version
|
|
|
|
{
|
2012-01-12 22:41:55 +00:00
|
|
|
if (MY_DOC->version)
|
|
|
|
return StringFromXMLStringPtr(MY_DOC->version);
|
|
|
|
else
|
|
|
|
return @"1.0";
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
|
|
|
|
{
|
2012-01-06 12:22:30 +00:00
|
|
|
NSXMLNodeKind kind;
|
|
|
|
|
|
|
|
NSAssert(nil != child, NSInvalidArgumentException);
|
|
|
|
NSAssert(index <= internal->childCount, NSInvalidArgumentException);
|
|
|
|
NSAssert(nil == [child parent], NSInvalidArgumentException);
|
|
|
|
kind = [child kind];
|
|
|
|
NSAssert(NSXMLAttributeKind != kind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLDTDKind != kind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLDocumentKind != kind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLElementDeclarationKind != kind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLEntityDeclarationKind != kind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLInvalidKind != kind, NSInvalidArgumentException);
|
|
|
|
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++;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertChildren: (NSArray*)children atIndex: (NSUInteger)index
|
|
|
|
{
|
|
|
|
NSEnumerator *enumerator = [children objectEnumerator];
|
2012-01-06 12:22:30 +00:00
|
|
|
NSXMLNode *child;
|
|
|
|
|
|
|
|
while ((child = [enumerator nextObject]) != nil)
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-01-06 12:22:30 +00:00
|
|
|
[self insertChild: child atIndex: index++];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeChildAtIndex: (NSUInteger)index
|
|
|
|
{
|
2012-01-06 12:22:30 +00:00
|
|
|
NSXMLNode *child = [internal->children objectAtIndex: index];
|
|
|
|
|
|
|
|
if (nil != child)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setChildren: (NSArray*)children
|
|
|
|
{
|
2012-01-06 12:22:30 +00:00
|
|
|
if (children != internal->children)
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-01-06 12:22:30 +00:00
|
|
|
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];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-06 12:22:30 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (void) addChild: (NSXMLNode*)child
|
|
|
|
{
|
2012-01-06 12:22:30 +00:00
|
|
|
[self insertChild: child atIndex: internal->childCount];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
2012-01-06 12:22:30 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (void) replaceChildAtIndex: (NSUInteger)index withNode: (NSXMLNode*)node
|
|
|
|
{
|
|
|
|
[self insertChild: node atIndex: index];
|
2012-01-06 12:22:30 +00:00
|
|
|
[self removeChildAtIndex: index + 1];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData*) XMLData
|
|
|
|
{
|
|
|
|
return [self XMLDataWithOptions: NSXMLNodeOptionsNone];
|
|
|
|
}
|
|
|
|
|
2012-01-06 02:43:26 +00:00
|
|
|
- (NSData *) XMLDataWithOptions: (NSUInteger)options
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-01-06 02:43:26 +00:00
|
|
|
NSString *xmlString = [self XMLStringWithOptions: options];
|
|
|
|
NSData *data = [NSData dataWithBytes: [xmlString UTF8String]
|
|
|
|
length: [xmlString length]];
|
|
|
|
return data;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 15:44:45 +00:00
|
|
|
- (NSString *) XMLStringWithOptions: (NSUInteger)options
|
|
|
|
{
|
2012-01-10 20:42:32 +00:00
|
|
|
NSString *string = nil;
|
|
|
|
xmlChar *buf = NULL;
|
|
|
|
int length;
|
2012-01-05 15:44:45 +00:00
|
|
|
|
2012-01-10 22:25:22 +00:00
|
|
|
xmlDocDumpFormatMemoryEnc(MY_DOC, &buf, &length, "utf-8", 1);
|
2012-01-10 20:42:32 +00:00
|
|
|
|
|
|
|
if (buf != 0 && length > 0)
|
2012-01-05 15:44:45 +00:00
|
|
|
{
|
2012-01-11 01:12:35 +00:00
|
|
|
string = StringFromXMLString(buf, length);
|
2012-01-10 20:42:32 +00:00
|
|
|
free(buf);
|
2012-01-05 15:44:45 +00:00
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-06 02:43:26 +00:00
|
|
|
- (id) copyWithZone: (NSZone *)zone
|
|
|
|
{
|
2012-01-06 12:22:30 +00:00
|
|
|
NSXMLDocument *c = (NSXMLDocument*)[super copyWithZone: zone];
|
2012-01-10 22:25:22 +00:00
|
|
|
internal->node = (xmlDoc *)xmlCopyDoc(MY_DOC, 1); // copy recursively
|
|
|
|
#warning need to zero out all of the _private pointers in the copied xmlDoc
|
|
|
|
// [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];
|
2012-01-06 02:43:26 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
@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
|
|
|
|
{
|
2012-01-05 15:44:45 +00:00
|
|
|
NSXMLElement *lastElement = [internal->elementStack lastObject];
|
2011-12-30 21:40:12 +00:00
|
|
|
NSXMLElement *currentElement =
|
|
|
|
[[NSXMLElement alloc] initWithName: elementName];
|
|
|
|
|
2012-01-05 15:44:45 +00:00
|
|
|
[lastElement addChild: currentElement];
|
2012-01-04 12:41:45 +00:00
|
|
|
[internal->elementStack addObject: currentElement];
|
2012-01-05 15:44:45 +00:00
|
|
|
[currentElement release];
|
2012-01-06 12:22:30 +00:00
|
|
|
if (nil == internal->rootElement)
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
|
|
|
[self setRootElement: currentElement];
|
|
|
|
}
|
2012-01-05 15:44:45 +00:00
|
|
|
[currentElement setAttributesAsDictionary: attributeDict];
|
2011-12-30 21:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|