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-12 13:27:32 +00:00
|
|
|
#define GS_XMLNODETYPE xmlDoc
|
|
|
|
#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>
|
|
|
|
#import <Foundation/NSError.h>
|
2011-12-30 21:40:12 +00:00
|
|
|
|
2012-02-20 03:40:15 +00:00
|
|
|
#if defined(HAVE_LIBXML)
|
2012-01-06 02:43:26 +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->docType release];
|
|
|
|
[internal->MIMEType release];
|
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) characterEncoding
|
|
|
|
{
|
2012-03-12 13:27:32 +00:00
|
|
|
if (internal->node->encoding)
|
|
|
|
return StringFromXMLStringPtr(internal->node->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
|
|
|
|
{
|
2012-01-04 12:41:45 +00:00
|
|
|
return internal->docType;
|
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";
|
|
|
|
int options = XML_PARSE_NOERROR;
|
|
|
|
xmlDocPtr doc = NULL;
|
|
|
|
|
|
|
|
if (!(mask & NSXMLNodePreserveWhitespace))
|
|
|
|
{
|
|
|
|
options |= XML_PARSE_NOBLANKS;
|
|
|
|
//xmlKeepBlanksDefault(0);
|
|
|
|
}
|
|
|
|
doc = xmlReadMemory([data bytes], [data length],
|
|
|
|
url, encoding, options);
|
|
|
|
if (doc == NULL)
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
if (error != NULL)
|
|
|
|
{
|
|
|
|
*error = [NSError errorWithDomain: @"NSXMLErrorDomain"
|
|
|
|
code: 0
|
|
|
|
userInfo: nil];
|
|
|
|
}
|
|
|
|
}
|
2012-03-04 21:40:39 +00:00
|
|
|
// FIXME: Free old node
|
2012-02-27 12:53:25 +00:00
|
|
|
[self _setNode: doc];
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
return [super initWithKind: kind options: theOptions];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
return [[NSXMLNode alloc] initWithKind: kind
|
2012-03-04 21:40:39 +00:00
|
|
|
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
|
|
|
|
{
|
2012-03-12 13:27:32 +00:00
|
|
|
return (internal->node->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-03-12 13:27:32 +00:00
|
|
|
xmlNodePtr rootElem = xmlDocGetRootElement(internal->node);
|
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
|
|
|
|
{
|
2012-03-12 13:27:32 +00:00
|
|
|
internal->node->encoding = XMLStringCopy(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-02-20 03:40:15 +00:00
|
|
|
NSAssert(documentTypeDeclaration != nil, NSInvalidArgumentException);
|
2012-01-04 12:41:45 +00:00
|
|
|
ASSIGNCOPY(internal->docType, documentTypeDeclaration);
|
2012-03-12 13:27:32 +00:00
|
|
|
internal->node->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-02-20 03:40:15 +00:00
|
|
|
id oldElement = [self rootElement];
|
|
|
|
|
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-04 21:40:39 +00:00
|
|
|
// FIXME: Should remove all sub nodes
|
|
|
|
|
2012-03-12 13:27:32 +00:00
|
|
|
xmlDocSetRootElement(internal->node, [root _node]);
|
2012-02-20 03:40:15 +00:00
|
|
|
|
|
|
|
// Do our subNode housekeeping...
|
2012-02-27 12:53:25 +00:00
|
|
|
[self _removeSubNode: oldElement];
|
|
|
|
[self _addSubNode: root];
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStandalone: (BOOL)standalone
|
|
|
|
{
|
2012-03-12 13:27:32 +00:00
|
|
|
internal->node->standalone = standalone;
|
2009-02-09 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setVersion: (NSString*)version
|
|
|
|
{
|
|
|
|
if ([version isEqualToString: @"1.0"] || [version isEqualToString: @"1.1"])
|
|
|
|
{
|
2012-03-12 13:27:32 +00:00
|
|
|
internal->node->version = XMLStringCopy(version);
|
2012-02-20 03:40:15 +00:00
|
|
|
}
|
2009-02-09 16:16:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Bad XML version (%@)", version];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) version
|
|
|
|
{
|
2012-03-12 13:27:32 +00:00
|
|
|
if (internal->node->version)
|
|
|
|
return StringFromXMLStringPtr(internal->node->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-02-20 03:40:15 +00:00
|
|
|
NSXMLNodeKind kind = [child kind];
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
2012-02-20 03:40:15 +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
|
|
|
|
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-02-20 03:40:15 +00:00
|
|
|
NSString *string = nil;
|
|
|
|
xmlChar *buf = NULL;
|
|
|
|
int length;
|
2012-01-05 15:44:45 +00:00
|
|
|
|
2012-03-12 13:27:32 +00:00
|
|
|
xmlDocDumpFormatMemoryEnc(internal->node, &buf, &length, "utf-8",
|
2012-03-01 06:33:11 +00:00
|
|
|
((options & NSXMLNodePrettyPrint) ? 1 : 0));
|
2012-02-20 03:40:15 +00:00
|
|
|
|
|
|
|
if (buf != 0 && length > 0)
|
2012-01-05 15:44:45 +00:00
|
|
|
{
|
2012-02-20 03:40:15 +00:00
|
|
|
string = StringFromXMLString(buf, length);
|
|
|
|
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
|
|
|
|
{
|
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],
|
|
|
|
NULL, NULL, XML_PARSE_NOERROR);
|
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
|
|
|
|
|
|
|
*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);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the stylesheet and get the result...
|
2012-03-12 13:27:32 +00:00
|
|
|
resultDoc
|
|
|
|
= xsltApplyStylesheet(stylesheet, internal->node, (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];
|
|
|
|
#else
|
2009-02-09 16:16:11 +00:00
|
|
|
return nil;
|
2012-02-20 03:40:15 +00:00
|
|
|
#endif
|
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
|
2012-03-12 13:27:32 +00:00
|
|
|
BOOL result = (BOOL)(xmlValidateDocument(ctxt, internal->node));
|
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]];
|
|
|
|
[c setDTD: [self DTD]];
|
|
|
|
[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-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
|
|
|
|
|
|
|
#endif
|