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-02-20 03:40:15 +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-03-21 08:00:33 +00:00
|
|
|
#if defined(HAVE_LIBXML)
|
|
|
|
|
2012-03-12 13:27:32 +00:00
|
|
|
#define GSInternal NSXMLDocumentInternal
|
|
|
|
|
2012-01-05 20:40:12 +00:00
|
|
|
#import "NSXMLPrivate.h"
|
|
|
|
#import "GSInternal.h"
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2012-01-04 12:41:45 +00:00
|
|
|
GS_PRIVATE_INTERNAL(NSXMLDocument)
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
//#import <Foundation/NSXMLParser.h>
|
2015-11-01 22:49:21 +00:00
|
|
|
#import "Foundation/NSError.h"
|
2011-12-30 21:40:12 +00:00
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
@implementation NSXMLDocument
|
|
|
|
|
|
|
|
+ (Class) replacementClassForClass: (Class)cls
|
|
|
|
{
|
2012-02-27 12:53:25 +00:00
|
|
|
return cls;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
if (GS_EXISTS_INTERNAL)
|
|
|
|
{
|
|
|
|
[internal->MIMEType release];
|
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) characterEncoding
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
if (internal->node.doc->encoding)
|
|
|
|
return StringFromXMLStringPtr(internal->node.doc->encoding);
|
2012-02-20 03:40:15 +00:00
|
|
|
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
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDtdPtr dtd = xmlGetIntSubset(internal->node.doc);
|
2012-03-17 13:57:58 +00:00
|
|
|
return (NSXMLDTD *)[NSXMLNode _objectForNode: (xmlNodePtr)dtd];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
- (void) _createInternal
|
|
|
|
{
|
|
|
|
GS_CREATE_INTERNAL(NSXMLDocument);
|
|
|
|
}
|
|
|
|
|
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];
|
2012-02-20 03:40:15 +00:00
|
|
|
doc = [self initWithData: data options: mask error: error];
|
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-02-20 03:40:15 +00:00
|
|
|
// Check for nil data and throw an exception
|
2012-02-27 12:53:25 +00:00
|
|
|
if (nil == data)
|
2012-01-05 18:55:17 +00:00
|
|
|
{
|
|
|
|
DESTROY(self);
|
2012-01-05 20:49:29 +00:00
|
|
|
[NSException raise: NSInvalidArgumentException
|
2012-02-27 12:53:25 +00:00
|
|
|
format: @"[NSXMLDocument-%@] nil argument",
|
2012-02-20 03:40:15 +00:00
|
|
|
NSStringFromSelector(_cmd)];
|
2012-01-05 18:55:17 +00:00
|
|
|
}
|
2012-02-27 12:53:25 +00:00
|
|
|
if (![data isKindOfClass: [NSData class]])
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
DESTROY(self);
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
2012-02-27 12:53:25 +00:00
|
|
|
format: @"[NSXMLDocument-%@] non data argument",
|
2012-02-20 03:40:15 +00:00
|
|
|
NSStringFromSelector(_cmd)];
|
2011-12-30 21:40:12 +00:00
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2012-02-27 12:53:25 +00:00
|
|
|
if ((self = [self initWithKind: NSXMLDocumentKind options: 0]) != nil)
|
|
|
|
{
|
|
|
|
char *url = NULL;
|
|
|
|
char *encoding = NULL; // "UTF8";
|
2012-04-06 22:27:55 +00:00
|
|
|
int xmlOptions = XML_PARSE_NOERROR;
|
2012-02-27 12:53:25 +00:00
|
|
|
xmlDocPtr doc = NULL;
|
|
|
|
|
|
|
|
if (!(mask & NSXMLNodePreserveWhitespace))
|
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
xmlOptions |= XML_PARSE_NOBLANKS;
|
2012-02-27 12:53:25 +00:00
|
|
|
//xmlKeepBlanksDefault(0);
|
|
|
|
}
|
2014-04-11 21:43:14 +00:00
|
|
|
if (mask & NSXMLNodeLoadExternalEntitiesNever)
|
|
|
|
{
|
|
|
|
xmlOptions |= XML_PARSE_NOENT;
|
|
|
|
}
|
|
|
|
if (!(mask & NSXMLNodeLoadExternalEntitiesAlways))
|
|
|
|
{
|
|
|
|
xmlOptions |= XML_PARSE_NONET;
|
|
|
|
}
|
|
|
|
|
2012-02-27 12:53:25 +00:00
|
|
|
doc = xmlReadMemory([data bytes], [data length],
|
2012-04-06 22:27:55 +00:00
|
|
|
url, encoding, xmlOptions);
|
2012-02-27 12:53:25 +00:00
|
|
|
if (doc == NULL)
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
if (error != NULL)
|
|
|
|
{
|
|
|
|
*error = [NSError errorWithDomain: @"NSXMLErrorDomain"
|
|
|
|
code: 0
|
|
|
|
userInfo: nil];
|
|
|
|
}
|
2012-03-17 13:57:58 +00:00
|
|
|
return nil;
|
2012-02-27 12:53:25 +00:00
|
|
|
}
|
2012-03-17 13:57:58 +00:00
|
|
|
|
|
|
|
// Free old node
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlFreeDoc((xmlDocPtr)internal->node.doc);
|
2012-02-27 12:53:25 +00:00
|
|
|
[self _setNode: doc];
|
2012-03-17 13:57:58 +00:00
|
|
|
|
|
|
|
if (mask & NSXMLDocumentValidate)
|
|
|
|
{
|
|
|
|
[self validateAndReturnError: error];
|
|
|
|
}
|
2012-02-27 12:53:25 +00:00
|
|
|
}
|
2012-03-17 13:57:58 +00:00
|
|
|
|
2012-02-27 12:53:25 +00:00
|
|
|
return self;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
- (id) initWithKind: (NSXMLNodeKind)theKind options: (NSUInteger)theOptions
|
2012-01-04 12:41:45 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
if (NSXMLDocumentKind == theKind)
|
2012-01-04 12:41:45 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
return [super initWithKind: theKind options: theOptions];
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self release];
|
2012-04-06 22:27:55 +00:00
|
|
|
// This cast is here to keep clang quite that expects an init* method to
|
|
|
|
// return an object of the same class, which is not true here.
|
|
|
|
return (NSXMLDocument*)[[NSXMLNode alloc] initWithKind: theKind
|
|
|
|
options: theOptions];
|
2012-01-04 12:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (id) initWithRootElement: (NSXMLElement*)element
|
|
|
|
{
|
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-02-27 12:53:25 +00:00
|
|
|
if (nil == string)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
2012-02-27 12:53:25 +00:00
|
|
|
format: @"[NSXMLDocument-%@] nil argument",
|
|
|
|
NSStringFromSelector(_cmd)];
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
2012-02-27 12:53:25 +00:00
|
|
|
if (NO == [string isKindOfClass: [NSString class]])
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-27 12:53:25 +00:00
|
|
|
DESTROY(self);
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[NSXMLDocument-%@] invalid argument",
|
|
|
|
NSStringFromSelector(_cmd)];
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
2012-02-27 12:53:25 +00:00
|
|
|
return [self initWithData: [string dataUsingEncoding: NSUTF8StringEncoding]
|
|
|
|
options: mask
|
|
|
|
error: error];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isStandalone
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
return (internal->node.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
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlNodePtr rootElem = xmlDocGetRootElement(internal->node.doc);
|
2012-02-27 12:53:25 +00:00
|
|
|
return (NSXMLElement *)[NSXMLNode _objectForNode: rootElem];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setCharacterEncoding: (NSString*)encoding
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
if (internal->node.doc->encoding != NULL)
|
2012-03-21 09:01:48 +00:00
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlFree((xmlChar *)internal->node.doc->encoding);
|
2012-03-21 09:01:48 +00:00
|
|
|
}
|
2018-03-20 17:44:16 +00:00
|
|
|
internal->node.doc->encoding = XMLStringCopy(encoding);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
- (void) setDocumentContentKind: (NSXMLDocumentContentKind)theContentKind
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
internal->contentKind = theContentKind;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDTD: (NSXMLDTD*)documentTypeDeclaration
|
|
|
|
{
|
2012-03-17 13:57:58 +00:00
|
|
|
NSXMLDTD *old;
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
NSAssert(documentTypeDeclaration != nil, NSInvalidArgumentException);
|
2012-03-17 13:57:58 +00:00
|
|
|
|
|
|
|
// detach the old DTD, this also removes the corresponding child
|
|
|
|
old = [self DTD];
|
|
|
|
[old detach];
|
|
|
|
|
2018-03-20 17:44:16 +00:00
|
|
|
internal->node.doc->intSubset = (xmlDtdPtr)[documentTypeDeclaration _node];
|
2012-03-17 13:57:58 +00:00
|
|
|
[self addChild: documentTypeDeclaration];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
- (void) setMIMEType: (NSString*)theMIMEType
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
ASSIGNCOPY(internal->MIMEType, theMIMEType);
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setRootElement: (NSXMLNode*)root
|
|
|
|
{
|
2012-02-23 17:57:50 +00:00
|
|
|
if (root == nil)
|
2012-01-07 13:08:03 +00:00
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
return;
|
2012-01-07 13:08:03 +00:00
|
|
|
}
|
2012-02-27 12:53:25 +00:00
|
|
|
if ([root parent] != nil)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"%@ cannot be used as root of %@",
|
|
|
|
root,
|
|
|
|
self];
|
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2012-03-14 22:41:32 +00:00
|
|
|
// remove all sub nodes
|
|
|
|
[self setChildren: nil];
|
2012-03-04 21:40:39 +00:00
|
|
|
|
2012-03-21 09:01:48 +00:00
|
|
|
// FIXME: Should we use addChild: here?
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDocSetRootElement(internal->node.doc, [root _node]);
|
2012-02-20 03:40:15 +00:00
|
|
|
|
|
|
|
// Do our subNode housekeeping...
|
2012-02-27 12:53:25 +00:00
|
|
|
[self _addSubNode: root];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStandalone: (BOOL)standalone
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
internal->node.doc->standalone = standalone;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 09:34:54 +00:00
|
|
|
- (void) setURI: (NSString*)URI
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDocPtr theNode = internal->node.doc;
|
2012-03-22 09:34:54 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
if (theNode->URL != NULL)
|
2012-03-22 09:34:54 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
xmlFree((xmlChar *)theNode->URL);
|
2012-03-22 09:34:54 +00:00
|
|
|
}
|
2012-04-06 22:27:55 +00:00
|
|
|
theNode->URL = XMLStringCopy(URI);
|
2012-03-22 09:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) URI
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDocPtr theNode = internal->node.doc;
|
2012-03-22 09:34:54 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
if (theNode->URL)
|
2012-03-22 09:34:54 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
return StringFromXMLStringPtr(theNode->URL);
|
2012-03-22 09:34:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:16:11 +00:00
|
|
|
- (void) setVersion: (NSString*)version
|
|
|
|
{
|
|
|
|
if ([version isEqualToString: @"1.0"] || [version isEqualToString: @"1.1"])
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDocPtr theNode = internal->node.doc;
|
2012-03-22 09:34:54 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
if (theNode->version != NULL)
|
2012-03-21 09:01:48 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
xmlFree((xmlChar *)theNode->version);
|
2012-03-21 09:01:48 +00:00
|
|
|
}
|
2012-04-06 22:27:55 +00:00
|
|
|
theNode->version = XMLStringCopy(version);
|
2012-03-21 09:01:48 +00:00
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Bad XML version (%@)", version];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) version
|
|
|
|
{
|
2018-03-20 17:44:16 +00:00
|
|
|
xmlDocPtr theNode = internal->node.doc;
|
2012-03-22 09:34:54 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
if (theNode->version)
|
|
|
|
return StringFromXMLStringPtr(theNode->version);
|
2012-02-20 03:40:15 +00:00
|
|
|
else
|
|
|
|
return @"1.0";
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
|
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
NSXMLNodeKind theKind = [child kind];
|
2012-02-20 03:40:15 +00:00
|
|
|
NSUInteger childCount = [self childCount];
|
2012-01-06 12:22:30 +00:00
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
// Check to make sure this is a valid addition...
|
2012-01-06 12:22:30 +00:00
|
|
|
NSAssert(nil != child, NSInvalidArgumentException);
|
2012-02-20 03:40:15 +00:00
|
|
|
NSAssert(index <= childCount, NSInvalidArgumentException);
|
2012-01-06 12:22:30 +00:00
|
|
|
NSAssert(nil == [child parent], NSInvalidArgumentException);
|
2012-04-06 22:27:55 +00:00
|
|
|
NSAssert(NSXMLAttributeKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLDTDKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLDocumentKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLElementDeclarationKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLEntityDeclarationKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLInvalidKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLNamespaceKind != theKind, NSInvalidArgumentException);
|
|
|
|
NSAssert(NSXMLNotationDeclarationKind != theKind, NSInvalidArgumentException);
|
2012-01-06 12:22:30 +00:00
|
|
|
|
2012-03-17 13:57:58 +00:00
|
|
|
[self _insertChild: child atIndex: index];
|
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-02-22 22:52:31 +00:00
|
|
|
NSXMLNode *child;
|
2012-01-06 12:22:30 +00:00
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
if (index >= [self childCount])
|
2012-01-06 12:22:30 +00:00
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
[NSException raise: NSRangeException
|
2012-02-22 22:52:31 +00:00
|
|
|
format: @"index too large"];
|
2012-01-06 12:22:30 +00:00
|
|
|
}
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2012-02-22 22:52:31 +00:00
|
|
|
child = [self childAtIndex: index];
|
|
|
|
[child detach];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setChildren: (NSArray*)children
|
|
|
|
{
|
2012-02-22 22:52:31 +00:00
|
|
|
NSUInteger count = [self childCount];
|
|
|
|
|
|
|
|
while (count-- > 0)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-22 22:52:31 +00:00
|
|
|
[self removeChildAtIndex: count];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
2012-02-22 22:52:31 +00:00
|
|
|
|
|
|
|
[self insertChildren: children atIndex: 0];
|
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-02-20 03:40:15 +00:00
|
|
|
[self insertChild: child atIndex: [self childCount]];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
2012-01-06 12:22:30 +00:00
|
|
|
|
2012-04-06 22:27:55 +00:00
|
|
|
- (void) replaceChildAtIndex: (NSUInteger)index withNode: (NSXMLNode*)theNode
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
[self insertChild: theNode 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-04-06 22:27:55 +00:00
|
|
|
- (NSData *) XMLDataWithOptions: (NSUInteger)theOptions
|
2009-02-09 16:16:11 +00:00
|
|
|
{
|
2012-04-06 22:27:55 +00:00
|
|
|
NSString *xmlString = [self XMLStringWithOptions: theOptions];
|
2012-03-14 22:41:32 +00:00
|
|
|
|
|
|
|
return [xmlString dataUsingEncoding: NSUTF8StringEncoding
|
|
|
|
allowLossyConversion: NO];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectByApplyingXSLT: (NSData*)xslt
|
|
|
|
arguments: (NSDictionary*)arguments
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
#ifdef HAVE_LIBXSLT
|
|
|
|
xmlChar **params = NULL;
|
2012-02-27 12:53:25 +00:00
|
|
|
xmlDocPtr stylesheetDoc = xmlReadMemory([xslt bytes], [xslt length],
|
2014-04-11 21:43:14 +00:00
|
|
|
NULL, NULL, XML_PARSE_NOERROR | XML_PARSE_NONET);
|
2012-02-20 03:40:15 +00:00
|
|
|
xsltStylesheetPtr stylesheet = xsltParseStylesheetDoc(stylesheetDoc);
|
|
|
|
xmlDocPtr resultDoc = NULL;
|
|
|
|
|
|
|
|
// Iterate over the keys and put them into params...
|
2012-02-23 17:57:50 +00:00
|
|
|
if (arguments != nil)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
2012-02-27 12:53:25 +00:00
|
|
|
NSEnumerator *en = [arguments keyEnumerator];
|
|
|
|
NSString *key = nil;
|
|
|
|
NSUInteger index = 0;
|
2012-02-20 03:40:15 +00:00
|
|
|
int count = [[arguments allKeys] count];
|
2012-02-27 12:53:25 +00:00
|
|
|
|
2013-04-14 06:31:52 +00:00
|
|
|
params = NSZoneCalloc([self zone], ((count + 1) * 2), sizeof(xmlChar *));
|
2012-02-23 17:57:50 +00:00
|
|
|
while ((key = [en nextObject]) != nil)
|
2012-02-20 03:40:15 +00:00
|
|
|
{
|
|
|
|
params[index] = (xmlChar *)XMLSTRING(key);
|
2013-04-14 06:31:52 +00:00
|
|
|
params[index + 1]
|
|
|
|
= (xmlChar *)XMLSTRING([arguments objectForKey: key]);
|
2012-02-27 12:53:25 +00:00
|
|
|
index += 2;
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
2012-03-17 13:57:58 +00:00
|
|
|
params[index] = NULL;
|
|
|
|
params[index + 1] = NULL;
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the stylesheet and get the result...
|
2018-03-20 17:44:16 +00:00
|
|
|
resultDoc = xsltApplyStylesheet(stylesheet, internal->node.doc,
|
2012-03-17 13:57:58 +00:00
|
|
|
(const char **)params);
|
2012-02-20 03:40:15 +00:00
|
|
|
|
|
|
|
// Cleanup...
|
|
|
|
xsltFreeStylesheet(stylesheet);
|
|
|
|
xmlFreeDoc(stylesheetDoc);
|
|
|
|
xsltCleanupGlobals();
|
|
|
|
xmlCleanupParser();
|
|
|
|
NSZoneFree([self zone], params);
|
|
|
|
|
|
|
|
return [NSXMLNode _objectForNode: (xmlNodePtr)resultDoc];
|
2015-11-03 09:15:26 +00:00
|
|
|
#else /* HAVE_LIBXSLT */
|
2009-02-09 16:16:11 +00:00
|
|
|
return nil;
|
2015-11-03 09:15:26 +00:00
|
|
|
#endif /* HAVE_LIBXSLT */
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectByApplyingXSLTString: (NSString*)xslt
|
|
|
|
arguments: (NSDictionary*)arguments
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
2012-02-27 12:53:25 +00:00
|
|
|
NSData *data = [xslt dataUsingEncoding: NSUTF8StringEncoding];
|
2012-02-22 22:52:31 +00:00
|
|
|
NSXMLDocument *result = [self objectByApplyingXSLT: data
|
|
|
|
arguments: arguments
|
|
|
|
error: error];
|
2012-02-20 03:40:15 +00:00
|
|
|
return result;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectByApplyingXSLTAtURL: (NSURL*)xsltURL
|
2012-02-20 03:40:15 +00:00
|
|
|
arguments: (NSDictionary*)arguments
|
2009-02-09 16:16:11 +00:00
|
|
|
error: (NSError**)error
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
NSData *data = [NSData dataWithContentsOfURL: xsltURL];
|
2012-02-22 22:52:31 +00:00
|
|
|
NSXMLDocument *result = [self objectByApplyingXSLT: data
|
|
|
|
arguments: arguments
|
|
|
|
error: error];
|
2012-02-20 03:40:15 +00:00
|
|
|
return result;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) validateAndReturnError: (NSError**)error
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
xmlValidCtxtPtr ctxt = xmlNewValidCtxt();
|
2012-02-22 22:52:31 +00:00
|
|
|
// FIXME: Should use xmlValidityErrorFunc and userData
|
|
|
|
// to get the error
|
2018-03-20 17:44:16 +00:00
|
|
|
BOOL result = (BOOL)(xmlValidateDocument(ctxt, internal->node.doc));
|
2012-02-22 22:52:31 +00:00
|
|
|
xmlFreeValidCtxt(ctxt);
|
2012-02-20 03:40:15 +00:00
|
|
|
return result;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 02:43:26 +00:00
|
|
|
- (id) copyWithZone: (NSZone *)zone
|
|
|
|
{
|
2012-02-27 18:35:38 +00:00
|
|
|
NSXMLDocument *c = (NSXMLDocument*)[super copyWithZone: zone];
|
2012-02-27 17:30:07 +00:00
|
|
|
|
2012-02-27 18:35:38 +00:00
|
|
|
[c setMIMEType: [self MIMEType]];
|
2012-03-17 13:57:58 +00:00
|
|
|
// the intSubset is copied by libxml2
|
|
|
|
//[c setDTD: [self DTD]];
|
2012-02-27 18:35:38 +00:00
|
|
|
[c setDocumentContentKind: [self documentContentKind]];
|
2012-01-06 02:43:26 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
- (BOOL) isEqual: (id)other
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
2012-02-22 22:52:31 +00:00
|
|
|
if (self == other)
|
2011-12-30 21:40:12 +00:00
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
return YES;
|
2011-12-30 21:40:12 +00:00
|
|
|
}
|
2012-03-14 22:41:32 +00:00
|
|
|
// FIXME
|
2012-02-20 03:40:15 +00:00
|
|
|
return [[self rootElement] isEqual: [other rootElement]];
|
2011-12-30 21:40:12 +00:00
|
|
|
}
|
|
|
|
@end
|
2012-02-20 03:40:15 +00:00
|
|
|
|
2015-11-03 09:15:26 +00:00
|
|
|
#else /* HAVE_LIBXML */
|
2015-11-01 22:40:55 +00:00
|
|
|
|
2015-11-01 22:49:21 +00:00
|
|
|
#import "Foundation/NSXMLDocument.h"
|
|
|
|
|
2015-11-01 22:40:55 +00:00
|
|
|
@implementation NSXMLDocument
|
|
|
|
|
|
|
|
+ (Class) replacementClassForClass: (Class)cls
|
|
|
|
{
|
|
|
|
return cls;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) characterEncoding
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLDocumentContentKind) documentContentKind
|
|
|
|
{
|
2015-11-01 22:49:21 +00:00
|
|
|
return 0;
|
2015-11-01 22:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLDTD*) DTD
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
2015-11-02 09:07:39 +00:00
|
|
|
return [super init];
|
2015-11-01 22:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithContentsOfURL: (NSURL*)url
|
|
|
|
options: (NSUInteger)mask
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
return [self init];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithData: (NSData*)data
|
|
|
|
options: (NSUInteger)mask
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
return [self init];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithKind: (NSXMLNodeKind)theKind options: (NSUInteger)theOptions
|
|
|
|
{
|
2015-11-01 22:49:21 +00:00
|
|
|
return [self init];
|
2015-11-01 22:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithRootElement: (NSXMLElement*)element
|
|
|
|
{
|
|
|
|
return [self init];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithXMLString: (NSString*)string
|
|
|
|
options: (NSUInteger)mask
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
return [self init];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isStandalone
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) MIMEType
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSXMLElement*) rootElement
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setCharacterEncoding: (NSString*)encoding
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDocumentContentKind: (NSXMLDocumentContentKind)theContentKind
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDTD: (NSXMLDTD*)documentTypeDeclaration
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setMIMEType: (NSString*)theMIMEType
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setRootElement: (NSXMLNode*)root
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStandalone: (BOOL)standalone
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setURI: (NSString*)URI
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) URI
|
|
|
|
{
|
2015-11-01 22:49:21 +00:00
|
|
|
return nil;
|
2015-11-01 22:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setVersion: (NSString*)version
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) version
|
|
|
|
{
|
2015-11-01 22:49:21 +00:00
|
|
|
return nil;
|
2015-11-01 22:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertChild: (NSXMLNode*)child atIndex: (NSUInteger)index
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insertChildren: (NSArray*)children atIndex: (NSUInteger)index
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeChildAtIndex: (NSUInteger)index
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setChildren: (NSArray*)children
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addChild: (NSXMLNode*)child
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) replaceChildAtIndex: (NSUInteger)index withNode: (NSXMLNode*)theNode
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData*) XMLData
|
|
|
|
{
|
2015-11-01 22:49:21 +00:00
|
|
|
return nil;
|
2015-11-01 22:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData *) XMLDataWithOptions: (NSUInteger)theOptions
|
|
|
|
{
|
2015-11-01 22:49:21 +00:00
|
|
|
return nil;
|
2015-11-01 22:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectByApplyingXSLT: (NSData*)xslt
|
|
|
|
arguments: (NSDictionary*)arguments
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectByApplyingXSLTString: (NSString*)xslt
|
|
|
|
arguments: (NSDictionary*)arguments
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectByApplyingXSLTAtURL: (NSURL*)xsltURL
|
|
|
|
arguments: (NSDictionary*)arguments
|
|
|
|
error: (NSError**)error
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) validateAndReturnError: (NSError**)error
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone *)zone
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)other
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2015-11-03 09:15:26 +00:00
|
|
|
#endif /* HAVE_LIBXML */
|